如何通过玩笑来测试中间件?

时间:2018-12-20 11:04:35

标签: javascript reactjs jestjs enzyme

我需要向中间件添加onMount和onUnmount函数,并测试它们是否正确。我该怎么办?我可以开玩笑,酶

import { set, open } from "./";
export function middleware(
    options,
) {
    return ({ dispatch }) => (next) => {
        return (action) => {
            const onMount = () => {
                if (options) {
                    options.mount();
                }
                dispatch(set(true));
            };
            const onUnmount = () => {
                if (legacyOptions) {
                    options.unmount();
                }
                dispatch(set(false));
            };
            open({
                onMount: onMount,
                onUnmount: onUnmount,
            )}
        }

    }
    return next(action);
}

我的测试文件。我创建了这个中间件。如何测试此功能?

const openMock = jest.fn();
jest.mock("./", () => ({
    open: openMock,
}));

const { next, store } = create();
const invoke = (action) => middleware()(store)(next)(action);
invoke(init());
expect(openMock).toHaveBeenCalledTimes(1);

0 个答案:

没有答案