我尝试比较两个框架(Jest 和 Mocha)。
此测试与 Jest 一起使用,但我必须以某种方式将其转换为 Mocha 和 Chai,但我无法完全转换此 Jest 功能,我尝试使用 sinon 但也没有运气,任何帮助都值得赞赏。
index.test.js
import React from "react";
import {configure, shallow} from "enzyme";
import {expect} from "chai";
import Adapter from "enzyme-adapter-react-16";
import { CollectionItem } from "../components/collection-item/collection-item.component";
configure({
adapter: new Adapter()
});
describe('CollectionItem component', () => {
let wrapper;
let mockAddItem;
const imageUrl = 'www.testImage.com';
const mockName = 'black hat';
const mockPrice = "10";
beforeEach(() => {
const mockAddItem = jest.fn();
const mockProps = {
item: {
imageUrl: imageUrl,
price: mockPrice,
name: mockName
},
addItem: mockAddItem
};
wrapper = shallow(<CollectionItem {...mockProps} />);
});
it('should call addItem when AddButton clicked', () => {
wrapper.find('CustomButton').simulate('click');
expect(mockAddItem).to.have.beenCalled();
});
});