【发布时间】:2019-07-08 18:35:51
【问题描述】:
晚上好,
我正在使用 Gatsby 构建一个仅限客户端的应用程序。
我正在尝试为我的<Login /> 组件编写一些单元测试。
一旦用户成功登录,他们将被重定向到 /app/, 以编程方式通过 gatsby 的导航功能
这是我迄今为止的测试:
import React from 'react'
import userEvent from '@testing-library/user-event'
import 'jest-dom/extend-expect'
import { navigate } from 'gatsby'
import {
render,
fireEvent,
cleanup,
act,
wait
} from '~/__tests__/utils/wrapper'
import Login from '../index'
describe('<Login />', () => {
afterEach(cleanup)
it('should redirect to /app/ when the correct details are used', async () => {
const { getByTestId, getByLabelText } = render(<Login />)
const emailField = getByLabelText('Email address')
const passwordField = getByTestId('password-input')
act(() => {
userEvent.type(emailField, 'test@email.com')
userEvent.type(passwordField, 'password')
})
await wait()
act(() => {
fireEvent.click(getByTestId('login-submit'))
})
await wait()
expect(navigate).toHaveBeenCalledTimes(1) // FAILS
expect(navigate).toHaveBeenCalledWith('/app/') // FAILS
})
})
我在这里关注了文档: https://www.gatsbyjs.org/docs/unit-testing/#3-useful-mocks-to-complete-your-testing-environment
我还在导出的对象中添加了navigate: jest.fn()。
该组件正在工作,所以它肯定与我的测试有关。
任何帮助,不胜感激
【问题讨论】:
标签: reactjs unit-testing jestjs gatsby react-testing-library