简体   繁体   English

来自 HOC 的模拟道具对快照测试做出反应

[英]Mocking props from HOC in react for snapshot tests

I am trying to appropriately mock props for a simple sign out button component.我正在尝试为一个简单的注销按钮组件适当地模拟道具。 The only prop in this component is the firebase prop which is coming from a Higher Order Component (HOC) called withFirebase .这个组件中唯一的道具是firebase道具,它来自一个名为withFirebase高阶组件 (HOC)。

import React from 'react';
import { withFirebase } from '../Firebase';

const SignOutButton = ({ firebase }) => (
  <button type="button" onClick={firebase.doSignOut}>
    Sign Out
  </button>
);

export default withFirebase(SignOutButton);
export { SignOutButton as SignOutButtonTest };

My thought was that I could export the functional component without the HOC and mock the firebase prop directly and pass that to the component for the snapshot test.我的想法是我可以在没有 HOC 的情况下导出功能组件并直接模拟firebase道具并将其传递给组件进行快照测试。

import React from 'react';
import renderer from 'react-test-renderer';
import SignOutButtonTest from '../SignOutButton';

describe('SignOutButton Tests', () => {
    it('renders with some data', () => {
        const props = {
            firebase: {
                doSignOut: () => {},
            }
        };
        const tree = renderer.create(
            <SignOutButtonTest {...props} />
        ).toJSON();
        expect(tree).toMatchSnapshot();
    });
});

However, when I run this test I get the following TypeError .但是,当我运行此测试时,我得到以下TypeError Any thoughts on how to properly mock this firebase prop for a snapshot test?关于如何正确模拟此firebase道具以进行快照测试的任何想法?

  ● SignOutButton Tests › renders with some data

    TypeError: Cannot read property 'doSignOut' of null

      3 | 
      4 | const SignOutButton = ({ firebase }) => (
    > 5 |   <button type="button" onClick={firebase.doSignOut}>
        |                                           ^
      6 |     Sign Out
      7 |   </button>
      8 | );

Use named export instead of default export.使用命名导出而不是默认导出。 Here:这里:

import SignOutButtonTest from '../SignOutButton';

you are still importing HOC'ed version.您仍在导入 HOC 版本。

It should be:它应该是:

import { SignOutButtonTest } from '../SignOutButton';

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

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