我没有得到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();
});
});