gpt4 book ai didi

javascript - 使用 i18next 和 Storybook 进行 Jest 测试

转载 作者:行者123 更新时间:2023-11-28 03:42:10 25 4
gpt4 key购买 nike

我在我的组件存储库中使用 i18next。组件工作正常,但我在测试时遇到问题。我将 i18next 与 hoc 一起使用,当我仅导出组件时,测试通过,但是当我像导出默认翻译('组件')(列表)一样导出它时;测试失败。我尝试制作两个导出,有临时的和没有临时的,但有些组件在另一个中使用,我无法在没有临时的情况下导入它。我的架构如下:带有 I18nextProvider 的根组件,我已通过该组件将故事中的每个组件包装起来,它是我的主应用程序中的主要组件。看起来就像这样:

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 的其他组件中使用 List 组件的每个测试中都遇到同样的问题。

问候

最佳答案

您不应该测试包含在 HOC 中的组件,因为单元测试应该测试代码的各个部分。

In computer programming, unit testing is a software testing method by which individual units of source code. https://en.wikipedia.org/wiki/Unit_testing

尽量避免 Jest 使用 { 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');
});

希望它能有所帮助,编码愉快!

关于javascript - 使用 i18next 和 Storybook 进行 Jest 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48847341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com