|
| 1 | +import { render, fireEvent, waitFor, screen } from '@testing-library/preact'; |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | +import { ResendVerification } from '../ResendVerification'; |
| 4 | + |
| 5 | +// Mock utils (fetchClient) – path resolution to actual file used by component |
| 6 | +vi.mock('../../utils', () => ({ |
| 7 | + fetchClient: { |
| 8 | + POST: vi.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +// Mock i18n |
| 13 | +vi.mock('react-i18next', () => ({ |
| 14 | + useTranslation: () => ({ |
| 15 | + t: (key: string) => key, |
| 16 | + i18n: { language: 'en' }, |
| 17 | + }), |
| 18 | +})); |
| 19 | + |
| 20 | +describe('ResendVerification', () => { |
| 21 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 22 | + let mockUtils: any; |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + vi.clearAllMocks(); |
| 26 | + mockUtils = await import('../../utils'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns null when email prop is empty', () => { |
| 30 | + const { container } = render(<ResendVerification email="" />); |
| 31 | + expect(container.firstChild).toBeNull(); |
| 32 | + expect(screen.queryByTestId('resend-verification')).toBeNull(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('renders resend button when email provided', () => { |
| 36 | + render(<ResendVerification email="[email protected]" />); |
| 37 | + expect(screen.getByTestId('submit-button')).toBeTruthy(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('calls API and shows success alert on 200', async () => { |
| 41 | + mockUtils.fetchClient.POST.mockResolvedValue({ response: { status: 200 } }); |
| 42 | + |
| 43 | + render(<ResendVerification email="[email protected]" />); |
| 44 | + const btn = screen.getByTestId('submit-button'); |
| 45 | + fireEvent.click(btn); |
| 46 | + |
| 47 | + await waitFor(() => { |
| 48 | + expect(mockUtils.fetchClient.POST).toHaveBeenCalledWith('/auth/resend_verification', { |
| 49 | + body: { email: '[email protected]' }, |
| 50 | + headers: { 'X-Lang': 'en' }, |
| 51 | + }); |
| 52 | + expect(screen.getByTestId('resend-success')).toBeTruthy(); |
| 53 | + // Button should disappear after success |
| 54 | + expect(screen.queryByTestId('submit-button')).toBeNull(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('calls API and shows error alert on non-200', async () => { |
| 59 | + mockUtils.fetchClient.POST.mockResolvedValue({ response: { status: 500 } }); |
| 60 | + |
| 61 | + render(<ResendVerification email="[email protected]" />); |
| 62 | + const btn = screen.getByTestId('submit-button'); |
| 63 | + fireEvent.click(btn); |
| 64 | + |
| 65 | + await waitFor(() => { |
| 66 | + expect(mockUtils.fetchClient.POST).toHaveBeenCalledTimes(1); |
| 67 | + expect(screen.getByTestId('resend-error')).toBeTruthy(); |
| 68 | + // Button still present (still can retry) since done=false |
| 69 | + expect(screen.getByTestId('submit-button')).toBeTruthy(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('shows spinner and prevents double submission while sending', async () => { |
| 74 | + let resolvePromise: (value: unknown) => void; |
| 75 | + const pending = new Promise(resolve => { resolvePromise = resolve; }); |
| 76 | + mockUtils.fetchClient.POST.mockReturnValue(pending); |
| 77 | + |
| 78 | + render(<ResendVerification email="[email protected]" />); |
| 79 | + const btn = screen.getByTestId('submit-button'); |
| 80 | + fireEvent.click(btn); |
| 81 | + // Immediate second click attempt |
| 82 | + fireEvent.click(btn); |
| 83 | + |
| 84 | + expect(mockUtils.fetchClient.POST).toHaveBeenCalledTimes(1); |
| 85 | + |
| 86 | + // Spinner should be visible while pending (mocked Spinner renders 'Loading...') |
| 87 | + expect(screen.getByText('Loading...')).toBeTruthy(); |
| 88 | + |
| 89 | + // Finish request with non-success to keep button |
| 90 | + // @ts-expect-error resolvePromise defined above |
| 91 | + resolvePromise({ response: { status: 500 } }); |
| 92 | + |
| 93 | + await waitFor(() => { |
| 94 | + expect(screen.getByTestId('resend-error')).toBeTruthy(); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments