diff options
Diffstat (limited to 'test/console')
-rw-r--r-- | test/console/actions/console.test.ts | 52 | ||||
-rw-r--r-- | test/console/commandline/CommandLineParser.test.ts | 10 | ||||
-rw-r--r-- | test/console/commandline/CommandParser.test.ts | 10 | ||||
-rw-r--r-- | test/console/components/console/Completion.test.tsx | 243 | ||||
-rw-r--r-- | test/console/reducers/console.test.ts | 197 |
5 files changed, 278 insertions, 234 deletions
diff --git a/test/console/actions/console.test.ts b/test/console/actions/console.test.ts index e6567b2..3169c4b 100644 --- a/test/console/actions/console.test.ts +++ b/test/console/actions/console.test.ts @@ -1,69 +1,77 @@ -import * as actions from '../../../src/console/actions'; -import * as consoleActions from '../../../src/console/actions/console'; -import { expect } from 'chai'; +import * as actions from "../../../src/console/actions"; +import * as consoleActions from "../../../src/console/actions/console"; +import { expect } from "chai"; + +// eslint-disable-next-line @typescript-eslint/ban-ts-ignore +// @ts-ignore +import browserFake from "webextensions-api-fake"; describe("console actions", () => { - describe('hide', () => { - it('create CONSOLE_HIDE action', () => { + beforeEach(() => { + (global as any).browser = browserFake(); + }); + + describe("hide", () => { + it("create CONSOLE_HIDE action", () => { const action = consoleActions.hide(); expect(action.type).to.equal(actions.CONSOLE_HIDE); }); }); describe("showCommand", () => { - it('create CONSOLE_SHOW_COMMAND action', async () => { - const action = await consoleActions.showCommand('hello'); + it("create CONSOLE_SHOW_COMMAND action", async () => { + const action = await consoleActions.showCommand("hello"); expect(action.type).to.equal(actions.CONSOLE_SHOW_COMMAND); - expect(action.text).to.equal('hello'); + expect(action.text).to.equal("hello"); }); }); describe("showFind", () => { - it('create CONSOLE_SHOW_FIND action', () => { + it("create CONSOLE_SHOW_FIND action", () => { const action = consoleActions.showFind(); expect(action.type).to.equal(actions.CONSOLE_SHOW_FIND); }); }); describe("showError", () => { - it('create CONSOLE_SHOW_ERROR action', () => { - const action = consoleActions.showError('an error'); + it("create CONSOLE_SHOW_ERROR action", () => { + const action = consoleActions.showError("an error"); expect(action.type).to.equal(actions.CONSOLE_SHOW_ERROR); - expect(action.text).to.equal('an error'); + expect(action.text).to.equal("an error"); }); }); describe("showInfo", () => { - it('create CONSOLE_SHOW_INFO action', () => { - const action = consoleActions.showInfo('an info'); + it("create CONSOLE_SHOW_INFO action", () => { + const action = consoleActions.showInfo("an info"); expect(action.type).to.equal(actions.CONSOLE_SHOW_INFO); - expect(action.text).to.equal('an info'); + expect(action.text).to.equal("an info"); }); }); describe("hideCommand", () => { - it('create CONSOLE_HIDE_COMMAND action', () => { + it("create CONSOLE_HIDE_COMMAND action", () => { const action = consoleActions.hideCommand(); expect(action.type).to.equal(actions.CONSOLE_HIDE_COMMAND); }); }); - describe('setConsoleText', () => { - it('create CONSOLE_SET_CONSOLE_TEXT action', () => { - const action = consoleActions.setConsoleText('hello world'); + describe("setConsoleText", () => { + it("create CONSOLE_SET_CONSOLE_TEXT action", () => { + const action = consoleActions.setConsoleText("hello world"); expect(action.type).to.equal(actions.CONSOLE_SET_CONSOLE_TEXT); - expect(action.consoleText).to.equal('hello world'); + expect(action.consoleText).to.equal("hello world"); }); }); describe("completionPrev", () => { - it('create CONSOLE_COMPLETION_PREV action', () => { + it("create CONSOLE_COMPLETION_PREV action", () => { const action = consoleActions.completionPrev(); expect(action.type).to.equal(actions.CONSOLE_COMPLETION_PREV); }); }); describe("completionNext", () => { - it('create CONSOLE_COMPLETION_NEXT action', () => { + it("create CONSOLE_COMPLETION_NEXT action", () => { const action = consoleActions.completionNext(); expect(action.type).to.equal(actions.CONSOLE_COMPLETION_NEXT); }); diff --git a/test/console/commandline/CommandLineParser.test.ts b/test/console/commandline/CommandLineParser.test.ts index 6aec682..7cba04c 100644 --- a/test/console/commandline/CommandLineParser.test.ts +++ b/test/console/commandline/CommandLineParser.test.ts @@ -1,4 +1,6 @@ -import CommandLineParser, {InputPhase} from "../../../src/console/commandline/CommandLineParser"; +import CommandLineParser, { + InputPhase, +} from "../../../src/console/commandline/CommandLineParser"; import { Command } from "../../../src/shared/Command"; import { expect } from "chai"; @@ -9,7 +11,7 @@ describe("CommandLineParser", () => { expect(sut.inputPhase("")).to.equal(InputPhase.OnCommand); expect(sut.inputPhase("op")).to.equal(InputPhase.OnCommand); expect(sut.inputPhase("open ")).to.equal(InputPhase.OnArgs); - expect(sut.inputPhase("open apple")).to.equal(InputPhase.OnArgs) + expect(sut.inputPhase("open apple")).to.equal(InputPhase.OnArgs); }); }); describe("#parse", () => { @@ -24,6 +26,6 @@ describe("CommandLineParser", () => { command: Command.QuitAll, args: "", }); - }) - }) + }); + }); }); diff --git a/test/console/commandline/CommandParser.test.ts b/test/console/commandline/CommandParser.test.ts index 4ad78fd..f72afd6 100644 --- a/test/console/commandline/CommandParser.test.ts +++ b/test/console/commandline/CommandParser.test.ts @@ -1,6 +1,8 @@ -import CommandParser, { UnknownCommandError } from "../../../src/console/commandline/CommandParser"; +import CommandParser, { + UnknownCommandError, +} from "../../../src/console/commandline/CommandParser"; import { Command } from "../../../src/shared/Command"; -import { expect } from "chai" +import { expect } from "chai"; describe("CommandParser", () => { describe("#parse", () => { @@ -10,6 +12,6 @@ describe("CommandParser", () => { expect(sut.parse("w")).to.equal(Command.WindowOpen); expect(sut.parse("bdelete!")).to.equal(Command.BufferDeleteForce); expect(() => sut.parse("harakiri")).to.throw(UnknownCommandError); - }) - }) + }); + }); }); diff --git a/test/console/components/console/Completion.test.tsx b/test/console/components/console/Completion.test.tsx index e411c4a..921720b 100644 --- a/test/console/components/console/Completion.test.tsx +++ b/test/console/components/console/Completion.test.tsx @@ -1,166 +1,165 @@ -import React from 'react'; -import Completion from 'console/components/console/Completion' -import ReactTestRenderer from 'react-test-renderer'; +import React from "react"; +import Completion from "../../../../src/console/components/console/Completion"; +import ReactTestRenderer, { ReactTestInstance } from "react-test-renderer"; +import { expect } from "chai"; +import CompletionTitle from "../../../../src/console/components/console/CompletionTitle"; +import CompletionItem from "../../../../src/console/components/console/CompletionItem"; describe("console/components/console/completion", () => { - const completions = [{ - name: "Fruit", - items: [{ caption: "apple" }, { caption: "banana" }, { caption: "cherry" }], - }, { - name: "Element", - items: [{ caption: "argon" }, { caption: "boron" }, { caption: "carbon" }], - }]; - - it('renders Completion component', () => { - const root = ReactTestRenderer.create(<Completion - completions={completions} - size={30} - />).root; - - expect(root.children).to.have.lengthOf(1); - - const children = root.children[0].children; + const completions = [ + { + name: "Fruit", + items: [ + { caption: "apple" }, + { caption: "banana" }, + { caption: "cherry" }, + ], + }, + { + name: "Element", + items: [ + { caption: "argon" }, + { caption: "boron" }, + { caption: "carbon" }, + ], + }, + ]; + + it("renders Completion component", () => { + const root = ReactTestRenderer.create( + <Completion completions={completions} size={30} select={-1} /> + ).root; + + // const children = root.findByType('ul').children as Array<ReactTestInstance>; + const children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children).to.have.lengthOf(8); - expect(children[0].props.title).to.equal('Fruit'); - expect(children[1].props.caption).to.equal('apple'); - expect(children[2].props.caption).to.equal('banana'); - expect(children[3].props.caption).to.equal('cherry'); - expect(children[4].props.title).to.equal('Element'); - expect(children[5].props.caption).to.equal('argon'); - expect(children[6].props.caption).to.equal('boron'); - expect(children[7].props.caption).to.equal('carbon'); + expect(children.map((e) => e.type)).to.deep.equal([ + CompletionTitle, + CompletionItem, + CompletionItem, + CompletionItem, + CompletionTitle, + CompletionItem, + CompletionItem, + CompletionItem, + ]); + expect(children[0].props.title).to.equal("Fruit"); + expect(children[1].props.caption).to.equal("apple"); + expect(children[2].props.caption).to.equal("banana"); + expect(children[3].props.caption).to.equal("cherry"); + expect(children[4].props.title).to.equal("Element"); + expect(children[5].props.caption).to.equal("argon"); + expect(children[6].props.caption).to.equal("boron"); + expect(children[7].props.caption).to.equal("carbon"); }); - it('highlight current item', () => { - const root = ReactTestRenderer.create(<Completion - completions={completions} - size={30} - select={3} - />).root; + it("highlight current item", () => { + const root = ReactTestRenderer.create( + <Completion completions={completions} size={30} select={3} /> + ).root; - const children = root.children[0].children; + const children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children[5].props.highlight).to.be.true; }); - it('does not highlight any items', () => { - const root = ReactTestRenderer.create(<Completion - completions={completions} - size={30} - select={-1} - />).root; - - const children = root.children[0].children; - for (const li of children[0].children) { - expect(li.props.highlight).not.to.be.ok; - } + it("does not highlight any items", () => { + const root = ReactTestRenderer.create( + <Completion completions={completions} size={30} select={-1} /> + ).root; + + const children = root.findByType("ul").findAllByType(CompletionItem); + expect(children.every((e) => e.props.highlight === false)).to.be.true; }); - it('limits completion items', () => { - let root = ReactTestRenderer.create(<Completion - completions={completions} - size={3} - select={-1} - />).root; + it("limits completion items", () => { + let root = ReactTestRenderer.create( + <Completion completions={completions} size={3} select={-1} /> + ).root; - let children = root.children[0].children; + let children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children).to.have.lengthOf(3); - expect(children[0].props.title).to.equal('Fruit'); - expect(children[1].props.caption).to.equal('apple'); - expect(children[2].props.caption).to.equal('banana'); + expect(children[0].props.title).to.equal("Fruit"); + expect(children[1].props.caption).to.equal("apple"); + expect(children[2].props.caption).to.equal("banana"); - root = ReactTestRenderer.create(<Completion - completions={completions} - size={3} select={0} - />).root; + root = ReactTestRenderer.create( + <Completion completions={completions} size={3} select={0} /> + ).root; - children = root.children[0].children; + children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children[1].props.highlight).to.be.true; - }) - - it('scrolls up to down with select', () => { - const component = ReactTestRenderer.create(<Completion - completions={completions} - size={3} - select={1} - />); + }); + + it("scrolls up to down with select", () => { + const component = ReactTestRenderer.create( + <Completion completions={completions} size={3} select={1} /> + ); const root = component.root; - let children = root.children[0].children; + let children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children).to.have.lengthOf(3); - expect(children[0].props.title).to.equal('Fruit'); - expect(children[1].props.caption).to.equal('apple'); - expect(children[2].props.caption).to.equal('banana'); + expect(children[0].props.title).to.equal("Fruit"); + expect(children[1].props.caption).to.equal("apple"); + expect(children[2].props.caption).to.equal("banana"); - component.update(<Completion - completions={completions} - size={3} - select={2} - />); + component.update( + <Completion completions={completions} size={3} select={2} /> + ); - children = root.children[0].children; + children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children).to.have.lengthOf(3); - expect(children[0].props.caption).to.equal('apple'); - expect(children[1].props.caption).to.equal('banana'); - expect(children[2].props.caption).to.equal('cherry'); + expect(children[0].props.caption).to.equal("apple"); + expect(children[1].props.caption).to.equal("banana"); + expect(children[2].props.caption).to.equal("cherry"); expect(children[2].props.highlight).to.be.true; - component.update(<Completion - completions={completions} - size={3} - select={3} - />); + component.update( + <Completion completions={completions} size={3} select={3} /> + ); - children = root.children[0].children; + children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children).to.have.lengthOf(3); - expect(children[0].props.caption).to.equal('cherry'); - expect(children[1].props.title).to.equal('Element'); - expect(children[2].props.caption).to.equal('argon'); + expect(children[0].props.caption).to.equal("cherry"); + expect(children[1].props.title).to.equal("Element"); + expect(children[2].props.caption).to.equal("argon"); expect(children[2].props.highlight).to.be.true; }); - it('scrolls down to up with select', () => { - const component = ReactTestRenderer.create(<Completion - completions={completions} - size={3} - select={5} - />); + it("scrolls down to up with select", () => { + const component = ReactTestRenderer.create( + <Completion completions={completions} size={3} select={5} /> + ); const root = component.root; - let children = root.children[0].children; + let children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children).to.have.lengthOf(3); - expect(children[0].props.caption).to.equal('argon'); - expect(children[1].props.caption).to.equal('boron'); - expect(children[2].props.caption).to.equal('carbon'); + expect(children[0].props.caption).to.equal("argon"); + expect(children[1].props.caption).to.equal("boron"); + expect(children[2].props.caption).to.equal("carbon"); - component.update(<Completion - completions={completions} - size={3} - select={4} - />); + component.update( + <Completion completions={completions} size={3} select={4} /> + ); - children = root.children[0].children; + children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children[1].props.highlight).to.be.true; - component.update(<Completion - completions={completions} - size={3} - select={3} - />); + component.update( + <Completion completions={completions} size={3} select={3} /> + ); - children = root.children[0].children; + children = root.findByType("ul").children as Array<ReactTestInstance>; expect(children[0].props.highlight).to.be.true; - component.update(<Completion - completions={completions} - size={3} - select={2} - />); + component.update( + <Completion completions={completions} size={3} select={2} /> + ); - children = root.children[0].children; - expect(children[0].props.caption).to.equal('cherry'); - expect(children[1].props.title).to.equal('Element'); - expect(children[2].props.caption).to.equal('argon'); + children = root.findByType("ul").children as Array<ReactTestInstance>; + expect(children[0].props.caption).to.equal("cherry"); + expect(children[1].props.title).to.equal("Element"); + expect(children[2].props.caption).to.equal("argon"); expect(children[0].props.highlight).to.be.true; }); }); diff --git a/test/console/reducers/console.test.ts b/test/console/reducers/console.test.ts index 038e712..64e8eb3 100644 --- a/test/console/reducers/console.test.ts +++ b/test/console/reducers/console.test.ts @@ -1,131 +1,164 @@ -import * as actions from 'console/actions'; -import reducer from 'console/reducers'; +import * as actions from "../../../src/console/actions"; +import reducer, { State } from "../../../src/console/reducers"; +import { expect } from "chai"; +import CompletionType from "../../../src/shared/CompletionType"; +import { ConsoleAction } from "../../../src/console/actions"; describe("console reducer", () => { - it('return the initial state', () => { - const state = reducer(undefined, {}); - expect(state).to.have.property('mode', ''); - expect(state).to.have.property('messageText', ''); - expect(state).to.have.property('consoleText', ''); - expect(state).to.have.deep.property('completions', []); - expect(state).to.have.property('select', -1); + it("return the initial state", () => { + const state = reducer(undefined, {} as any); + expect(state).to.have.property("mode", ""); + expect(state).to.have.property("messageText", ""); + expect(state).to.have.property("consoleText", ""); + expect(state).to.have.deep.property("completions", []); + expect(state).to.have.property("select", -1); }); - it('return next state for CONSOLE_HIDE', () => { - const action = { type: actions.CONSOLE_HIDE }; - const state = reducer({ mode: 'error' }, action); - expect(state).to.have.property('mode', ''); - }) - - it('return next state for CONSOLE_SHOW_COMMAND', () => { - const action = { type: actions.CONSOLE_SHOW_COMMAND, text: 'open ' }; - const state = reducer({}, action); - expect(state).to.have.property('mode', 'command'); - expect(state).to.have.property('consoleText', 'open '); + it("return next state for CONSOLE_HIDE", () => { + const initialState = reducer(undefined, {} as any); + const action: actions.ConsoleAction = { type: actions.CONSOLE_HIDE }; + const state = reducer({ ...initialState, mode: "error" }, action); + expect(state).to.have.property("mode", ""); }); - it('return next state for CONSOLE_SHOW_INFO', () => { - const action = { type: actions.CONSOLE_SHOW_INFO, text: 'an info' }; - const state = reducer({}, action); - expect(state).to.have.property('mode', 'info'); - expect(state).to.have.property('messageText', 'an info'); + it("return next state for CONSOLE_SHOW_COMMAND", () => { + const action: actions.ConsoleAction = { + type: actions.CONSOLE_SHOW_COMMAND, + completionTypes: [CompletionType.SearchEngines, CompletionType.History], + text: "open ", + }; + const state = reducer(undefined, action); + expect(state).to.have.property("mode", "command"); + expect(state).to.have.property("consoleText", "open "); }); - it('return next state for CONSOLE_SHOW_ERROR', () => { - const action = { type: actions.CONSOLE_SHOW_ERROR, text: 'an error' }; - const state = reducer({}, action); - expect(state).to.have.property('mode', 'error'); - expect(state).to.have.property('messageText', 'an error'); + it("return next state for CONSOLE_SHOW_INFO", () => { + const action: actions.ConsoleAction = { + type: actions.CONSOLE_SHOW_INFO, + text: "an info", + }; + const state = reducer(undefined, action); + expect(state).to.have.property("mode", "info"); + expect(state).to.have.property("messageText", "an info"); + }); + + it("return next state for CONSOLE_SHOW_ERROR", () => { + const action: actions.ConsoleAction = { + type: actions.CONSOLE_SHOW_ERROR, + text: "an error", + }; + const state = reducer(undefined, action); + expect(state).to.have.property("mode", "error"); + expect(state).to.have.property("messageText", "an error"); }); - it('return next state for CONSOLE_HIDE_COMMAND', () => { - const action = { type: actions.CONSOLE_HIDE_COMMAND }; - let state = reducer({ mode: 'command' }, action); - expect(state).to.have.property('mode', ''); + it("return next state for CONSOLE_HIDE_COMMAND", () => { + const initialState = reducer(undefined, {} as any); + const action: actions.ConsoleAction = { + type: actions.CONSOLE_HIDE_COMMAND, + }; + let state = reducer({ ...initialState, mode: "command" }, action); + expect(state).to.have.property("mode", ""); - state = reducer({ mode: 'error' }, action); - expect(state).to.have.property('mode', 'error'); + state = reducer({ ...initialState, mode: "error" }, action); + expect(state).to.have.property("mode", "error"); }); - it('return next state for CONSOLE_SET_CONSOLE_TEXT', () => { - const action = { + it("return next state for CONSOLE_SET_CONSOLE_TEXT", () => { + const action: actions.ConsoleAction = { type: actions.CONSOLE_SET_CONSOLE_TEXT, - consoleText: 'hello world' - } - const state = reducer({}, action) + consoleText: "hello world", + }; + const state = reducer(undefined, action); - expect(state).to.have.property('consoleText', 'hello world'); + expect(state).to.have.property("consoleText", "hello world"); }); - it ('return next state for CONSOLE_SET_COMPLETIONS', () => { - let state = { + it("return next state for CONSOLE_SET_COMPLETIONS", () => { + const initialState = reducer(undefined, {} as any); + let state: State = { + ...initialState, select: 0, completions: [], - } - const action = { + }; + const action: actions.ConsoleAction = { type: actions.CONSOLE_SET_COMPLETIONS, - completions: [{ - name: 'Apple', - items: [1, 2, 3] - }, { - name: 'Banana', - items: [4, 5, 6] - }] - } + completions: [ + { + name: "Apple", + items: [{}, {}, {}], + }, + { + name: "Banana", + items: [{}, {}, {}], + }, + ], + completionSource: "", + }; state = reducer(state, action); - expect(state).to.have.property('completions', action.completions); - expect(state).to.have.property('select', -1); + expect(state).to.have.property("completions", action.completions); + expect(state).to.have.property("select", -1); }); - it ('return next state for CONSOLE_COMPLETION_NEXT', () => { - const action = { type: actions.CONSOLE_COMPLETION_NEXT }; + it("return next state for CONSOLE_COMPLETION_NEXT", () => { + const initialState = reducer(undefined, {} as any); + const action: ConsoleAction = { type: actions.CONSOLE_COMPLETION_NEXT }; let state = { + ...initialState, select: -1, - completions: [{ - name: 'Apple', - items: [1, 2] - }, { - name: 'Banana', - items: [3] - }] + completions: [ + { + name: "Apple", + items: [{}, {}], + }, + { + name: "Banana", + items: [{}], + }, + ], }; state = reducer(state, action); - expect(state).to.have.property('select', 0); + expect(state).to.have.property("select", 0); state = reducer(state, action); - expect(state).to.have.property('select', 1); + expect(state).to.have.property("select", 1); state = reducer(state, action); - expect(state).to.have.property('select', 2); + expect(state).to.have.property("select", 2); state = reducer(state, action); - expect(state).to.have.property('select', -1); + expect(state).to.have.property("select", -1); }); - it ('return next state for CONSOLE_COMPLETION_PREV', () => { - const action = { type: actions.CONSOLE_COMPLETION_PREV }; + it("return next state for CONSOLE_COMPLETION_PREV", () => { + const initialState = reducer(undefined, {} as any); + const action: ConsoleAction = { type: actions.CONSOLE_COMPLETION_PREV }; let state = { + ...initialState, select: -1, - completions: [{ - name: 'Apple', - items: [1, 2] - }, { - name: 'Banana', - items: [3] - }] + completions: [ + { + name: "Apple", + items: [{}, {}], + }, + { + name: "Banana", + items: [{}], + }, + ], }; state = reducer(state, action); - expect(state).to.have.property('select', 2); + expect(state).to.have.property("select", 2); state = reducer(state, action); - expect(state).to.have.property('select', 1); + expect(state).to.have.property("select", 1); state = reducer(state, action); - expect(state).to.have.property('select', 0); + expect(state).to.have.property("select", 0); state = reducer(state, action); - expect(state).to.have.property('select', -1); + expect(state).to.have.property("select", -1); }); }); |