[英]Jest tests with i18next and storybook
我在组件存储库中使用i18next。 组件工作正常,但测试有问题。 我将i18next与hoc一起使用,当我仅导出组件时,测试通过了,但是当我导出时,如导出默认转换(“ components”)(列表); 测试失败。 我尝试使用hoc和不使用hoc进行两次导出,但是在其他组件中使用了某些组件,而没有hoc则无法导入。 我的体系结构看起来像:带有I18nextProvider的Root组件,我已经用这个组件包装了故事中的每个组件,它是我的主应用程序中的主要组件。 看起来像这样:
const Root = ({ component, i18 }) => (
<I18nextProvider i18n={i18}>
<div className={bindClasses('wrapper')}>
{component}
</div>
</I18nextProvider>
);
示例组件:
import React, { Component } from 'react';
import { translate } from 'react-i18next';
import classNameBind from 'classnames/bind';
import styles from './List.css';
import Icon from '../Icon';
import uuid from '../../utils/methods/uuid';
class List extends Component {
constructor(props) {
super(props);
this.state = {
items: props.items,
};
}
renderNoItems() {
const { items, t } = this.props;
return items.length === 0 ?
t('noMatchingFoundLabel') : '';
}
renderItems() {
const { onItemSelected } = this.props;
const { items } = this.state;
return items
.map(element =>
(
<li key={uuid()}>
<a role="button" tabIndex={0} onClick={() => onItemSelected(element)}>
{ element.icon ? <Icon {...element.icon} /> : '' }
{element.name}
</a>
</li>
),
);
}
render() {
const { className } = this.props;
const bindClasses = classNameBind.bind(styles);
return (
<nav
ref={(scrollable) => { this.scrollable = scrollable; }}
className={bindClasses('wrapper', className)}
onScroll={this.handleScroll}
>
<ul>
{this.renderItems()}
</ul>
</nav>
);
}
}
export default translate('components')(List);
每个组件都有index.js文件,只是带有导出默认值。
故事中的用法:
<Root i18={i18}>
<List />
</Root>
我不确定在这里。 我应该将t函数传递给List组件吗? 没有它就可以工作,但也许我应该。 并测试:
import React from 'react';
import List from '../List';
import { shallow, mount } from 'enzyme';
import Icon from "../../Icon";
import TextField from "../../TextField";
describe("List component", () => {
const defaultPropsWithIcons = {
items: [
{ name: 'Item1', icon: { name: 'upload', color: 'default' }, options: { some: 'options1'} },
{ name: 'Item2', icon: { name: 'upload', color: 'default' }, options: { some: 'options2'} },
],
t: jest.fn(),
};
test("should render List component with passed items with icons", () => {
const wrapper = shallow(<List {...defaultPropsWithIcons} />);
const actionList = wrapper.find('.wrapper');
expect(list.find('ul').children().length).toBe(2);
expect(list.find('ul').children().at(0).find('a').exists());
expect(list.find('ul').children().at(0).find(Icon).exists()).toBe(true);
});
在正常导出组件的情况下,它可以正常工作,而在翻译过程中,则失败。 我尝试使用
jest.mock('react-i18next', () => ({
receive the t function as a prop
translate: () => List => props => <List t={() => ''} {...props} />,
}));
要么
const wrapper = shallow(<List {...defaultPropsWithIcons} t={key => key} />);
但这没有帮助,仍然在第一期望时出现错误(预期为2,收到0)有人知道如何测试它吗? 还是我在使用i18next时出错? 在我使用List组件在另一个组件中或仅在i18next的其他组件中使用的每个测试中,都遇到了相同的问题。
问候
您不应该测试包装在HOC中的组件,这仅仅是因为单元测试应该测试代码的各个部分。
在计算机编程中,单元测试是一种软件测试方法,通过该方法可以对源代码的各个单元进行操作。 https://zh.wikipedia.org/wiki/Unit_testing
尽量避免开玩笑地使用{mount},我怀疑使用它时会遇到麻烦,因为它会呈现子组件。
如果您想进行集成测试并渲染ne = sted组件,则可以像下面这样使用模拟:
import { translate } from 'react-i18next';
jest.mock('react-i18next', () => ({
translate: () => List => props => <List t={() => ''} {...props} />,
}));
更新:
如果您不喜欢将导入标记为未使用,则可以测试翻译组件:
import { translate } from 'react-i18next';
jest.mock('react-i18next', () => ({
translate: jest.fn(() => List => props => <List t={() => ''} {...props} />),
}));
test('should test translation', () => {
expect(translate).toHaveBeenCalled();
expect(translate).toHaveBeenCalledWith('components');
});
希望对您有所帮助,编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.