我有一個任務來測試React組件,它依賴於BrowserRouter
組件,它位於上層的某處。據我所知BrowserRouter
傳遞路由對象到任何孩子,孫子等道具Jest Enzyme setState of memoryRouter dependent
組件:
import React, { Component } from 'react';
import Program from './Program';
export default class Programs extends Component {
constructor() {
super();
this.state = {
programs: [
/** example */
{ id: 10, name: 'Golden Knights' },
{ id: 20, name: 'Silver Lords' },
{ id: 30, name: 'Wooden Kings' },
],
};
}
render() {
const { programs } = this.state;
return (
<ul className="programs">{/*<-- add class "att-programs"*/}
{
programs.map(
program => <Program
key={program.id}
program_id={program.id}
program_name={program.name}
/>,
)
}
</ul>
);
}
}
當我嘗試獨立測試這個組件,我得到一個錯誤:Cannot read property 'route' of undefined
。要解決這個問題,我總結我的元素爲MemoryRouter
分量和我的測試看起來如下:
import React from 'react';
import renderer from 'react-test-renderer';
import { mount, shallow } from 'enzyme';
import { MemoryRouter } from 'react-router-dom';
import Programs from '../../src/components/Programs';
describe('<Programs />',() => {
it('Renders list for any child from state',() => {
const wrapper = mount(<MemoryRouter><Programs /></MemoryRouter>);
const programs = wrapper.find(Programs);
expect(programs.length).toBe(1);
programs.setState({
programs: [
{ id: 1, name: 'qwe' },
{ id: 2, name: 'asd' },
{ id: 3, name: 'zxc' },
],
});
expect(wrapper.find('li').length).toBe(3);
expect(wrapper.find('a[href="/player/1"]').length).toBe(1);
});
});
現在我有一個問題:ReactWrapper::setState() can only be called on the root
請幫我找出我怎麼嘲笑路由器,但留下能在測試組件上定義setState
?
您是否想過在其他地方維護該狀態(例如Redux)?這種方式將作爲屬性傳遞到您的組件 - > 100%可測試。 – luboskrnac