diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-04-12 13:09:09 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-12 13:09:09 +0000 |
commit | d80d0f87b82ba4bd74ed9b2bb7354421a28a11b3 (patch) | |
tree | 691185ad88418d0f44c236d0913cf5c425b29b23 /test/console/completion/reducer.test.ts | |
parent | ea73c900f66107fd4a5b2f3b05080bcf643c94ea (diff) | |
parent | 8a5bba1da639355a25da8c279a9f1cf0a7300a9f (diff) |
Merge pull request #1098 from ueokande/replace-redux-with-react-hooks
Refactor state management with React Hooks on Console
Diffstat (limited to 'test/console/completion/reducer.test.ts')
-rw-r--r-- | test/console/completion/reducer.test.ts | 168 |
1 files changed, 168 insertions, 0 deletions
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); + }); + }); + }); +}); |