You are viewing the documentation for an older version of Preact. Switch to the current version.
使用Enzyme来编写单元测试
React提供了react-addons-test-utils
来测试组件,而Airbnb开发的enzyme
则让这个更进一步,提供了多种渲染模式和其他有用的特性。多亏了preact-compat-enzyme
这个模块,在preact-compat
基础之上实现了必要的React的内部属性,这让使用enzyme
来测试preact的组件成为可能。
安装
我们需要下面的两个包:
preact-compat-enzyme
: 提供enzyme
所需要的React的内部属性。
preact-test-utils
: 提供了enzyme
所需要的react-addons-test-utils
中的部分功能
npm install --save-dev preact-compat-enzyme preact-test-utils
配置
使用karma来进行测试服务的启动。我们需要在karma
的webpack aliases
的加入下面的alias。
{
"resolve": {
"alias": {
"react-dom/server": "preact-render-to-string",
"react-addons-test-utils": "preact-test-utils",
"react": "preact-compat-enzyme",
"react-dom": "preact-compat-enzyme"
}
}
}
限制
- 目前使用
enzyme
仅仅支持mount
模式。 - 在执行
React Wrapper
的setProps
或者setState
方法时候,需要使用setTimeout
方法对断言进行包裹。
示例
let dataSource = [{ id: '1', name: 'test-content' }, { id: '2', name: 'test-content' }],
table,
wrapper;
beforeEach(() => {
table = <Table dataSource={dataSource}>
<Table.Column dataIndex='id' />
<Table.Column dataIndex='name' />
</Table>
wrapper = mount(table);
})
afterEach(() => {
table = null;
})
it('should render checkboxMode', (done) => {
wrapper.setProps({
rowSelection: {
getProps: (record) => {
if (record.id === '1') {
return {
disabled: true
}
}
}
}
});
setTimeout(() => {
expect(wrapper.find('.checkbox').length).to.be.equal(3);
expect(wrapper.find('.checkbox.disabled').length).to.be.equal(1);
done();
}, 10);
});