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

CookieProvider not re-rendering component with jest and react-testing-library #227

Closed
brandonweiss opened this issue Jul 3, 2019 · 8 comments

Comments

@brandonweiss
Copy link

brandonweiss commented Jul 3, 2019

I implemented one of those GDPR cookie banners. When you click a button it dismisses the banner and sets a cookie so we know not to show it again.

Here’s the code (I’ve simplified it a bit for clarity):

const CookieConsent = () => {
  const [cookies, setCookie] = useCookies([storageKey])
  const value = typeof cookies[storageKey] === "undefined" ? null : cookies[storageKey]
  const dismissed = JSON.parse(value)

  const dismiss = () => {
    setCookie(storageKey, true)
  }

  if (dismissed) {
    return null
  }

  return <>
    <Message>{text}</Message>

    <Button onClick={dismiss}>OK</Button>
  </>
}

This all works when testing manually, but for some reason it doesn’t seem to work when I write an automated test for it.

test("hidden after being dismissed", () => {
  let { queryByText, getByText } = render(
    <CookiesProvider>
      <CookieConsent />
    </CookiesProvider>
  )

  fireEvent.click(getByText("OK"))

  expect(queryByText(text)).not.toBeInTheDocument()
})

As best I can tell, when setCookie is called in the click handler, it’s not re-rendering the component.

Is there something I’m doing wrong or is this a bug?

@stevenmusumeche
Copy link

I am having the same issue with jest and RTL.

@stevenmusumeche
Copy link

This comment on a different issue solved it for me: #113 (comment)

@eXon
Copy link
Collaborator

eXon commented Jul 4, 2019

In v4.0.1, it should detect JSDOM and skip the browser. Can you confirm it now work?

@brandonweiss
Copy link
Author

@eXon Thanks! Hmm, it doesn’t seem to be fixed for me…

@mocantimoteidavid
Copy link

mocantimoteidavid commented Jul 4, 2019

For me too, the solution from the issue above doesn't work with typescript.

@stevenmusumeche
Copy link

@mocantimoteidavid this is a good place for @ts-ignore, IMO

@eXon
Copy link
Collaborator

eXon commented Dec 27, 2019

Should be fixed on the next version

@eXon eXon closed this as completed Dec 27, 2019
@eXon
Copy link
Collaborator

eXon commented Dec 27, 2019

Released with v4.0.3

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

4 participants