I'm trying to create a unit test for a react component, using jest + enzyme:
it('renders the problem view without crashing', () => {
// TODO: tell the test fixure that the current URL is /problems
const wrapper = shallow(<App />);
// ... check that the problem view is actually rendered...
});
Depending on the URL (suffix), the App component should render a different page (using react-router v4). I don't know how to inject an URL into the test fixture:
- Do I have to inject this into jsdom? If yes, how?
- Is there another way to achieve this?
Any pointers to the right direction are highly appreciated!
Edit 1: The answer on this post (How to test react-router with enzyme) suggest to write a kind of white-box unit test, where the Router object is retrieved and its URL->component mapping table is checked for correctness. However, I'd like to write a black-box unit test: given the right environment (i.e. URL) the component under test shall render the right stuff, irrespective of the fact which client-side routing library is used.
Edit 2: The Jest config parameter testURL looks almost like what I need, but it seems that it cannot be set from inside a test. The answer from this question gives some hints, but I didn't find anything about injecting a custom jsdom object from within a test into Jest in the official Jest docs.