我有一个相当简单的 React 组件,它使用 i18next 实现了国际化功能。我使用的 i18next 版本是 11.9.0,我使用的 react-i18next 版本是 8.1.0。组件(和规格)看起来像这样:
TestComponent.component.js
import React, { Component } from "react";
import { I18n } from "react-i18next";
export class TestComponent extends Component {
render() {
return (
<I18n ns="translations">
{ t => (
<p>
{t('test')}
</p>
)}
</I18n>
);
}
}
export default TestComponent;
TestComponent.component.spec.js
import React from "react";
import { I18n } from "react-i18next";
import { shallow } from "enzyme";
import TestComponent from './TestComponent.component';
describe('TestComponent', () => {
describe('Snapshot Test', () => {
it('it matches snapshot', () => {
const wrapper = shallow(<TestComponent />);
wrapper.instance().render();
expect(wrapper.instance()).toMatchSnapshot();
});
});
});
我正在尝试使用 Jest Snapshots 测试这个组件,但是当我检查这个组件的代码覆盖率时,它表明覆盖率检查器没有达到 t 函数。
我想这可以通过以某种方式模拟 I18n 来解决,但是我在网上找到的所有例子都没有解决我的问题。
任何人都可以提供一些关于为什么覆盖检查器没有达到 t 函数的见解吗?为了提高我对这样一个组件的覆盖率,需要做什么?