Open andrastothtw opened 5 years ago
I have the same issue
I've managed to get this working when using 'https://testing-library.com/docs/react-testing-library/'
// Home.js
<>
<Helmet>
<title>I am a meta title</title>
</Helmet>
<h1>I am a title</h1>
</>
// Home.test.js
import { render, wait } from '@testing-library/react'
it('Should render the correct meta data', async () => {
render(<Home />)
await wait(() => expect(document.title).toEqual('I am a meta title'))
})
@andrastothtw I've found the following setup works:
import React from "react"
import {Helmet} from "react-helmet"
import {render} from "enzyme"
Helmet.canUseDOM = false
describe("react-helmet", () => {
afterEach(() => {
Helmet.rewind()
})
it("test 1", () => {
render(<MyApp />)
const helmet = Helmet.peek()
expect(...).toBeTruthy()
})
it("test 2", () => {
render(<MyApp someProp />)
const helmet = Helmet.peek()
expect(...).toBeFalsy()
})
})
From what I can tell, rewind is the reset function you are looking for, but it doesn't work unless you set Helmet.canUseDOM = false
which tells Helmet to render in a server context.
If it helps, the peek and rewind functions come from react-side-effect, which helped me debug this problem.
Any update regarding this issue or any workarounds from other engineers?
Facing the same issue, the order of unit tests within a test class is changing the values I get from Helmet.peek() as Helmet seems to be keeping track of all the mounted components as part of the earlier tests.
This is a question/bug report.
How can you 'reset' elements put into
<head>
by Helmet?Helmet.peek()
returns elements from previous tests in the same file? See code below:Code to reproduce
What is the expected behavior? These two cases should be independent.
Which versions of React and react-helmet...?
react-helmet
: 5.2.1react
: 16.4.1