笑话/酶| Ovverride defaultProps,用于特定测试

时间:2019-02-28 13:16:38

标签: javascript reactjs jestjs enzyme

我有一个呈现按钮的组件。我想测试一下该按钮单击是否将状态设置为true,反之亦然,以及是否正确显示了模态。

我有一些defaultProps,不允许我要渲染的组件渲染。如果securityMode'',则它应正确地不呈现组件。如果处于NORMAL模式,则应呈现具有所需按钮的组件。问题是这样的:

如何针对特定测试覆盖defaultProps以便呈现按钮?

这是测试代码:

const defaultProps = {
  securityMode: '',
  listUsers: () => null
};

describe('UserOverviewScreen', () => {
  let wrapper;
  let listUsers;
  let openCreateUserModal;
  let closeCreateUserModal;
  beforeEach(() => {
    listUsers = jest.fn();
    notify = jest.fn();
    openCreateUserModal = jest.fn();
    closeCreateUserModal = jest.fn();

    wrapper = shallow(
      <UserOverviewScreen
        {...defaultProps}
        openCreateUserModal={openCreateUserModal}
        closeCreateUserModal={closeCreateUserModal}
        notify={notify}
        listUsers={listUsers}
      />,
      {
        disableLifecycleMethods: false
      }
    );
  });

以上运行beforeEach()。下面是失败的测试:

    it('UserOverviewScreen, calls listUsers props function', () => {
      const response = {
        username: 'username',
        group: 'group'
      };

      console.log(wrapper.debug());
      wrapper.instance().onCreateUserSuccess(response);
      expect(listUsers).toHaveBeenCalled();
    });

    it('UserOverviewScreen, call openCreateUserModal when New User is clicked', () => {
      const button = wrapper.find('#new-user-button');
      const event = button.simulate('click');
      expect(event).toBeCalled();
      expect(event.state('isCreateUserModalOpen')).toBe(true);
    });

    it('UserOverviewScreen, call closeCreateUserModal when New User is clicked', () => {
      expect(wrapper.state('isCreateUserModalOpen')).toBe(false);
    });

有人可以帮助我更正这两项测试并撰写另一项吗?

0 个答案:

没有答案