Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide mechanism for accessing the page title #402

Closed
jednano opened this issue Jul 5, 2019 · 3 comments
Closed

Provide mechanism for accessing the page title #402

jednano opened this issue Jul 5, 2019 · 3 comments

Comments

@jednano
Copy link

jednano commented Jul 5, 2019

Describe the feature you'd like:

Similar to this guy who has yet to receive an answer, I'm looking to do the exact same thing (i.e., test dynamic changes to the page title).

Suggested implementation:

const { getPageTitle } = render(...);

Describe alternatives you've considered:

I tried just testing expect(document.title).toEqual('something') or even requestAnimationFrame before testing the title, which is what Helmet does for their tests, but no 🎲 .

Teachability, Documentation, Adoption, Migration Strategy:

  1. I'm not sure if this somehow goes against the @testing-library paradigms.
  2. I think it's a pretty self-explanatory method name, so I wouldn't want to add any more noise to it.
@Gpx
Copy link
Member

Gpx commented Jul 7, 2019

This works for me:

test("test", async () => {
  const Foo = () => (
    <>
      <Helmet>
        <title>foo</title>
      </Helmet>
      <div>Hello</div>
    </>
  );
  render(<Foo />);
  await wait(() => expect(document.title).toEqual("foo"));
 });

@mbrowne
Copy link

mbrowne commented Aug 14, 2019

This also works:

    await waitForDomChange()
    expect(document.title).toEqual("foo")

And actually if you're not checking for document.title directly, then wait() will NOT work. For example I needed to use waitForDomChange() to get this to work (MetaDataOpenGraph is our custom component, which uses Helmet internally):

    it('should add title and meta tags to <head>', async () => {
        render(<MetaDataOpenGraph {...metadata} routeChanged={false} />)
        await waitForDomChange()
        expect(document.querySelector('head')).toMatchSnapshot()
    })

@ctsstc
Copy link

ctsstc commented Jan 14, 2022

A note from the future, this seems to be the updated API:

it('renders the login page title', async () => {
    await waitFor(() => expect(document.title).toBe('Company Name - Login'));
  });

A note for Next JS users, this doesn't seem to work with the <Head> component unfortunately.
Here's an interesting thread on it: vercel/next.js#11060

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants