Skip to content

Commit

Permalink
test: bindActionCreators
Browse files Browse the repository at this point in the history
  • Loading branch information
iamogbz committed Sep 15, 2020
1 parent ea41eaa commit e374ada
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function createContextWithValue<S, T extends string, P>(
return React.createContext<ContextValue<S, T, P>>({
dispatch: (a) => a,
getState: () => value.state,
reducer: (s) => s,
...value,
} as ContextValue<S, T, P>);
}
Expand Down
47 changes: 47 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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",
);
},
);
});

0 comments on commit e374ada

Please sign in to comment.