|
| 1 | +/* PauseResumeButton.test.tsx — unit coverage for the pause/resume toggle. |
| 2 | + * |
| 3 | + * Covers: |
| 4 | + * - Label flips based on resource.status |
| 5 | + * - Click opens the confirmation modal (no accidental auto-confirm) |
| 6 | + * - Confirm in modal calls api.pauseResource / api.resumeResource by status |
| 7 | + * - 402 swaps the modal body for the inline UpgradeButton CTA |
| 8 | + * - 500 surfaces an inline error and leaves the modal open |
| 9 | + * - Terminal statuses (expired/tombstoned/deleted) render nothing |
| 10 | + * |
| 11 | + * We mock the api module so no real fetch goes out and so we can control |
| 12 | + * exactly which promise the button awaits. */ |
| 13 | + |
| 14 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 15 | +import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react' |
| 16 | +import type { Resource } from '../api' |
| 17 | + |
| 18 | +vi.mock('../api', async () => { |
| 19 | + const actual = await vi.importActual<typeof import('../api')>('../api') |
| 20 | + return { |
| 21 | + ...actual, |
| 22 | + pauseResource: vi.fn(), |
| 23 | + resumeResource: vi.fn(), |
| 24 | + reportExperimentConverted: vi.fn(), |
| 25 | + } |
| 26 | +}) |
| 27 | + |
| 28 | +import * as api from '../api' |
| 29 | +import { PauseResumeButton } from './PauseResumeButton' |
| 30 | + |
| 31 | +const baseResource: Resource = { |
| 32 | + id: 'res_abc123', |
| 33 | + token: 'tok_abc123', |
| 34 | + resource_type: 'postgres', |
| 35 | + tier: 'hobby', |
| 36 | + status: 'active', |
| 37 | + name: 'orders-db', |
| 38 | + env: 'production', |
| 39 | + storage_bytes: 1_000_000, |
| 40 | + storage_limit_bytes: 500_000_000, |
| 41 | + storage_exceeded: false, |
| 42 | + connections_in_use: 1, |
| 43 | + connections_limit: 5, |
| 44 | + cloud_vendor: 'aws', |
| 45 | + country_code: 'IN', |
| 46 | + expires_at: null, |
| 47 | + created_at: '2026-05-01T00:00:00Z', |
| 48 | +} |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + ;(api.pauseResource as any).mockReset() |
| 52 | + ;(api.resumeResource as any).mockReset() |
| 53 | + ;(api.reportExperimentConverted as any).mockReset() |
| 54 | + ;(api.reportExperimentConverted as any).mockResolvedValue(undefined) |
| 55 | +}) |
| 56 | + |
| 57 | +afterEach(() => { |
| 58 | + cleanup() |
| 59 | +}) |
| 60 | + |
| 61 | +describe('PauseResumeButton — label per status', () => { |
| 62 | + it('renders "Pause" when status is active', () => { |
| 63 | + render(<PauseResumeButton resource={baseResource} onUpdated={() => {}} />) |
| 64 | + const btn = screen.getByTestId('pause-resume-button') |
| 65 | + expect(btn.textContent).toBe('Pause') |
| 66 | + expect(btn.getAttribute('data-action')).toBe('pause') |
| 67 | + }) |
| 68 | + |
| 69 | + it('renders "Resume" when status is paused', () => { |
| 70 | + const paused: Resource = { ...baseResource, status: 'paused' } |
| 71 | + render(<PauseResumeButton resource={paused} onUpdated={() => {}} />) |
| 72 | + const btn = screen.getByTestId('pause-resume-button') |
| 73 | + expect(btn.textContent).toBe('Resume') |
| 74 | + expect(btn.getAttribute('data-action')).toBe('resume') |
| 75 | + }) |
| 76 | + |
| 77 | + it('renders nothing for expired resources', () => { |
| 78 | + const expired: Resource = { ...baseResource, status: 'expired' } |
| 79 | + const { container } = render( |
| 80 | + <PauseResumeButton resource={expired} onUpdated={() => {}} />, |
| 81 | + ) |
| 82 | + expect(container.firstChild).toBeNull() |
| 83 | + }) |
| 84 | + |
| 85 | + it('renders nothing for tombstoned resources', () => { |
| 86 | + const t: Resource = { ...baseResource, status: 'tombstoned' } |
| 87 | + const { container } = render(<PauseResumeButton resource={t} onUpdated={() => {}} />) |
| 88 | + expect(container.firstChild).toBeNull() |
| 89 | + }) |
| 90 | + |
| 91 | + it('renders nothing for deleted resources', () => { |
| 92 | + const d: Resource = { ...baseResource, status: 'deleted' } |
| 93 | + const { container } = render(<PauseResumeButton resource={d} onUpdated={() => {}} />) |
| 94 | + expect(container.firstChild).toBeNull() |
| 95 | + }) |
| 96 | +}) |
| 97 | + |
| 98 | +describe('PauseResumeButton — modal flow', () => { |
| 99 | + it('does NOT call the api on first click — opens the modal instead', async () => { |
| 100 | + render(<PauseResumeButton resource={baseResource} onUpdated={() => {}} />) |
| 101 | + fireEvent.click(screen.getByTestId('pause-resume-button')) |
| 102 | + await waitFor(() => { |
| 103 | + expect(screen.getByTestId('pause-resume-modal')).toBeTruthy() |
| 104 | + }) |
| 105 | + expect(api.pauseResource).not.toHaveBeenCalled() |
| 106 | + expect(api.resumeResource).not.toHaveBeenCalled() |
| 107 | + }) |
| 108 | + |
| 109 | + it('confirming on an active resource calls api.pauseResource(id)', async () => { |
| 110 | + const onUpdated = vi.fn() |
| 111 | + ;(api.pauseResource as any).mockResolvedValue({ |
| 112 | + ok: true, |
| 113 | + resource: { ...baseResource, status: 'paused' }, |
| 114 | + }) |
| 115 | + render(<PauseResumeButton resource={baseResource} onUpdated={onUpdated} />) |
| 116 | + fireEvent.click(screen.getByTestId('pause-resume-button')) |
| 117 | + fireEvent.click(screen.getByTestId('pause-resume-confirm')) |
| 118 | + await waitFor(() => { |
| 119 | + expect(api.pauseResource).toHaveBeenCalledWith('res_abc123') |
| 120 | + }) |
| 121 | + expect(api.resumeResource).not.toHaveBeenCalled() |
| 122 | + await waitFor(() => { |
| 123 | + expect(onUpdated).toHaveBeenCalledWith( |
| 124 | + expect.objectContaining({ status: 'paused' }), |
| 125 | + ) |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + it('confirming on a paused resource calls api.resumeResource(id)', async () => { |
| 130 | + const onUpdated = vi.fn() |
| 131 | + const paused: Resource = { ...baseResource, status: 'paused' } |
| 132 | + ;(api.resumeResource as any).mockResolvedValue({ |
| 133 | + ok: true, |
| 134 | + resource: { ...baseResource, status: 'active' }, |
| 135 | + }) |
| 136 | + render(<PauseResumeButton resource={paused} onUpdated={onUpdated} />) |
| 137 | + fireEvent.click(screen.getByTestId('pause-resume-button')) |
| 138 | + fireEvent.click(screen.getByTestId('pause-resume-confirm')) |
| 139 | + await waitFor(() => { |
| 140 | + expect(api.resumeResource).toHaveBeenCalledWith('res_abc123') |
| 141 | + }) |
| 142 | + expect(api.pauseResource).not.toHaveBeenCalled() |
| 143 | + await waitFor(() => { |
| 144 | + expect(onUpdated).toHaveBeenCalledWith( |
| 145 | + expect.objectContaining({ status: 'active' }), |
| 146 | + ) |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + it('Cancel button closes the modal without calling the api', async () => { |
| 151 | + render(<PauseResumeButton resource={baseResource} onUpdated={() => {}} />) |
| 152 | + fireEvent.click(screen.getByTestId('pause-resume-button')) |
| 153 | + expect(screen.getByTestId('pause-resume-modal')).toBeTruthy() |
| 154 | + fireEvent.click(screen.getByTestId('pause-resume-cancel')) |
| 155 | + await waitFor(() => { |
| 156 | + expect(screen.queryByTestId('pause-resume-modal')).toBeNull() |
| 157 | + }) |
| 158 | + expect(api.pauseResource).not.toHaveBeenCalled() |
| 159 | + }) |
| 160 | +}) |
| 161 | + |
| 162 | +describe('PauseResumeButton — tier-wall (402) handling', () => { |
| 163 | + it('on 402 swaps the modal body for the upgrade CTA and does NOT call onUpdated', async () => { |
| 164 | + const onUpdated = vi.fn() |
| 165 | + const tierErr = Object.assign(new Error('pro tier required'), { |
| 166 | + status: 402, |
| 167 | + code: 'agent_action', |
| 168 | + }) |
| 169 | + ;(api.pauseResource as any).mockRejectedValue(tierErr) |
| 170 | + render(<PauseResumeButton resource={baseResource} onUpdated={onUpdated} />) |
| 171 | + fireEvent.click(screen.getByTestId('pause-resume-button')) |
| 172 | + fireEvent.click(screen.getByTestId('pause-resume-confirm')) |
| 173 | + await waitFor(() => { |
| 174 | + expect(screen.getByTestId('pause-resume-upgrade')).toBeTruthy() |
| 175 | + }) |
| 176 | + // The upgrade CTA itself is the UpgradeButton — surfaced under its |
| 177 | + // own data-testid (pause-resume-upgrade-cta). |
| 178 | + expect(screen.getByTestId('pause-resume-upgrade-cta')).toBeTruthy() |
| 179 | + expect(onUpdated).not.toHaveBeenCalled() |
| 180 | + // No inline error in the 402 path — the tier-wall replaces the entire |
| 181 | + // action row. |
| 182 | + expect(screen.queryByTestId('pause-resume-error')).toBeNull() |
| 183 | + }) |
| 184 | +}) |
| 185 | + |
| 186 | +describe('PauseResumeButton — generic error (5xx / network)', () => { |
| 187 | + it('on 500 surfaces an inline error and leaves the modal open', async () => { |
| 188 | + const onUpdated = vi.fn() |
| 189 | + const serverErr = Object.assign(new Error('upstream timeout'), { |
| 190 | + status: 500, |
| 191 | + code: 'http_500', |
| 192 | + }) |
| 193 | + ;(api.pauseResource as any).mockRejectedValue(serverErr) |
| 194 | + render(<PauseResumeButton resource={baseResource} onUpdated={onUpdated} />) |
| 195 | + fireEvent.click(screen.getByTestId('pause-resume-button')) |
| 196 | + fireEvent.click(screen.getByTestId('pause-resume-confirm')) |
| 197 | + await waitFor(() => { |
| 198 | + const err = screen.getByTestId('pause-resume-error') |
| 199 | + expect(err).toBeTruthy() |
| 200 | + expect(err.textContent).toMatch(/upstream timeout/) |
| 201 | + }) |
| 202 | + // Modal stays open so the user can retry. |
| 203 | + expect(screen.getByTestId('pause-resume-modal')).toBeTruthy() |
| 204 | + expect(onUpdated).not.toHaveBeenCalled() |
| 205 | + // Confirm button is re-enabled after the error so a retry is possible. |
| 206 | + const confirm = screen.getByTestId('pause-resume-confirm') as HTMLButtonElement |
| 207 | + expect(confirm.disabled).toBe(false) |
| 208 | + }) |
| 209 | + |
| 210 | + it('on a thrown network error (no status) still surfaces the message', async () => { |
| 211 | + const netErr = new Error('Failed to fetch') |
| 212 | + ;(api.pauseResource as any).mockRejectedValue(netErr) |
| 213 | + render(<PauseResumeButton resource={baseResource} onUpdated={() => {}} />) |
| 214 | + fireEvent.click(screen.getByTestId('pause-resume-button')) |
| 215 | + fireEvent.click(screen.getByTestId('pause-resume-confirm')) |
| 216 | + await waitFor(() => { |
| 217 | + expect(screen.getByTestId('pause-resume-error').textContent).toMatch( |
| 218 | + /Failed to fetch/, |
| 219 | + ) |
| 220 | + }) |
| 221 | + }) |
| 222 | +}) |
0 commit comments