mardi 4 juin 2019

react-test-renderer: how to inject context?

My react Component states depend on its context. For normal testing:

My test case likes:

const wrapper= shallow(<Storage />,{context: {valley: valley}});
expect(wrapper.is('Fragment')).toBeTruthy();

The context works, But I want to apply react-test-renderer like react document https://react.docschina.org/docs/shallow-renderer.html:

import ShallowRenderer from 'react-test-renderer/shallow';

const renderer = new ShallowRenderer();
renderer.render(<Storage />, {context: {valley: valley}});
const result = renderer.getRenderOutput();
console.log(restult);

but I find the context is not injected into the Component yet. How to inject the context into ShallowRenderer and ReacterTestRender objects like:

ReactTestRenderer = require('react-test-renderer');

const renderer = ReactTestRenderer.create(
 <Link page="https://www.facebook.com/">Facebook</Link>
);

Aucun commentaire:

Enregistrer un commentaire