diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-04-15 19:35:15 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-05-02 12:55:42 +0900 |
commit | b0fe06bc2e739cc252a559f666da65b27769959d (patch) | |
tree | 4fe9d1c6378b87082b14ca6d1b7ca2737df94aac /test/console | |
parent | 20d40d8017284f80876bde8e28cbde47c3651886 (diff) |
Fix types in tests
Diffstat (limited to 'test/console')
-rw-r--r-- | test/console/actions/console.test.ts | 8 | ||||
-rw-r--r-- | test/console/components/console/Completion.test.tsx | 50 | ||||
-rw-r--r-- | test/console/reducers/console.test.ts | 76 |
3 files changed, 88 insertions, 46 deletions
diff --git a/test/console/actions/console.test.ts b/test/console/actions/console.test.ts index 5a531a6..3169c4b 100644 --- a/test/console/actions/console.test.ts +++ b/test/console/actions/console.test.ts @@ -2,7 +2,15 @@ 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", () => { + beforeEach(() => { + (global as any).browser = browserFake(); + }); + describe("hide", () => { it("create CONSOLE_HIDE action", () => { const action = consoleActions.hide(); diff --git a/test/console/components/console/Completion.test.tsx b/test/console/components/console/Completion.test.tsx index 200bb1a..921720b 100644 --- a/test/console/components/console/Completion.test.tsx +++ b/test/console/components/console/Completion.test.tsx @@ -1,6 +1,9 @@ import React from "react"; -import Completion from "console/components/console/Completion"; -import ReactTestRenderer from "react-test-renderer"; +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 = [ @@ -24,13 +27,22 @@ describe("console/components/console/completion", () => { it("renders Completion component", () => { const root = ReactTestRenderer.create( - <Completion completions={completions} size={30} /> + <Completion completions={completions} size={30} select={-1} /> ).root; - expect(root.children).to.have.lengthOf(1); - - const children = root.children[0].children; + // 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.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"); @@ -46,7 +58,7 @@ describe("console/components/console/completion", () => { <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; }); @@ -55,10 +67,8 @@ describe("console/components/console/completion", () => { <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; - } + const children = root.findByType("ul").findAllByType(CompletionItem); + expect(children.every((e) => e.props.highlight === false)).to.be.true; }); it("limits completion items", () => { @@ -66,7 +76,7 @@ describe("console/components/console/completion", () => { <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"); @@ -77,7 +87,7 @@ describe("console/components/console/completion", () => { <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; }); @@ -87,7 +97,7 @@ describe("console/components/console/completion", () => { ); 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"); @@ -97,7 +107,7 @@ describe("console/components/console/completion", () => { <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"); @@ -108,7 +118,7 @@ describe("console/components/console/completion", () => { <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"); @@ -122,7 +132,7 @@ describe("console/components/console/completion", () => { ); 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"); @@ -132,21 +142,21 @@ describe("console/components/console/completion", () => { <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} /> ); - 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} /> ); - children = root.children[0].children; + 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"); diff --git a/test/console/reducers/console.test.ts b/test/console/reducers/console.test.ts index e8432c5..64e8eb3 100644 --- a/test/console/reducers/console.test.ts +++ b/test/console/reducers/console.test.ts @@ -1,9 +1,12 @@ -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, {}); + const state = reducer(undefined, {} as any); expect(state).to.have.property("mode", ""); expect(state).to.have.property("messageText", ""); expect(state).to.have.property("consoleText", ""); @@ -12,68 +15,85 @@ describe("console reducer", () => { }); it("return next state for CONSOLE_HIDE", () => { - const action = { type: actions.CONSOLE_HIDE }; - const state = reducer({ mode: "error" }, action); + 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 = { type: actions.CONSOLE_SHOW_COMMAND, text: "open " }; - const state = reducer({}, action); + 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 = { type: actions.CONSOLE_SHOW_INFO, text: "an info" }; - const state = reducer({}, action); + 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 = { type: actions.CONSOLE_SHOW_ERROR, text: "an error" }; - const state = reducer({}, action); + 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); + 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); + state = reducer({ ...initialState, mode: "error" }, action); expect(state).to.have.property("mode", "error"); }); it("return next state for CONSOLE_SET_CONSOLE_TEXT", () => { - const action = { + const action: actions.ConsoleAction = { type: actions.CONSOLE_SET_CONSOLE_TEXT, consoleText: "hello world", }; - const state = reducer({}, action); + const state = reducer(undefined, action); expect(state).to.have.property("consoleText", "hello world"); }); it("return next state for CONSOLE_SET_COMPLETIONS", () => { - let state = { + 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], + items: [{}, {}, {}], }, { name: "Banana", - items: [4, 5, 6], + items: [{}, {}, {}], }, ], + completionSource: "", }; state = reducer(state, action); expect(state).to.have.property("completions", action.completions); @@ -81,17 +101,19 @@ describe("console reducer", () => { }); it("return next state for CONSOLE_COMPLETION_NEXT", () => { - const action = { type: actions.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], + items: [{}, {}], }, { name: "Banana", - items: [3], + items: [{}], }, ], }; @@ -110,17 +132,19 @@ describe("console reducer", () => { }); it("return next state for CONSOLE_COMPLETION_PREV", () => { - const action = { type: actions.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], + items: [{}, {}], }, { name: "Banana", - items: [3], + items: [{}], }, ], }; |