diff --git a/src/createContext.ts b/src/createContext.ts index 32a37a27..dc2bc2ff 100644 --- a/src/createContext.ts +++ b/src/createContext.ts @@ -17,7 +17,6 @@ export function createContextWithValue( return React.createContext>({ dispatch: (a) => a, getState: () => value.state, - reducer: (s) => s, ...value, } as ContextValue); } diff --git a/tests/utils.test.ts b/tests/utils.test.ts new file mode 100644 index 00000000..2a3bf9be --- /dev/null +++ b/tests/utils.test.ts @@ -0,0 +1,47 @@ +import { bindActionCreators } from "src/utils/bindActionCreators"; + +describe("bindActionCreators", () => { + it("binds action creator function to dispatch", () => { + const dispatch = jest.fn(); + const action = { type: "ACTION_TYPE", payload: "Some payload" }; + const boundActionCreator = bindActionCreators(() => action, dispatch); + expect(boundActionCreator()).toBeUndefined(); + expect(dispatch).toHaveBeenCalledTimes(1); + expect(dispatch).toHaveBeenCalledWith(action); + }); + + it("binds action creator mapping to dispatch", () => { + const dispatch = jest.fn(); + const action = { type: "ACTION_TYPE", payload: "Some payload" }; + const boundActionCreators = bindActionCreators( + ({ + action1: () => action, + action2: null, + action3: undefined, + action4: "non function", + } as unknown) as ActionCreatorMapping, + dispatch, + ); + expect(boundActionCreators.action1()).toBeUndefined(); + expect(dispatch).toHaveBeenCalledTimes(1); + expect(dispatch).toHaveBeenCalledWith(action); + expect(boundActionCreators.action2).toBeUndefined(); + expect(boundActionCreators.action3).toBeUndefined(); + expect(boundActionCreators.action4).toBeUndefined(); + }); + + it.each` + actionCreator + ${"non function"} + ${null} + ${undefined} + `( + "does not binds unsupported action creator to dispatch", + ({ actionCreator }) => { + const dispatch = jest.fn(); + expect(() => bindActionCreators(actionCreator, dispatch)).toThrow( + "bindActionCreators expected an object or a function", + ); + }, + ); +});