|
| 1 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 2 | +import { renderHook, waitFor } from '@testing-library/react-native'; |
| 3 | +import React from 'react'; |
| 4 | + |
| 5 | +import { useSignUp } from '@/api/auth/use-sign-up'; |
| 6 | +import { client } from '@/api/common'; |
| 7 | + |
| 8 | +// Mock the client |
| 9 | +jest.mock('@/api/common', () => ({ |
| 10 | + client: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +const mockedClient = client as jest.MockedFunction<typeof client>; |
| 14 | + |
| 15 | +function createTestQueryClient() { |
| 16 | + return new QueryClient({ |
| 17 | + defaultOptions: { |
| 18 | + queries: { retry: false }, |
| 19 | + mutations: { retry: false }, |
| 20 | + }, |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +function createMockSignUpResponse(overrides = {}) { |
| 25 | + return { |
| 26 | + data: { |
| 27 | + status: 'success', |
| 28 | + data: { |
| 29 | + id: '1', |
| 30 | + |
| 31 | + name: 'Test User', |
| 32 | + provider: 'email', |
| 33 | + |
| 34 | + allowPasswordChange: true, |
| 35 | + ...overrides, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +function createSignUpVariables(overrides = {}) { |
| 42 | + return { |
| 43 | + |
| 44 | + name: 'Test User', |
| 45 | + password: 'password123', |
| 46 | + passwordConfirmation: 'password123', |
| 47 | + ...overrides, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +const createWrapper = () => { |
| 52 | + const queryClient = createTestQueryClient(); |
| 53 | + |
| 54 | + return ({ children }: { children: React.ReactNode }) => ( |
| 55 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 56 | + ); |
| 57 | +}; |
| 58 | + |
| 59 | +describe('useSignUp', () => { |
| 60 | + beforeEach(() => { |
| 61 | + jest.clearAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should be defined', () => { |
| 65 | + expect(useSignUp).toBeDefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should call client with correct parameters', async () => { |
| 69 | + const mockResponse = createMockSignUpResponse({ |
| 70 | + createdAt: '2023-01-01T00:00:00Z', |
| 71 | + updatedAt: '2023-01-01T00:00:00Z', |
| 72 | + }); |
| 73 | + |
| 74 | + mockedClient.mockResolvedValueOnce(mockResponse); |
| 75 | + |
| 76 | + const { result } = renderHook(() => useSignUp(), { |
| 77 | + wrapper: createWrapper(), |
| 78 | + }); |
| 79 | + |
| 80 | + const variables = createSignUpVariables(); |
| 81 | + result.current.mutate(variables); |
| 82 | + |
| 83 | + await waitFor(() => { |
| 84 | + expect(mockedClient).toHaveBeenCalledWith({ |
| 85 | + url: '/v1/users', |
| 86 | + method: 'POST', |
| 87 | + data: { |
| 88 | + user: variables, |
| 89 | + }, |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should handle sign up with all required fields', async () => { |
| 95 | + const mockResponse = createMockSignUpResponse({ |
| 96 | + |
| 97 | + name: 'New User', |
| 98 | + |
| 99 | + createdAt: '2023-01-01T00:00:00Z', |
| 100 | + updatedAt: '2023-01-01T00:00:00Z', |
| 101 | + }); |
| 102 | + |
| 103 | + mockedClient.mockResolvedValueOnce(mockResponse); |
| 104 | + |
| 105 | + const { result } = renderHook(() => useSignUp(), { |
| 106 | + wrapper: createWrapper(), |
| 107 | + }); |
| 108 | + |
| 109 | + const variables = createSignUpVariables({ |
| 110 | + |
| 111 | + name: 'New User', |
| 112 | + password: 'securepassword', |
| 113 | + passwordConfirmation: 'securepassword', |
| 114 | + }); |
| 115 | + |
| 116 | + result.current.mutate(variables); |
| 117 | + |
| 118 | + await waitFor(() => { |
| 119 | + expect(mockedClient).toHaveBeenCalledWith({ |
| 120 | + url: '/v1/users', |
| 121 | + method: 'POST', |
| 122 | + data: { |
| 123 | + user: variables, |
| 124 | + }, |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments