From 8a5bba1da639355a25da8c279a9f1cf0a7300a9f Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 11 Apr 2021 22:30:41 +0900 Subject: Replace app state with Custom Hooks --- test/console/actions/console.test.ts | 62 ------------------------- test/console/app/actions.test.ts | 62 +++++++++++++++++++++++++ test/console/app/reducer.test.ts | 85 +++++++++++++++++++++++++++++++++++ test/console/reducers/console.test.ts | 68 ---------------------------- 4 files changed, 147 insertions(+), 130 deletions(-) delete mode 100644 test/console/actions/console.test.ts create mode 100644 test/console/app/actions.test.ts create mode 100644 test/console/app/reducer.test.ts delete mode 100644 test/console/reducers/console.test.ts (limited to 'test') diff --git a/test/console/actions/console.test.ts b/test/console/actions/console.test.ts deleted file mode 100644 index 736dd54..0000000 --- a/test/console/actions/console.test.ts +++ /dev/null @@ -1,62 +0,0 @@ -import * as consoleActions from "../../../src/console/actions/console"; -import { - CONSOLE_HIDE, - CONSOLE_HIDE_COMMAND, - CONSOLE_SHOW_COMMAND, - CONSOLE_SHOW_ERROR, - CONSOLE_SHOW_FIND, - CONSOLE_SHOW_INFO, -} 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(CONSOLE_HIDE); - }); - }); - describe("showCommand", () => { - it("create CONSOLE_SHOW_COMMAND action", async () => { - const action = await consoleActions.showCommand("hello"); - expect(action.type).to.equal(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(CONSOLE_SHOW_FIND); - }); - }); - - describe("showError", () => { - it("create CONSOLE_SHOW_ERROR action", () => { - const action = consoleActions.showError("an error"); - expect(action.type).to.equal(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(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(CONSOLE_HIDE_COMMAND); - }); - }); -}); 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/reducers/console.test.ts b/test/console/reducers/console.test.ts deleted file mode 100644 index 390dc66..0000000 --- a/test/console/reducers/console.test.ts +++ /dev/null @@ -1,68 +0,0 @@ -import reducer from "../../../src/console/reducers/console"; -import { expect } from "chai"; -import { - CONSOLE_HIDE, - CONSOLE_HIDE_COMMAND, - CONSOLE_SHOW_COMMAND, - CONSOLE_SHOW_ERROR, - CONSOLE_SHOW_INFO, - ConsoleAction, -} from "../../../src/console/actions/console"; - -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", ""); - }); - - it("return next state for CONSOLE_HIDE", () => { - const initialState = reducer(undefined, {} as any); - const action: ConsoleAction = { type: 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: ConsoleAction = { - type: CONSOLE_SHOW_COMMAND, - 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: ConsoleAction = { - type: 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: ConsoleAction = { - type: 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: ConsoleAction = { - type: 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"); - }); -}); -- cgit v1.2.3