Skip to content

Instantly share code, notes, and snippets.

@perjo927
Last active September 8, 2020 07:27
Show Gist options
  • Save perjo927/0bb20993db65051145ad1dd9cdcc03f2 to your computer and use it in GitHub Desktop.
Save perjo927/0bb20993db65051145ad1dd9cdcc03f2 to your computer and use it in GitHub Desktop.
Test dispatch
import { makeDispatcher } from "../src/redux/store/dispatch.js";
import assert from "assert";
describe("dispatch.js", () => {
describe("makeDispatcher", () => {
it("generates a function from state handlers, a reducer and a callback", () => {
const stateHandlers = {
getState() {},
setState(arg) {},
};
const onDispatch = () => {};
const reducer = () => {};
const dispatcher = makeDispatcher(stateHandlers, reducer, onDispatch);
assert.equal(dispatcher.hasOwnProperty("dispatch"), true);
});
it("returns the action provided", () => {
const stateHandlers = {
getState() {},
setState(arg) {},
};
const onDispatch = () => {};
const reducer = () => {};
const action = { type: "FOO", value: "FOO" };
const { dispatch } = makeDispatcher(
stateHandlers,
reducer,
onDispatch
);
const expected = action;
const actual = dispatch(action);
assert.equal(actual, expected);
});
it("calls setState with the newState", () => {
const spyOnSetState = { arg: "" };
const stateHandlers = {
getState() {
return { state: "BAR" };
},
setState(arg) {
spyOnSetState.arg = arg;
},
};
const onDispatch = () => {};
const reducer = (state, action) => action.type + state.state;
const action = { type: "FOO", value: "FOO" };
const { dispatch } = makeDispatcher(
stateHandlers,
reducer,
onDispatch
);
dispatch(action);
const expected = "FOOBAR";
const actual = spyOnSetState.arg;
assert.equal(actual, expected);
});
it("calls onDispatch", () => {
const spyOnDispatch = { called: false };
const stateHandlers = {
getState() {},
setState(arg) {},
};
const onDispatch = () => {
spyOnDispatch.called = true;
};
const reducer = () => {};
const action = {};
const { dispatch } = makeDispatcher(
stateHandlers,
reducer,
onDispatch
);
dispatch(action);
const expected = true;
const actual = spyOnDispatch.called;
assert.equal(actual, expected);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment