-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseUpdateUser.test.tsx
82 lines (68 loc) · 3.05 KB
/
useUpdateUser.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {
ComponentWithProviders,
CookiesGetByNameFn,
MockAdapter,
renderHook,
waitFor,
} from '@baseapp-frontend/test'
import { TokenTypes, axios } from '@baseapp-frontend/utils'
import { UseMutationResult } from '@tanstack/react-query'
import Cookies from 'js-cookie'
import { IUser } from '../../../../types/user'
import { UseUpdateUserOptions } from '../types'
import request from './fixtures/request.json'
interface IIUseUpdateUser<IUser> extends Omit<UseMutationResult<IUser, unknown>, 'data'> {
user?: IUser
}
// TODO: BA-1308: improve tests
describe('useUserUpdate', () => {
// @ts-ignore TODO: (BA-1081) investigate AxiosRequestHeaders error
const axiosMock = new MockAdapter(axios)
let useUserUpdate: <TUser extends Partial<IUser>>(
props?: UseUpdateUserOptions<TUser>,
) => IIUseUpdateUser<TUser>
const decodeJWTMock = jest.fn()
const refreshAccessTokenMock = jest.fn()
jest.mock('@baseapp-frontend/utils', () => ({
...jest.requireActual('@baseapp-frontend/utils'),
decodeJWT: decodeJWTMock,
refreshAccessToken: refreshAccessTokenMock,
}))
const useQueryClientMock = jest.fn()
jest.mock('@tanstack/react-query', () => ({
...jest.requireActual('@tanstack/react-query'),
useQueryClient: useQueryClientMock,
}))
beforeAll(async () => {
process.env.NEXT_PUBLIC_TOKEN_TYPE = TokenTypes.jwt
useUserUpdate = (await import('../index')).default as any
// freeze time to
jest.useFakeTimers().setSystemTime(new Date(2020, 9, 1, 7))
})
const token =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzA5NjcxNjgzLCJpYXQiOjE3MDk2NjA3MTIsImp0aSI6IjhmMjg3ZGNhODVjODRjOTVhOThkZThmN2NiZTllNTE5IiwidXNlcl9pZCI6MSwiaWQiOjEsImVtYWlsIjoiYWFAdHNsLmlvIiwiaXNfZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJuZXdfZW1haWwiOiIiLCJpc19uZXdfZW1haWxfY29uZmlybWVkIjpmYWxzZSwicmVmZXJyYWxfY29kZSI6IiIsImF2YXRhciI6eyJmdWxsX3NpemUiOiIvbWVkaWEvdXNlci1hdmF0YXJzLzUvNi8xL3Jlc2l6ZWQvMTAyNC8xMDI0LzU0NGNhNDA2YWUxMWJhYzVjNDk3NTlhMjQwN2ZkY2JlLnBuZyIsInNtYWxsIjoiL21lZGlhL3VzZXItYXZhdGFycy81LzYvMS9yZXNpemVkLzY0LzY0LzU0NGNhNDA2YWUxMWJhYzVjNDk3NTlhMjQwN2ZkY2JlLnBuZyJ9LCJmaXJzdF9uYW1lIjoiYWEiLCJsYXN0X25hbWUiOiJUZXN0In0.zmTBh3Iz6iRGTiV84o7r4JMA3AU4Q4bVbN76ZUwm5Jg'
it('should run custom onSettled on 401', async () => {
;(Cookies.get as CookiesGetByNameFn) = jest.fn(() => token)
decodeJWTMock.mockImplementation(() => undefined)
const invalidateQueriesMock = jest.fn()
useQueryClientMock.mockImplementation(() => ({ invalidateQueries: invalidateQueriesMock }))
let hasOnSettledRan = false
axiosMock.onPatch('/users/1').reply(201, request)
const { result } = renderHook(
() =>
useUserUpdate({
options: {
onSettled: () => {
hasOnSettledRan = true
},
},
}),
{
wrapper: ComponentWithProviders,
},
)
await result.current.mutateAsync({ userId: 1, data: { firstName: 'BB' } })
await waitFor(() => expect(result.current.isLoading).toBe(false))
await waitFor(() => expect(hasOnSettledRan).toBe(true))
})
})