Skip to content

Commit

Permalink
test: context provider consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
iamogbz committed Sep 15, 2020
1 parent fed275f commit 7b7f789
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tests/createContext.test.ts → tests/createContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from "react";
import { act, render } from "@testing-library/react";
import { renderHook } from "@testing-library/react-hooks";
import { applyMiddleware, createContext } from "src";
import { Provider, applyMiddleware, createContext } from "src";
import { SymbolObservable } from "src/utils/symbolObservable";

describe("createContext", () => {
Expand All @@ -23,6 +24,31 @@ describe("createContext", () => {
expect(result.current.getState()).toStrictEqual(initialState);
});

it("passes context value to consumer", () => {
const initialState = {};
const reducer = jest.fn((s) => s);
const Context = createContext(reducer, initialState);
const { result } = renderHook(() => React.useContext(Context));
const contextValue = result.current;
const spyConsume = jest.fn(() => null);
render(
<Provider Context={Context}>
<Context.Consumer>{spyConsume}</Context.Consumer>
</Provider>,
);
expect(spyConsume).toHaveBeenCalledTimes(1);
const [consumeValue] = (spyConsume.mock.calls[0] as unknown) as [
ContextValue,
];
expect(consumeValue.enhancer).toBeUndefined();
expect(consumeValue.getState()).toStrictEqual(contextValue.state);
expect(reducer).toHaveBeenCalledTimes(1);
const action = { type: "ACTION_TYPE" };
act(() => void consumeValue.dispatch(action));
expect(reducer).toHaveBeenCalledTimes(2);
expect(reducer).toHaveBeenLastCalledWith(consumeValue.state, action);
});

it("provides and can use empty enhancer from applyMiddleware", () => {
const initialState = {};
const Context = createContext(
Expand Down

0 comments on commit 7b7f789

Please sign in to comment.