3

I'm trying to set a cookie in my test to make sure it's getting cleared out in my component:

import Cookies from 'universal-cookie';

test('successfully logs the user out', async () => {
  const cookie = new Cookies()
  cookie.set('authtoken', 'some-token')
  const { getByText } = render(<Logout/>)
})

But in my Logout component the cookies object is empty:

export default function Logout() {
  const [cookies, setCookie, removeCookie] = useCookies(['authtoken'])
  console.log(cookies)
}

Is there another way to do this? Preferably one that isn't passing the cookie as a prop.

dan-klasson
  • 13,734
  • 14
  • 63
  • 101
  • I don't know the inner working of those two packages, but it seems like you are trying to use 2 different cookie packages; react-cookie and universal-cookie. If you are using ssr use the first, else the second. Do you have any more context that may help? – Devin Oct 13 '19 at 16:34
  • @Devin One package has a dependency on the other. It's really a `react-cookie` question I think. – dan-klasson Oct 13 '19 at 16:37
  • Cookies in react cookie are provided contextually. Is it possible your cookie is not being provided because the cookie provider is not being rendered? Docs example export default function Root() { return ( ); } – Devin Oct 13 '19 at 16:49
  • @Devin I tried putting `` in the `render` method too. But still no dice. – dan-klasson Oct 13 '19 at 17:01

3 Answers3

2

So the problem was what @Oleg and @Devin were hinting at. To render the provider as well. But I also had to pass the cookies as parameter like so:

import Cookies from 'universal-cookie';

test('successfully logs the user out', async () => {
  const cookie = new Cookies({authtoken: 'some-token'});
  const { getByText } = render(
    <CookiesProvider cookies={cookie}>
      <Router>
        <Logout/>
      </Router>
    </CookiesProvider>
  )
})
dan-klasson
  • 13,734
  • 14
  • 63
  • 101
0

By adding

 const cookie = new Cookies();
 cookie.HAS_DOCUMENT_COOKIE = false;
Oleg
  • 3,580
  • 1
  • 7
  • 12
0

According to react-cookie, you can access the cookies directly using the DOM.

 document.cookie.split(';').forEach(function(c) {
    document.cookie = c
      .replace(/^ +/, '')
      .replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/');
 });

This function is to clear cookies.

Something else that I was thinking in an earlier comment was that you render the component with cookies via "

const cookied = withCookies(<Login />)
render(cookied)

"

A link to this information and it's context can be found at: https://github.com/reactivestack/cookies/blob/master/packages/universal-cookie/src/utils.ts

Devin
  • 134
  • 9
  • This should be the same as `document.cookie='foo'` no? Tried it as well and no dice unfortunately – dan-klasson Oct 13 '19 at 17:03
  • Seems like according to the docs `withCookies` sets some props on the component, which is not what I want. Tried as well and still no luck – dan-klasson Oct 13 '19 at 17:12
  • Well, i'm working on my own ssr application. I'm going to integrate cookies at the global level right now. I'll let you know if I run into this problem or why it works if I don't. – Devin Oct 13 '19 at 17:24
  • See my answer for the solution. It's not really ideal though as all I'm doing is passing in the cookie so it's available in the component. – dan-klasson Oct 13 '19 at 17:30