В моем компоненте реагирования есть статическая функция, которую я хочу протестировать с помощью jest.
static async getInitialProps (context, apolloClient) {
const { req } = context
const initProps = { user: {} }
if (req && req.headers) {
const cookies = req.headers.cookie
if (typeof cookies === 'string') {
const cookiesJSON = jsHttpCookie.parse(cookies)
initProps.token = cookiesJSON['auth-token']
if (cookiesJSON['auth-token']) {
jwt.verify(cookiesJSON['auth-token'], secret, (error, decoded) => {
if (error) {
console.error(error)
} else {
redirect(context, '/')
}
})
}
}
}
}
Это то, что я получил до сих пор, это тестирование для вызова jwt.verify
.Но как мне проверить обратный вызов этого?Я хочу проверить на звонок redirect
, если нет ошибки ...
test('should call redirect', () => {
// SETUP
const context = { req: { headers: { cookie: 'string' } } }
jsHttpCookie.parse = jest.fn().mockReturnValueOnce({ 'auth-token': 'token' })
jwt.verify = jest.fn(() => redirect)
// EXECUTE
Page.getInitialProps(context, {})
// VERIFY
expect(jwt.verify).toHaveBeenCalled()
})