如何使用Jest / Enzyme测试连接的组件?

时间:2019-01-17 17:02:57

标签: javascript unit-testing redux jestjs enzyme

我没有得到100%的测试覆盖率,因为Jest说我在第67行缺少覆盖率:

export default compose(
  connect(store => ({ // LINE 67
    accountGuid: store.global.accountGuid,
    shipmentsCSV: store.shipments.shipmentsCSV,
  })),
  translate(),
)(DownloadCSVItem);

这些是我为该组件编写的测试:

import React from 'react';
import { mount } from 'enzyme';
import FileSaver from 'file-saver';
import DownloadCSVItem from '../../DownloadCSVItem';

jest.mock('file-saver', () => ({
  saveAs: jest.fn(),
}));

global.Blob = function(content, options) {
  return { content, options };
};

describe('DownloadCSVItem component', () => {
  let props;

  beforeEach(() => {
    props = {
      t: k => k,
      accountGuid: 'abc-123',
      shipmentsCSV: {
        totalCount: 0,
        shipments: [],
      },
    };
  });

  it('renders DownloadCSVItem and check if class exists', () => {
    const wrapper = mount(<DownloadCSVItem.WrappedComponent {...props} />);

    expect(wrapper.exists('.download-csv-item')).toBeTruthy();
    expect(wrapper.exists('button')).toBeTruthy();
  });

  it('triggers stateless component downloadFile function', () => {
    FileSaver.saveAs = jest.fn();

    const wrapper = mount(<DownloadCSVItem.WrappedComponent {...props} />);

    expect(wrapper.find('#download-csv-item > li > button')).toHaveLength(1);

    wrapper.find('#download-csv-item > li > button').simulate('click');

    expect(FileSaver.saveAs).toBeCalled();
  });
});

0 个答案:

没有答案