如果您查看测试2和3,那是设置和测试我们期望的正确方法?
这里是否有性能问题,内存泄漏或“不良的处理方式”?
import { shallow } from 'enzyme';
import Component from 'path/to/my/component';
describe('<Component />', () => {
const defaultProps = {...};
const setup = (props = {}) => shallow(<Component {...defaultProps, ...props} />);
let wrapper;
describe('tests ...', () => {
beforeEach(() => { wrapper = setup(); });
// 1 - Use the wrapper without additional props
it('should ...', () => {
expect(wrapper.find('selector')).toHaveLength(1);
});
// 2 - Use setup with additional props directly inside an expect
it('should ...', () => {
expect(setup({ bar: 'bar' }).find('selector')).toHaveLength(0);
});
// 3 - Override wrapper with additional props and use it inside an expect
it('should ...', () => {
wrapper = setup({ bar: 'bar' });
expect(wrapper.find('selector')).toHaveLength(0);
});
// more tests..
});
});
如果您进行这种类型的测试,则可能首先不需要使用beforeEach。在2的情况下,不使用beforeEach创建的包装器,至少应该添加afterEach初始化包装器的动作。