|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 2 | +import { render, screen, cleanup, act } from '@testing-library/react' |
| 3 | +import userEvent from '@testing-library/user-event' |
| 4 | +import RouteErrorState from './RouteErrorState' |
| 5 | + |
| 6 | +describe('RouteErrorState', () => { |
| 7 | + beforeEach(() => { |
| 8 | + vi.useFakeTimers() |
| 9 | + }) |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + cleanup() |
| 13 | + vi.restoreAllMocks() |
| 14 | + }) |
| 15 | + |
| 16 | + it('renders title and message', () => { |
| 17 | + render( |
| 18 | + <RouteErrorState |
| 19 | + title="Loading Failed" |
| 20 | + message="Could not load portfolio data." |
| 21 | + onRetry={() => {}} |
| 22 | + />, |
| 23 | + ) |
| 24 | + expect(screen.getByText('Loading Failed')).toBeInTheDocument() |
| 25 | + expect(screen.getByText('Could not load portfolio data.')).toBeInTheDocument() |
| 26 | + }) |
| 27 | + |
| 28 | + it('calls onRetry when retry button is clicked', async () => { |
| 29 | + const onRetry = vi.fn() |
| 30 | + render( |
| 31 | + <RouteErrorState |
| 32 | + title="Error" |
| 33 | + message="Something went wrong." |
| 34 | + onRetry={onRetry} |
| 35 | + />, |
| 36 | + ) |
| 37 | + |
| 38 | + const button = screen.getByRole('button', { name: /retry/i }) |
| 39 | + await act(async () => { |
| 40 | + button.click() |
| 41 | + }) |
| 42 | + |
| 43 | + expect(onRetry).not.toHaveBeenCalled() |
| 44 | + |
| 45 | + act(() => { |
| 46 | + vi.advanceTimersByTime(1000) |
| 47 | + }) |
| 48 | + |
| 49 | + expect(onRetry).toHaveBeenCalledTimes(1) |
| 50 | + }) |
| 51 | + |
| 52 | + it('applies exponential backoff on repeated retries', async () => { |
| 53 | + const onRetry = vi.fn() |
| 54 | + render( |
| 55 | + <RouteErrorState |
| 56 | + title="Error" |
| 57 | + message="Failed." |
| 58 | + onRetry={onRetry} |
| 59 | + />, |
| 60 | + ) |
| 61 | + |
| 62 | + const button = screen.getByRole('button', { name: /retry/i }) |
| 63 | + |
| 64 | + await act(async () => { button.click() }) |
| 65 | + act(() => { vi.advanceTimersByTime(1000) }) |
| 66 | + expect(onRetry).toHaveBeenCalledTimes(1) |
| 67 | + |
| 68 | + await act(async () => { button.click() }) |
| 69 | + expect(onRetry).toHaveBeenCalledTimes(1) |
| 70 | + |
| 71 | + act(() => { vi.advanceTimersByTime(1000) }) |
| 72 | + expect(onRetry).toHaveBeenCalledTimes(1) |
| 73 | + |
| 74 | + act(() => { vi.advanceTimersByTime(1000) }) |
| 75 | + expect(onRetry).toHaveBeenCalledTimes(2) |
| 76 | + |
| 77 | + await act(async () => { button.click() }) |
| 78 | + act(() => { vi.advanceTimersByTime(4000) }) |
| 79 | + expect(onRetry).toHaveBeenCalledTimes(3) |
| 80 | + }) |
| 81 | + |
| 82 | + it('shows loading indicator while retry is in progress', async () => { |
| 83 | + render( |
| 84 | + <RouteErrorState |
| 85 | + title="Error" |
| 86 | + message="Failed." |
| 87 | + onRetry={() => {}} |
| 88 | + />, |
| 89 | + ) |
| 90 | + |
| 91 | + const button = screen.getByRole('button', { name: /retry/i }) |
| 92 | + expect(button).not.toBeDisabled() |
| 93 | + |
| 94 | + await act(async () => { button.click() }) |
| 95 | + |
| 96 | + expect(button).toBeDisabled() |
| 97 | + expect(screen.getByText('Retrying…')).toBeInTheDocument() |
| 98 | + }) |
| 99 | + |
| 100 | + it('renders back button when onBack is provided', () => { |
| 101 | + render( |
| 102 | + <RouteErrorState |
| 103 | + title="Error" |
| 104 | + message="Failed." |
| 105 | + onRetry={() => {}} |
| 106 | + onBack={() => {}} |
| 107 | + />, |
| 108 | + ) |
| 109 | + expect(screen.getByRole('button', { name: /back/i })).toBeInTheDocument() |
| 110 | + }) |
| 111 | + |
| 112 | + it('resets retry count after loading completes', async () => { |
| 113 | + const onRetry = vi.fn() |
| 114 | + const { rerender } = render( |
| 115 | + <RouteErrorState |
| 116 | + title="Error" |
| 117 | + message="Failed." |
| 118 | + onRetry={onRetry} |
| 119 | + loading={false} |
| 120 | + />, |
| 121 | + ) |
| 122 | + |
| 123 | + const button = screen.getByRole('button', { name: /retry/i }) |
| 124 | + |
| 125 | + await act(async () => { button.click() }) |
| 126 | + act(() => { vi.advanceTimersByTime(1000) }) |
| 127 | + expect(onRetry).toHaveBeenCalledTimes(1) |
| 128 | + |
| 129 | + rerender( |
| 130 | + <RouteErrorState |
| 131 | + title="Error" |
| 132 | + message="Failed." |
| 133 | + onRetry={onRetry} |
| 134 | + loading={true} |
| 135 | + />, |
| 136 | + ) |
| 137 | + |
| 138 | + rerender( |
| 139 | + <RouteErrorState |
| 140 | + title="Error" |
| 141 | + message="Failed." |
| 142 | + onRetry={onRetry} |
| 143 | + loading={false} |
| 144 | + />, |
| 145 | + ) |
| 146 | + |
| 147 | + act(() => { vi.advanceTimersByTime(5000) }) |
| 148 | + }) |
| 149 | +}) |
0 commit comments