简体   繁体   English

如果调用日志,我如何进行单元测试

[英]How I can unit test if log is called

I have such component. 我有这样的组件。 It is a wrapper component for another component. 它是另一个组件的包装组件。 There is onClick function, which should call the log if is mouse event 有onClick函数,如果是鼠标事件,应调用日志

import log from './log';

export function withPressedLog(
    Component,
    options,
) {
    class WithPressedLogComponent extends React.Component {
        constructor(props) {
            super(props);
            this.onClick = this.onClick.bind(this);
        }

        public render() {
            const { ...props } = this.props;
            return <Component {...props} onClick={this.onClick} />;
        }

        private onClick(e) {
            if (this.props.onClick !== undefined) {
                this.props.onClick(e);
            }

            if (e) {
                this.props.log();
            }
        }
    }

    const mapDispatchToProps = {
        log: () => log(options),
    };

    return connect(
        undefined,
        mapDispatchToProps,
    )(WithPressedLogComponent);
}

I need to test is it called this.props.log. 我需要测试它是否叫做this.props.log。 I have a unit test, but it not works. 我有一个单元测试,但它不起作用。 How I can do it using jest, enzyme? 我如何使用jest,酶来做到这一点?

it("should not log if has not mouse event", () => {
    const onClickMock = jest.fn();
    const logMock = jest.fn();
    const ButtonWithLog = withPressedLog(Button, {
        type: "BUTTON_PRESSED",
    });

    const subject = mountProvider(ButtonWithLog, { onClick: onClickMock, log: logMock });

    const mockedEvent = { target:{} };
    subject.find(ButtonWithLog).simulate("click", mockedEvent);

    expect(onClickMock.mock.calls).toHaveLength(1);
    expect(logMock.mock.calls).toHaveLength(0); // not works correctly, always return []

});

store 商店

const store = createStore(() => ({}));
const dispatchMock = jest.fn();
store.dispatch = dispatchMock;

mountProvider function mountProvider函数

function mountProvider(
    Component,
    props,
) {
    return mount(
        <Provider store={store}>
            <Component {...props} />
        </Provider>,
    );
}

I think the problem here is that you are actually testing the connected component, not the unwrapped component. 我认为这里的问题是你实际上在测试连接的组件,而不是未包装的组件。

Try to isolate the components you are testing more. 尝试隔离您正在测试的组件。 For example, you can use enzyme's shallow wrapper and the dive method on the connected component, to directly get to the unwrapped component. 例如,您可以在连接的组件上使用酶的浅包装和dive方法,直接进入未包装的组件。

Specifically, your problem could be that your connected component is getting the log prop from the store (through mapDispatchToProps ), but the store is mocked, so it does not work. 具体来说,您的问题可能是您的连接组件从商店获取log道具(通过mapDispatchToProps ),但商店被mapDispatchToProps ,因此它不起作用。 In your test, you pass some mock function as prop to the component, but the reference gets lost once the components connects. 在测试中,您将一些模拟函数作为prop传递给组件,但是一旦组件连接,引用就会丢失。

helpful thread on github github上有用的线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM