|
| 1 | +import { ComponentWithProviders, MockAdapter, renderHook } from '@baseapp-frontend/test' |
| 2 | +import { axios } from '@baseapp-frontend/utils' |
| 3 | + |
| 4 | +import { z } from 'zod' |
| 5 | + |
| 6 | +import useChangeExpiredPassword from '../index' |
| 7 | + |
| 8 | +// @ts-ignore TODO: (BA-1081) investigate AxiosRequestHeaders error |
| 9 | +export const axiosMock = new MockAdapter(axios) |
| 10 | + |
| 11 | +describe('useChangeExpiredPassword', () => { |
| 12 | + const currentPassword = '1234' |
| 13 | + const password = '123456' |
| 14 | + const token = 'fake-token' |
| 15 | + |
| 16 | + test('should run onSuccess', async () => { |
| 17 | + axiosMock.onPost('/change-expired-password').reply(200, { |
| 18 | + currentPassword, |
| 19 | + newPassword: password, |
| 20 | + token, |
| 21 | + }) |
| 22 | + |
| 23 | + let hasOnSuccessRan = false |
| 24 | + |
| 25 | + const { result } = renderHook( |
| 26 | + () => |
| 27 | + useChangeExpiredPassword({ |
| 28 | + token, |
| 29 | + defaultValues: { |
| 30 | + currentPassword, |
| 31 | + newPassword: password, |
| 32 | + confirmNewPassword: password, |
| 33 | + }, |
| 34 | + options: { |
| 35 | + onSuccess: () => { |
| 36 | + hasOnSuccessRan = true |
| 37 | + }, |
| 38 | + }, |
| 39 | + }), |
| 40 | + { |
| 41 | + wrapper: ComponentWithProviders, |
| 42 | + }, |
| 43 | + ) |
| 44 | + |
| 45 | + await result.current.form.handleSubmit() |
| 46 | + |
| 47 | + expect(hasOnSuccessRan).toBe(true) |
| 48 | + }) |
| 49 | + |
| 50 | + test('should run onError', async () => { |
| 51 | + axiosMock.onPost('/change-expired-password').reply(500, { |
| 52 | + error: 'any', |
| 53 | + }) |
| 54 | + |
| 55 | + let hasOnErrorRan = false |
| 56 | + |
| 57 | + const { result } = renderHook( |
| 58 | + () => |
| 59 | + useChangeExpiredPassword({ |
| 60 | + token, |
| 61 | + defaultValues: { |
| 62 | + currentPassword, |
| 63 | + newPassword: password, |
| 64 | + confirmNewPassword: password, |
| 65 | + }, |
| 66 | + options: { |
| 67 | + onError: () => { |
| 68 | + hasOnErrorRan = true |
| 69 | + }, |
| 70 | + }, |
| 71 | + }), |
| 72 | + { |
| 73 | + wrapper: ComponentWithProviders, |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + await result.current.form.handleSubmit() |
| 78 | + |
| 79 | + expect(hasOnErrorRan).toBe(true) |
| 80 | + }) |
| 81 | + |
| 82 | + test('should run onError when newPassword and confirmNewPassword are different', async () => { |
| 83 | + axiosMock.onPost('/change-expired-password').reply(200, {}) |
| 84 | + |
| 85 | + let hasOnSuccessRan = false |
| 86 | + |
| 87 | + const { result } = renderHook( |
| 88 | + () => |
| 89 | + useChangeExpiredPassword({ |
| 90 | + token, |
| 91 | + defaultValues: { |
| 92 | + currentPassword, |
| 93 | + newPassword: password, |
| 94 | + confirmNewPassword: `${password}different`, |
| 95 | + }, |
| 96 | + options: { |
| 97 | + onSuccess: () => { |
| 98 | + hasOnSuccessRan = true |
| 99 | + }, |
| 100 | + }, |
| 101 | + }), |
| 102 | + { |
| 103 | + wrapper: ComponentWithProviders, |
| 104 | + }, |
| 105 | + ) |
| 106 | + |
| 107 | + await result.current.form.handleSubmit() |
| 108 | + |
| 109 | + expect(hasOnSuccessRan).toBe(false) |
| 110 | + }) |
| 111 | + |
| 112 | + test('should allow custom defaultValues and validationSchema', async () => { |
| 113 | + axiosMock.onPost('/change-expired-password').reply(200, {}) |
| 114 | + |
| 115 | + const customDefaultValues = { |
| 116 | + currentPassword: '1234', |
| 117 | + newPassword: '12345', |
| 118 | + confirmNewPassword: '123456', // that would pass since the schema is not the default one, and doesnt check for password equality |
| 119 | + } |
| 120 | + const customValidationSchema = z.object({ |
| 121 | + currentPassword: z.string().nonempty(), |
| 122 | + newPassword: z.string().nonempty(), |
| 123 | + confirmNewPassword: z.string().nonempty(), |
| 124 | + }) |
| 125 | + |
| 126 | + let hasOnSuccessRan = false |
| 127 | + |
| 128 | + const { result } = renderHook( |
| 129 | + () => |
| 130 | + useChangeExpiredPassword({ |
| 131 | + token, |
| 132 | + defaultValues: customDefaultValues, |
| 133 | + validationSchema: customValidationSchema, |
| 134 | + options: { |
| 135 | + onSuccess: () => { |
| 136 | + hasOnSuccessRan = true |
| 137 | + }, |
| 138 | + }, |
| 139 | + }), |
| 140 | + { |
| 141 | + wrapper: ComponentWithProviders, |
| 142 | + }, |
| 143 | + ) |
| 144 | + |
| 145 | + await result.current.form.handleSubmit() |
| 146 | + |
| 147 | + expect(hasOnSuccessRan).toBe(true) |
| 148 | + }) |
| 149 | +}) |
0 commit comments