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

Issue with the getState mock #156

Open
maschad opened this issue Aug 27, 2018 · 1 comment
Open

Issue with the getState mock #156

maschad opened this issue Aug 27, 2018 · 1 comment

Comments

@maschad
Copy link

maschad commented Aug 27, 2018

Hi, so I am having an issue that's a variant of #71 , and I understand that getState returns the same state based on @dmitry-zaets explanation that the library is for testing the actions, but I have the two following files:

HttpClient-test.js

 it('Should place cookie in  header to be sent if there is one in local storage', () => {
    const opts = {
      headers: {}
    }
    const cookie = 'cookie'
    const store = mockStore({
      global: {
        cookie: cookie
      }
    })
    const client = new HttpClient(null, store.dispatch)
    const expectedOpts = {
      headers: {
        'X-RSA-COOKIE': cookie
      }
    }
    expect(client.sendRSACookie(opts)).toEqual(expectedOpts)
  })

HttpClient.js

import {store} '../configureStore'

sendRSACookie (opts) {
    if(_.isUndefined(opts.headers['X-RSA-COOKIE'])) {
      this.dispatch(retrieveCookie)
      const cookie = store.getState().global.cookie
      if (!_.isUndefined(cookie) && cookie !== null)  {
        opts.headers['X-RSA-COOKIE'] = store.getState().global.cookie
      }
    }
    return opts
  }

and the test is failing because the store.getState().global.cookie is null. How would I be able to test this without attempting to override the getState function?

@acostalima
Copy link

You can initialize the state to whatever you want, so there is no need to rewrite or override the getState function. Aside from this, I'm wondering how your code is working with a real Redux store. AFAIK, you should subscribe to state changes and only then you read the state.

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

2 participants