如何使用Cypress测试Redux中是否存在key:value

svujldwt  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(150)

我已按以下方式配置了存储:

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

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

我想检查下面的值是否存在。在redux中是这样的:store =〉store.auth.user.account.person.email,值是一个字符串,即电子邮件地址。
我正在尝试进行以下测试:

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')
      })
    })
  })

但由于以下错误而失败:

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 |       })

我该怎么补救?我做错了什么?

zhte4eai

zhte4eai1#

Redux的store.getState()是一个同步函数。它不返回一个promise。请尝试类似这样的操作:

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');
  })
})

相关问题