2017-03-06 2 views
2

Ich versuche, eine Komponente mit Enzym zu testen und ich erhalte den folgenden Fehler:Testing Verbindung mit Enzym

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) 

Hier ist mein Test:

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); 
    }); 
}); 

So ist die Frage, die ich habe, ist, wie Übergeben Sie den Verlaufskontext an die Komponente, wenn ich sie rendere? Ich bin hier irgendwie verloren.

Antwort

3

Es wird verursacht durch Router nicht zu haben. Wenn Sie Ihre Komponente mit <MemoryRouter> wie `` `

<MemoryRouter> 
    <ComponentClass/> 
</MemoryRouter> 

` ``

wickeln sollte es ein Problem beheben. Es lohnt sich, helper zu erstellen, da dies in all Ihren Fällen passieren wird (zumindest in denen, die mit Router v4 zusammenhängen).