测试与酶的联系

rah*_*all 3 reactjs enzyme react-router-v4

我试图用酶测试组件,我收到以下错误:

Warning: Failed context type: The context `history` is marked as required in `Link`, but its value is `undefined`.
        in Link (at UserTable.js:30)
        in button (created by Button)
        in Button (at UserTable.js:29)
        in td (at UserTable.js:28)
        in tr (at UserTable.js:24)
        in tbody (at UserTable.js:43)
        in table (created by Table)
        in Table (at UserTable.js:41)
        in UserTable (at UserTable.test.js:19)
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

import React from 'react';
import { render } from 'enzyme';
import { fromJS } from 'immutable';
import { Table } from 'react-bootstrap';
import UserTable from '../UserTable';

const users = fromJS([{
    id: 2026429,
    login: 'rahulthewall',
    avatar_url: 'https://avatars1.githubusercontent.com/u/2026429?v=3',
    login: 'rahulthewall',
    html_url: 'https://github.com/rahulthewall',
}]);

describe('<UserTable />', () => {
    // Prepare the components
    const context = { router: { isActive: (a, b) => true } };
    const wrapper = render(
        <UserTable users={users} />,
        { context }
    );
    const table = wrapper.find('table');
    const thead = table.find('thead');
    const tbody = table.find('tbody');
    const headerRow = thead.find('tr');
    const bodyRows = tbody.find('tr');

    it('renders the table header', () => {
    expect(headerRow.length).toEqual(1);
        expect(headerRow.find('th').length).toEqual(4);
  });

    it('renders the table data', () => {
        expect(bodyRows.length).toEqual(1);
        expect(bodyRows.find('td').length).toEqual(4);
    });
});
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,当我渲染它时,如何将History上下文传递给组件?我有点迷失在这里.

小智 5

这是因为没有Router.如果用<MemoryRouter>"`` 包装组件

<MemoryRouter>
    <ComponentClass/>
</MemoryRouter>
Run Code Online (Sandbox Code Playgroud)

```

它应该解决一个问题.值得创建帮助来处理它,因为它会在所有情况下发生(至少与路由器v4相关的情况).