Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing react-helmet code

I am using react-helmet to add elements to head element.

<Helmet>
     <title>title</title>
     <meta name="description" content="description" />
     <meta name="keywords" content="keywords" />
</Helmet>

And I am trying to write unit test like this:

it('should render metadata', () => {
    const wrapper = mount(<Metadata/>);
    // this is not working.
    expect(document.title).to.equal("title");
});
like image 289
emphaticsunshine Avatar asked May 19 '17 15:05

emphaticsunshine


2 Answers

I figured out the answer myself. I did following:

it('should render metadata', () => {
    const wrapper = mount(<Metadata/>);
    // this will return all the markup assigned to helmet
    // which will get rendered inside head.
    const helmet = Helmet.peek();
    expect(helmet.title).to.equal("title");
});

This does the trick for unit test.

like image 163
emphaticsunshine Avatar answered Sep 27 '22 23:09

emphaticsunshine


I was in a similar situation with react testing library and JSDOM, but I didn't want to make the test specific to the Helmet implementation because that's against the guiding principle of react testing library. I found that doing the obvious thing:

render(<App />)
await waitFor(...some DOM element)
expect(document.title).toEqual("title")

does not work even if the DOM element you're awaiting is rendered in the same render as the <Helmet>

However, this does work:

await waitFor(...some DOM element)
await waitFor( () => expect(document.title).toEqual("title"))

I presume that between React and Helmet there's some timing thing going on which means there's a delay between the rest of the DOM being rendered and the title being set in JSDOM.

like image 29
Andy Avatar answered Sep 27 '22 23:09

Andy