|
1 | | -import { describe } from 'vitest'; |
| 1 | +import { act } from 'react'; |
| 2 | +import { isOk } from 'true-myth/result'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | 4 |
|
3 | | -describe.todo('useAuth', () => {}); |
| 5 | +import type { ReactHookForm, UnknownMouseEvent } from '~/core/hooks/useSubmit'; |
| 6 | +import useLoginForm from '~/features/authentication/public/hooks/useLoginForm'; |
| 7 | +import { LoginSchema } from '~/features/schemas/Login'; |
| 8 | +import { renderHook } from '~/vitest/utils'; |
| 9 | + |
| 10 | +// Variables to capture the callbacks passed to useSubmit |
| 11 | +let capturedSubmitOptions: null | { |
| 12 | + onInvalidSubmit: (form: ReactHookForm<any>) => string; |
| 13 | + onValidSubmit: (data: any) => Promise<any>; |
| 14 | +} = null; |
| 15 | + |
| 16 | +// Mock useSubmit to simply capture the callbacks and return the onValidSubmit callback as onSubmit |
| 17 | +vi.mock('~/core/hooks/useSubmit', () => ({ |
| 18 | + __esModule: true, |
| 19 | + default: (options: { onInvalidSubmit: any; onValidSubmit: any }) => { |
| 20 | + capturedSubmitOptions = options; |
| 21 | + return options.onValidSubmit; |
| 22 | + }, |
| 23 | +})); |
| 24 | + |
| 25 | +// Create a mock for useNavigate |
| 26 | +const mockNavigate = vi.fn(); |
| 27 | +vi.mock('react-router', () => ({ |
| 28 | + useNavigate: () => mockNavigate, |
| 29 | +})); |
| 30 | + |
| 31 | +// Prepare a mock for useAuth to control the authentication.signIn behavior |
| 32 | +const mockSignIn = vi.fn(); |
| 33 | +const mockAuth = { |
| 34 | + authenticate: { |
| 35 | + signIn: mockSignIn, |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +vi.mock('~/features/authentication/public/hooks/useAuth', () => ({ |
| 40 | + __esModule: true, |
| 41 | + default: () => mockAuth, |
| 42 | +})); |
| 43 | + |
| 44 | +// Import the hook under test |
| 45 | + |
| 46 | +describe('useLoginForm', () => { |
| 47 | + const redirectUrl = '/dashboard'; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + mockNavigate.mockReset(); |
| 51 | + mockSignIn.mockReset(); |
| 52 | + capturedSubmitOptions = null; |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return expected properties', () => { |
| 56 | + const { result } = renderHook(() => useLoginForm(redirectUrl)); |
| 57 | + |
| 58 | + expect(result.current).toHaveProperty('control'); |
| 59 | + expect(result.current).toHaveProperty('dirtyFields'); |
| 60 | + expect(result.current).toHaveProperty('errors'); |
| 61 | + expect(result.current).toHaveProperty('isDirty'); |
| 62 | + expect(result.current).toHaveProperty('isSubmitSuccessful'); |
| 63 | + expect(result.current).toHaveProperty('isSubmitted'); |
| 64 | + expect(result.current).toHaveProperty('isSubmitting'); |
| 65 | + expect(result.current).toHaveProperty('isValid'); |
| 66 | + expect(result.current).toHaveProperty('onSubmit'); |
| 67 | + expect(result.current).toHaveProperty('register'); |
| 68 | + expect(result.current).toHaveProperty('schema', LoginSchema); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should call navigate on valid submission', async () => { |
| 72 | + // Arrange: mock signIn to return an Ok result |
| 73 | + mockSignIn.mockResolvedValue({ _tag: 'Ok' }); |
| 74 | + |
| 75 | + const { result } = renderHook(() => useLoginForm(redirectUrl)); |
| 76 | + |
| 77 | + // Act: call the onSubmit callback (which is onValidSubmit from our mocked useSubmit) |
| 78 | + await act(async () => { |
| 79 | + const res = await result.current.onSubmit(new MouseEvent('click') as unknown as UnknownMouseEvent); |
| 80 | + |
| 81 | + // And if signIn returns Ok, then navigate should be called |
| 82 | + if (isOk(res)) { |
| 83 | + expect(mockNavigate).toHaveBeenCalledWith(redirectUrl ?? '/'); |
| 84 | + } |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return the error result when sign in fails', async () => { |
| 89 | + // Arrange: mock signIn to return an Err result |
| 90 | + const errorResult = { _tag: 'Err', error: new Error('Invalid credentials') }; |
| 91 | + mockSignIn.mockResolvedValue(errorResult); |
| 92 | + |
| 93 | + const { result } = renderHook(() => useLoginForm(redirectUrl)); |
| 94 | + |
| 95 | + // Act: call the onSubmit callback |
| 96 | + let returnedResult; |
| 97 | + await act(async () => { |
| 98 | + returnedResult = await result.current.onSubmit(new MouseEvent('click') as unknown as UnknownMouseEvent); |
| 99 | + }); |
| 100 | + |
| 101 | + expect(mockNavigate).not.toHaveBeenCalled(); |
| 102 | + expect(returnedResult).toEqual(errorResult); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('onInvalidSubmit (extracted from useSubmit)', () => { |
| 106 | + it('should return "Username and password are required" when both errors exist', () => { |
| 107 | + // Build a dummy ReactHookForm with errors on both fields |
| 108 | + const dummyForm = { |
| 109 | + formState: { |
| 110 | + errors: { |
| 111 | + password: { type: 'required' }, |
| 112 | + username: { type: 'required' }, |
| 113 | + }, |
| 114 | + isSubmitted: true, |
| 115 | + isValid: false, |
| 116 | + }, |
| 117 | + } as unknown as ReactHookForm<any>; |
| 118 | + |
| 119 | + // Ensure that our captured callback exists by rendering the hook |
| 120 | + renderHook(() => useLoginForm(redirectUrl)); |
| 121 | + const errorMessage = capturedSubmitOptions?.onInvalidSubmit(dummyForm); |
| 122 | + expect(errorMessage).toBe('Username and password are required'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should return "Password is required" when only password has error', () => { |
| 126 | + const dummyForm = { |
| 127 | + formState: { |
| 128 | + errors: { |
| 129 | + password: { type: 'required' }, |
| 130 | + }, |
| 131 | + isSubmitted: true, |
| 132 | + isValid: false, |
| 133 | + }, |
| 134 | + } as unknown as ReactHookForm<any>; |
| 135 | + |
| 136 | + renderHook(() => useLoginForm(redirectUrl)); |
| 137 | + const errorMessage = capturedSubmitOptions?.onInvalidSubmit(dummyForm); |
| 138 | + expect(errorMessage).toBe('Password is required'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should return "Username is required" when only username has error', () => { |
| 142 | + const dummyForm = { |
| 143 | + formState: { |
| 144 | + errors: { |
| 145 | + username: { type: 'required' }, |
| 146 | + }, |
| 147 | + isSubmitted: true, |
| 148 | + isValid: false, |
| 149 | + }, |
| 150 | + } as unknown as ReactHookForm<any>; |
| 151 | + |
| 152 | + renderHook(() => useLoginForm(redirectUrl)); |
| 153 | + const errorMessage = capturedSubmitOptions?.onInvalidSubmit(dummyForm); |
| 154 | + expect(errorMessage).toBe('Username is required'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should return "Invalid username or password" when form is submitted and valid', () => { |
| 158 | + const dummyForm = { |
| 159 | + formState: { |
| 160 | + errors: {}, |
| 161 | + isSubmitted: true, |
| 162 | + isValid: true, |
| 163 | + }, |
| 164 | + } as unknown as ReactHookForm<any>; |
| 165 | + |
| 166 | + renderHook(() => useLoginForm(redirectUrl)); |
| 167 | + const errorMessage = capturedSubmitOptions?.onInvalidSubmit(dummyForm); |
| 168 | + expect(errorMessage).toBe('Invalid username or password'); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments