diff options
Diffstat (limited to 'test/console')
-rw-r--r-- | test/console/actions/console.test.ts | 77 | ||||
-rw-r--r-- | test/console/app/actions.test.ts | 62 | ||||
-rw-r--r-- | test/console/app/reducer.test.ts | 85 | ||||
-rw-r--r-- | test/console/completion/reducer.test.ts | 168 | ||||
-rw-r--r-- | test/console/components/ErrorMessage.test.tsx | 17 | ||||
-rw-r--r-- | test/console/components/InfoMessage.test.tsx | 17 | ||||
-rw-r--r-- | test/console/components/console/Completion.test.tsx | 61 | ||||
-rw-r--r-- | test/console/components/console/Message.test.tsx | 27 | ||||
-rw-r--r-- | test/console/reducers/console.test.ts | 164 |
9 files changed, 389 insertions, 289 deletions
diff --git a/test/console/actions/console.test.ts b/test/console/actions/console.test.ts deleted file mode 100644 index a03117a..0000000 --- a/test/console/actions/console.test.ts +++ /dev/null @@ -1,77 +0,0 @@ -import * as actions from "../../../src/console/actions"; -import * as consoleActions from "../../../src/console/actions/console"; -import { expect } from "chai"; - -import browserFake from "webextensions-api-fake"; - -describe("console actions", () => { - 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"); - expect(action.type).to.equal(actions.CONSOLE_SHOW_COMMAND); - expect(action.text).to.equal("hello"); - }); - }); - - describe("showFind", () => { - 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"); - expect(action.type).to.equal(actions.CONSOLE_SHOW_ERROR); - expect(action.text).to.equal("an error"); - }); - }); - - describe("showInfo", () => { - 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"); - }); - }); - - describe("hideCommand", () => { - 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"); - expect(action.type).to.equal(actions.CONSOLE_SET_CONSOLE_TEXT); - expect(action.consoleText).to.equal("hello world"); - }); - }); - - describe("completionPrev", () => { - 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", () => { - const action = consoleActions.completionNext(); - expect(action.type).to.equal(actions.CONSOLE_COMPLETION_NEXT); - }); - }); -}); diff --git a/test/console/app/actions.test.ts b/test/console/app/actions.test.ts new file mode 100644 index 0000000..2f9dc71 --- /dev/null +++ b/test/console/app/actions.test.ts @@ -0,0 +1,62 @@ +import * as consoleActions from "../../../src/console/app/actions"; +import { + HIDE, + HIDE_COMMAND, + SHOW_COMMAND, + SHOW_ERROR, + SHOW_FIND, + SHOW_INFO, +} from "../../../src/console/app/actions"; +import { expect } from "chai"; + +import browserFake from "webextensions-api-fake"; + +describe("console actions", () => { + beforeEach(() => { + (global as any).browser = browserFake(); + }); + + describe("hide", () => { + it("create CONSOLE_HIDE action", () => { + const action = consoleActions.hide(); + expect(action.type).to.equal(HIDE); + }); + }); + describe("showCommand", () => { + it("create CONSOLE_SHOW_COMMAND action", async () => { + const action = await consoleActions.showCommand("hello"); + expect(action.type).to.equal(SHOW_COMMAND); + expect(action.text).to.equal("hello"); + }); + }); + + describe("showFind", () => { + it("create CONSOLE_SHOW_FIND action", () => { + const action = consoleActions.showFind(); + expect(action.type).to.equal(SHOW_FIND); + }); + }); + + describe("showError", () => { + it("create CONSOLE_SHOW_ERROR action", () => { + const action = consoleActions.showError("an error"); + expect(action.type).to.equal(SHOW_ERROR); + expect(action.text).to.equal("an error"); + }); + }); + + describe("showInfo", () => { + it("create CONSOLE_SHOW_INFO action", () => { + const action = consoleActions.showInfo("an info"); + expect(action.type).to.equal(SHOW_INFO); + expect(action.text).to.equal("an info"); + }); + }); + + describe("hideCommand", () => { + it("create CONSOLE_HIDE_COMMAND action", () => { + const action = consoleActions.hideCommand(); + expect(action.type).to.equal(HIDE_COMMAND); + }); + }); +}); diff --git a/test/console/app/reducer.test.ts b/test/console/app/reducer.test.ts new file mode 100644 index 0000000..4406adc --- /dev/null +++ b/test/console/app/reducer.test.ts @@ -0,0 +1,85 @@ +import { expect } from "chai"; +import reducer, { defaultState, State } from "../../../src/console/app/recuer"; +import { + hide, + hideCommand, + showCommand, + showError, + showFind, + showInfo, +} from "../../../src/console/app/actions"; + +describe("app reducer", () => { + describe("hide", () => { + it("switches to none mode", () => { + const initialState: State = { + ...defaultState, + mode: "info", + }; + const nextState = reducer(initialState, hide()); + + expect(nextState.mode).to.be.empty; + }); + }); + + describe("showCommand", () => { + it("switches to command mode with a message", () => { + const nextState = reducer(defaultState, showCommand("open ")); + + expect(nextState.mode).equals("command"); + expect(nextState.consoleText).equals("open "); + }); + }); + + describe("showFind", () => { + it("switches to find mode with a message", () => { + const nextState = reducer(defaultState, showFind()); + + expect(nextState.mode).equals("find"); + }); + }); + + describe("showError", () => { + it("switches to error message mode with a message", () => { + const nextState = reducer(defaultState, showError("error occurs")); + + expect(nextState.mode).equals("error"); + expect(nextState.messageText).equals("error occurs"); + }); + }); + + describe("showInfo", () => { + it("switches to info message mode with a message", () => { + const nextState = reducer(defaultState, showInfo("what's up")); + + expect(nextState.mode).equals("info"); + expect(nextState.messageText).equals("what's up"); + }); + }); + + describe("hideCommand", () => { + describe("when command mode", () => { + it("switches to none mode", () => { + const initialState: State = { + ...defaultState, + mode: "command", + }; + const nextState = reducer(initialState, hideCommand()); + + expect(nextState.mode).to.be.empty; + }); + }); + + describe("when info message mode", () => { + it("does nothing", () => { + const initialState: State = { + ...defaultState, + mode: "info", + }; + const nextState = reducer(initialState, hideCommand()); + + expect(nextState.mode).equals("info"); + }); + }); + }); +}); diff --git a/test/console/completion/reducer.test.ts b/test/console/completion/reducer.test.ts new file mode 100644 index 0000000..b742872 --- /dev/null +++ b/test/console/completion/reducer.test.ts @@ -0,0 +1,168 @@ +import reducer, { + defaultState, + State, +} from "../../../src/console/completion/reducer"; +import { expect } from "chai"; +import { + initCompletion, + selectNext, + selectPrev, + setCompletions, + setCompletionSource, +} from "../../../src/console/completion/actions"; +import CompletionType from "../../../src/shared/CompletionType"; + +describe("completion reducer", () => { + describe("initCompletion", () => { + it("initializes completions", () => { + const nextState = reducer( + defaultState, + initCompletion([CompletionType.Bookmarks, CompletionType.History]) + ); + + expect(nextState.completionTypes).deep.equals([ + CompletionType.Bookmarks, + CompletionType.History, + ]); + }); + }); + + describe("setCompletionSource", () => { + it("sets a completion source", () => { + const nextState = reducer(defaultState, setCompletionSource("open ")); + + expect(nextState.completionSource).equals("open "); + }); + }); + + describe("setCompletions", () => { + it("sets completions", () => { + const nextState = reducer( + defaultState, + setCompletions([ + { + name: "Apple", + items: [{}, {}], + }, + { + name: "Banana", + items: [{}], + }, + ]) + ); + + expect(nextState.completions).deep.equals([ + { + name: "Apple", + items: [{}, {}], + }, + { + name: "Banana", + items: [{}], + }, + ]); + }); + }); + + describe("selectNext", () => { + context("when no completion groups", () => { + it("does nothing", () => { + const nextState = reducer(defaultState, selectNext()); + expect(nextState.select).equals(-1); + }); + }); + + context("when no completion items", () => { + it("does nothing", () => { + const state = { + ...defaultState, + completions: [{ name: "apple", items: [] }], + }; + const nextState = reducer(state, selectNext()); + expect(nextState.select).equals(-1); + }); + }); + + context("when completions exist", () => { + it("selects next selection", () => { + let state: State = { + ...defaultState, + select: -1, + completions: [ + { + name: "Apple", + items: [{}, {}], + }, + { + name: "Banana", + items: [{}], + }, + ], + }; + + state = reducer(state, selectNext()); + expect(state.select).equals(0); + + state = reducer(state, selectNext()); + expect(state.select).equals(1); + + state = reducer(state, selectNext()); + expect(state.select).equals(2); + + state = reducer(state, selectNext()); + expect(state.select).equals(-1); + }); + }); + }); + + describe("selectPrev", () => { + context("when no completion groups", () => { + it("does nothing", () => { + const nextState = reducer(defaultState, selectPrev()); + expect(nextState.select).equals(-1); + }); + + context("when no completion items", () => { + it("does nothing", () => { + const state = { + ...defaultState, + completions: [{ name: "apple", items: [] }], + }; + const nextState = reducer(state, selectPrev()); + expect(nextState.select).equals(-1); + }); + }); + }); + + context("when completions exist", () => { + it("selects a previous completion", () => { + let state: State = { + ...defaultState, + select: -1, + completions: [ + { + name: "Apple", + items: [{}, {}], + }, + { + name: "Banana", + items: [{}], + }, + ], + }; + + state = reducer(state, selectPrev()); + expect(state).to.have.property("select", 2); + + state = reducer(state, selectPrev()); + expect(state).to.have.property("select", 1); + + state = reducer(state, selectPrev()); + expect(state).to.have.property("select", 0); + + state = reducer(state, selectPrev()); + expect(state).to.have.property("select", -1); + }); + }); + }); +}); diff --git a/test/console/components/ErrorMessage.test.tsx b/test/console/components/ErrorMessage.test.tsx new file mode 100644 index 0000000..46ec0b0 --- /dev/null +++ b/test/console/components/ErrorMessage.test.tsx @@ -0,0 +1,17 @@ +import React from "react"; +import ReactTestRenderer from "react-test-renderer"; +import { expect } from "chai"; +import ErrorMessage from "../../../src/console/components/ErrorMessage"; + +describe("console/components/console/completion/ErrorMessage", () => { + it("renders an error message", () => { + const root = ReactTestRenderer.create( + <ErrorMessage mode="error">Hello!</ErrorMessage> + ).root; + + const p = root.findByType("p"); + + expect(p.props["role"]).to.equal("alert"); + expect(p.children).to.deep.equal(["Hello!"]); + }); +}); diff --git a/test/console/components/InfoMessage.test.tsx b/test/console/components/InfoMessage.test.tsx new file mode 100644 index 0000000..5b678ff --- /dev/null +++ b/test/console/components/InfoMessage.test.tsx @@ -0,0 +1,17 @@ +import React from "react"; +import ReactTestRenderer from "react-test-renderer"; +import { expect } from "chai"; +import InfoMessage from "../../../src/console/components/InfoMessage"; + +describe("console/components/console/completion/InfoMessage", () => { + it("renders an information message", () => { + const root = ReactTestRenderer.create( + <InfoMessage mode="info">Hello!</InfoMessage> + ).root; + + const p = root.findByType("p"); + + expect(p.props["role"]).to.equal("status"); + expect(p.children).to.deep.equal(["Hello!"]); + }); +}); diff --git a/test/console/components/console/Completion.test.tsx b/test/console/components/console/Completion.test.tsx index 0e4e21f..9b47637 100644 --- a/test/console/components/console/Completion.test.tsx +++ b/test/console/components/console/Completion.test.tsx @@ -100,9 +100,14 @@ describe("console/components/console/completion/Completion", () => { }); it("scrolls up to down with select", () => { - const component = ReactTestRenderer.create( - <Completion completions={completions} size={3} select={1} /> - ); + let component: ReturnType<ReactTestRenderer["create"]> | null = null; + + ReactTestRenderer.act(() => { + component = ReactTestRenderer.create( + <Completion completions={completions} size={3} select={1} /> + ); + }); + const root = component.root; let items = root.findAllByType(CompletionItem); @@ -126,9 +131,11 @@ describe("console/components/console/completion/Completion", () => { false, ]); - component.update( - <Completion completions={completions} size={3} select={2} /> - ); + ReactTestRenderer.act(() => { + component.update( + <Completion completions={completions} size={3} select={2} /> + ); + }); items = root.findAllByType(CompletionItem); showns = root .findAllByProps({ role: "group" }) @@ -151,9 +158,11 @@ describe("console/components/console/completion/Completion", () => { ]); expect(items[2].props.highlight).to.be.true; - component.update( - <Completion completions={completions} size={3} select={3} /> - ); + ReactTestRenderer.act(() => { + component.update( + <Completion completions={completions} size={3} select={3} /> + ); + }); items = root.findAllByType(CompletionItem); showns = root .findAllByProps({ role: "group" }) @@ -178,9 +187,13 @@ describe("console/components/console/completion/Completion", () => { }); it("scrolls down to up with select", () => { - const component = ReactTestRenderer.create( - <Completion completions={completions} size={3} select={5} /> - ); + let component: ReturnType<ReactTestRenderer["create"]> | null = null; + + ReactTestRenderer.act(() => { + component = ReactTestRenderer.create( + <Completion completions={completions} size={3} select={5} /> + ); + }); const root = component.root; let items = root.findAllByType(CompletionItem); @@ -206,9 +219,11 @@ describe("console/components/console/completion/Completion", () => { ]); expect(items[5].props.highlight).to.be.true; - component.update( - <Completion completions={completions} size={3} select={4} /> - ); + ReactTestRenderer.act(() => { + component.update( + <Completion completions={completions} size={3} select={4} /> + ); + }); items = root.findAllByType(CompletionItem); showns = root .findAllByProps({ role: "group" }) @@ -231,9 +246,11 @@ describe("console/components/console/completion/Completion", () => { ]); expect(items[4].props.highlight).to.be.true; - component.update( - <Completion completions={completions} size={3} select={3} /> - ); + ReactTestRenderer.act(() => { + component.update( + <Completion completions={completions} size={3} select={3} /> + ); + }); items = root.findAllByType(CompletionItem); showns = root .findAllByProps({ role: "group" }) @@ -256,9 +273,11 @@ describe("console/components/console/completion/Completion", () => { ]); expect(items[3].props.highlight).to.be.true; - component.update( - <Completion completions={completions} size={3} select={2} /> - ); + ReactTestRenderer.act(() => { + component.update( + <Completion completions={completions} size={3} select={2} /> + ); + }); items = root.findAllByType(CompletionItem); showns = root .findAllByProps({ role: "group" }) diff --git a/test/console/components/console/Message.test.tsx b/test/console/components/console/Message.test.tsx deleted file mode 100644 index f8f950a..0000000 --- a/test/console/components/console/Message.test.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import React from "react"; -import ReactTestRenderer from "react-test-renderer"; -import { expect } from "chai"; -import Message from "../../../../src/console/components/console/Message"; - -describe("console/components/console/completion/Message", () => { - it("renders an information message", () => { - const root = ReactTestRenderer.create(<Message mode="info">Hello!</Message>) - .root; - - const p = root.findByType("p"); - - expect(p.props["role"]).to.equal("status"); - expect(p.children).to.deep.equal(["Hello!"]); - }); - - it("renders an error message", () => { - const root = ReactTestRenderer.create( - <Message mode="error">Hello!</Message> - ).root; - - const p = root.findByType("p"); - - expect(p.props["role"]).to.equal("alert"); - expect(p.children).to.deep.equal(["Hello!"]); - }); -}); diff --git a/test/console/reducers/console.test.ts b/test/console/reducers/console.test.ts deleted file mode 100644 index 64e8eb3..0000000 --- a/test/console/reducers/console.test.ts +++ /dev/null @@ -1,164 +0,0 @@ -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, {} 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 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_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_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 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({ ...initialState, mode: "error" }, action); - expect(state).to.have.property("mode", "error"); - }); - - 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(undefined, action); - - expect(state).to.have.property("consoleText", "hello world"); - }); - - it("return next state for CONSOLE_SET_COMPLETIONS", () => { - const initialState = reducer(undefined, {} as any); - let state: State = { - ...initialState, - select: 0, - completions: [], - }; - const action: actions.ConsoleAction = { - type: actions.CONSOLE_SET_COMPLETIONS, - 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); - }); - - 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: [{}, {}], - }, - { - name: "Banana", - items: [{}], - }, - ], - }; - - state = reducer(state, action); - expect(state).to.have.property("select", 0); - - state = reducer(state, action); - expect(state).to.have.property("select", 1); - - state = reducer(state, action); - expect(state).to.have.property("select", 2); - - state = reducer(state, action); - expect(state).to.have.property("select", -1); - }); - - 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: [{}, {}], - }, - { - name: "Banana", - items: [{}], - }, - ], - }; - - state = reducer(state, action); - expect(state).to.have.property("select", 2); - - state = reducer(state, action); - expect(state).to.have.property("select", 1); - - state = reducer(state, action); - expect(state).to.have.property("select", 0); - - state = reducer(state, action); - expect(state).to.have.property("select", -1); - }); -}); |