use*_*113 14 reactjs jestjs react-native redux-persist
我有以下代码:
const App = () => {
return (
<Provider store={store}>
<PersistGate persistor={persistor} loading={<Text>Loading!</Text>}>
<ConnectedRootComponent />
</PersistGate>
</Provider>
);
};
export default App;
Run Code Online (Sandbox Code Playgroud)
它使用redux-persist来补充状态,在此之前,它将显示加载属性中的内容.我有一个Jest测试(只是默认的一个带有反应原生的开箱即用):
it('renders without crashing', () => {
const rendered = renderer.create(<App />).toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)
但是虽然测试通过,但我发现仍然发生的动作,persist/PERSIST以及persist/REHYDRATE测试中打印到控制台的值(渲染输出)是:
{
"type": "Text",
"props": {
"accessible": true,
"allowFontScaling": true,
"ellipsizeMode": "tail"
},
"children": ["Loading!"]
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是等到redux-persist完成水合,然后检查渲染值.我怎样才能做到这一点?
您可以模拟PersistGate始终返回props.children,然后您的测试可以检查渲染的值。
创建模拟的方法如下:
jest.mock('redux-persist/integration/react', () => ({
PersistGate: props => props.children,
}))
Run Code Online (Sandbox Code Playgroud)
学分。
| 归档时间: |
|
| 查看次数: |
2049 次 |
| 最近记录: |