Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to test if a key:value is present in Redux using Cypress

I have configured the store this way:

  ReactDOM.render(
    <CookiesProvider>
      <Provider store={store}>
        <App />
      </Provider>
    </CookiesProvider>,
    document.getElementById('root'),
  )

  if (window.Cypress) {
    window.Cypress.store = store
  }

I want to check if the following value exists. In redux is here:
store => store.auth.user.account.person.email and the value is a string, an email address.

I am trying with the following test:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 it.only('checks if email is being shown', () => {
    cy.get('.email-button')
    .should(() => {
        Cypress.store.getState().then(state => {
          console.log("state", state);
        expect(state.auth.user.account.person.email).to.equal('asfasdf')
      })
    })
  })

But it is failing with the following error:

Timed out retrying after 4000ms: Cypress.store.getState(...).then is not a function

  40 |     cy.get('.contact-button.email')
  41 |     .should(() => {
> 42 |         Cypress.store.getState().then(state => {
     |                                  ^
  43 |           console.log("state", state);
  44 |         expect(state.token).to.equal('asfasdf')
  45 |       })

How can I fix it? What am I doing wrong?

>Solution :

Redux’s store.getState() is a synchronous function. It does not return a promise. Try something like this:

it.only('checks if email is being shown', () => {
  cy.get('.email-button')
  .should(() => {
    const state = Cypress.store.getState();
    expect(state.auth.user.account.person.email).to.equal('asfasdf');
  })
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading