2017-12-18 193 views
0

我有這樣的代碼:在這個例子中如何測試react.js中的模擬上下文?

render() { 
    ... 

    const { 
    exampleMethod, 
    } = this.props; 

    const { getPath } = this.context; 
    const exampletData = exampleMethod(getPath()); 

    ... 
} 

我如何可以模擬,測試情境?一般來說,你如何測試上下文?

回答

2

如果您使用的是enzyme,則可以通過傳遞選項來設置淺顯示或全局渲染時的上下文。 (docs here):

如:

import { shallow, mount } from 'enzyme'; 

// ...... 

const testContext = { getPath:() => 'foo' }; 
const shallowWrapper = shallow(<SomeComponent />, { context: testContext }); 

// or 

const fullWrapper = mount(<SomeComponent />, { context: testContext }); 

如果你想測試一個節點上的背景下,你可以從enzyme

使用 .context()方法