|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { RetrySettings } from './retry-settings' |
| 8 | + |
| 9 | +const policy = { enabled: true as const, maxTries: 5, waitBetweenTriesMs: 2000 } |
| 10 | + |
| 11 | +let container: HTMLDivElement |
| 12 | +let root: Root |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + container = document.createElement('div') |
| 16 | + document.body.appendChild(container) |
| 17 | + root = createRoot(container) |
| 18 | +}) |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + act(() => root.unmount()) |
| 22 | + container.remove() |
| 23 | +}) |
| 24 | + |
| 25 | +function renderSettings(props: Partial<Parameters<typeof RetrySettings>[0]> = {}) { |
| 26 | + const onChange = vi.fn() |
| 27 | + act(() => { |
| 28 | + root.render(<RetrySettings retry={policy} disabled={false} onChange={onChange} {...props} />) |
| 29 | + }) |
| 30 | + return { onChange } |
| 31 | +} |
| 32 | + |
| 33 | +const field = (id: string) => container.querySelector<HTMLInputElement>(`#${id}`) |
| 34 | + |
| 35 | +describe('RetrySettings', () => { |
| 36 | + it('leaves a configured value alone when the field is blurred untouched', () => { |
| 37 | + const { onChange } = renderSettings() |
| 38 | + const maxTries = field('block-retry-max-tries')! |
| 39 | + expect(maxTries.value).toBe('5') |
| 40 | + |
| 41 | + act(() => { |
| 42 | + maxTries.dispatchEvent(new FocusEvent('focusout', { bubbles: true })) |
| 43 | + }) |
| 44 | + |
| 45 | + expect(onChange).not.toHaveBeenCalled() |
| 46 | + expect(field('block-retry-max-tries')!.value).toBe('5') |
| 47 | + }) |
| 48 | + |
| 49 | + it('renders only the switch while retry is off', () => { |
| 50 | + renderSettings({ retry: { ...policy, enabled: false } }) |
| 51 | + |
| 52 | + expect(field('block-retry-enabled')).not.toBeNull() |
| 53 | + expect(field('block-retry-max-tries')).toBeNull() |
| 54 | + }) |
| 55 | +}) |
0 commit comments