|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, type ReactNode } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { ChipConfirmModal, ChipModal, ChipModalFooter, ChipModalHeader } from './chip-modal' |
| 8 | + |
| 9 | +vi.mock('next/navigation', () => ({ |
| 10 | + usePathname: () => '/workspace/workspace-1/home', |
| 11 | +})) |
| 12 | + |
| 13 | +let root: Root | null = null |
| 14 | +let container: HTMLDivElement | null = null |
| 15 | + |
| 16 | +function mount(ui: ReactNode) { |
| 17 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 18 | + container = document.createElement('div') |
| 19 | + document.body.appendChild(container) |
| 20 | + root = createRoot(container) |
| 21 | + act(() => root?.render(ui)) |
| 22 | +} |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + if (root) act(() => root?.unmount()) |
| 26 | + container?.remove() |
| 27 | + root = null |
| 28 | + container = null |
| 29 | +}) |
| 30 | + |
| 31 | +/** The dialog panel Radix renders, which owns the Escape/outside-click handlers. */ |
| 32 | +function dialog(): HTMLElement { |
| 33 | + const node = document.querySelector<HTMLElement>('[role="dialog"]') |
| 34 | + if (!node) throw new Error('Dialog did not render') |
| 35 | + return node |
| 36 | +} |
| 37 | + |
| 38 | +function buttonByText(text: string): HTMLButtonElement { |
| 39 | + const match = Array.from(document.querySelectorAll('button')).find((button) => |
| 40 | + button.textContent?.includes(text) |
| 41 | + ) |
| 42 | + if (!match) throw new Error(`No button containing "${text}"`) |
| 43 | + return match as HTMLButtonElement |
| 44 | +} |
| 45 | + |
| 46 | +function closeButton(): HTMLButtonElement { |
| 47 | + const match = Array.from(document.querySelectorAll('button')).find((button) => |
| 48 | + button.querySelector('.sr-only')?.textContent?.includes('Close') |
| 49 | + ) |
| 50 | + if (!match) throw new Error('Close button did not render') |
| 51 | + return match as HTMLButtonElement |
| 52 | +} |
| 53 | + |
| 54 | +function pressEscape() { |
| 55 | + act(() => { |
| 56 | + dialog().dispatchEvent( |
| 57 | + new KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true }) |
| 58 | + ) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +function Harness({ |
| 63 | + onOpenChange, |
| 64 | + dismissDisabled, |
| 65 | +}: { |
| 66 | + onOpenChange: (open: boolean) => void |
| 67 | + dismissDisabled?: boolean |
| 68 | +}) { |
| 69 | + return ( |
| 70 | + <ChipModal |
| 71 | + open |
| 72 | + onOpenChange={onOpenChange} |
| 73 | + srTitle='Test modal' |
| 74 | + dismissDisabled={dismissDisabled} |
| 75 | + > |
| 76 | + <ChipModalHeader onClose={() => onOpenChange(false)}>Title</ChipModalHeader> |
| 77 | + <ChipModalFooter |
| 78 | + onCancel={() => onOpenChange(false)} |
| 79 | + primaryAction={{ label: 'Save', onClick: () => {} }} |
| 80 | + /> |
| 81 | + </ChipModal> |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +describe('ChipModal dismissDisabled', () => { |
| 86 | + it('closes through every path when not set', () => { |
| 87 | + const onOpenChange = vi.fn() |
| 88 | + mount(<Harness onOpenChange={onOpenChange} />) |
| 89 | + |
| 90 | + expect(closeButton().disabled).toBe(false) |
| 91 | + expect(buttonByText('Cancel').disabled).toBe(false) |
| 92 | + |
| 93 | + pressEscape() |
| 94 | + expect(onOpenChange).toHaveBeenCalledWith(false) |
| 95 | + }) |
| 96 | + |
| 97 | + // Outside-click is guarded by the same flag but jsdom cannot drive Radix's |
| 98 | + // outside-interaction path, so asserting it here could never fail. |
| 99 | + it('blocks the close button, Cancel and Escape when set', () => { |
| 100 | + const onOpenChange = vi.fn() |
| 101 | + mount(<Harness onOpenChange={onOpenChange} dismissDisabled />) |
| 102 | + |
| 103 | + expect(closeButton().disabled).toBe(true) |
| 104 | + expect(buttonByText('Cancel').disabled).toBe(true) |
| 105 | + |
| 106 | + pressEscape() |
| 107 | + expect(onOpenChange).not.toHaveBeenCalled() |
| 108 | + }) |
| 109 | + |
| 110 | + // Either flag disables: an explicit `false` must not re-enable a button whose |
| 111 | + // click Radix has already been told to ignore. |
| 112 | + it('cannot be re-enabled by an explicit closeDisabled or cancelDisabled of false', () => { |
| 113 | + const onOpenChange = vi.fn() |
| 114 | + mount( |
| 115 | + <ChipModal open onOpenChange={onOpenChange} srTitle='Test modal' dismissDisabled> |
| 116 | + <ChipModalHeader onClose={() => onOpenChange(false)} closeDisabled={false}> |
| 117 | + Title |
| 118 | + </ChipModalHeader> |
| 119 | + <ChipModalFooter |
| 120 | + onCancel={() => onOpenChange(false)} |
| 121 | + cancelDisabled={false} |
| 122 | + primaryAction={{ label: 'Save', onClick: () => {} }} |
| 123 | + /> |
| 124 | + </ChipModal> |
| 125 | + ) |
| 126 | + |
| 127 | + expect(closeButton().disabled).toBe(true) |
| 128 | + expect(buttonByText('Cancel').disabled).toBe(true) |
| 129 | + }) |
| 130 | + |
| 131 | + it('still lets an explicit true disable a button on its own', () => { |
| 132 | + const onOpenChange = vi.fn() |
| 133 | + mount( |
| 134 | + <ChipModal open onOpenChange={onOpenChange} srTitle='Test modal'> |
| 135 | + <ChipModalHeader onClose={() => onOpenChange(false)} closeDisabled> |
| 136 | + Title |
| 137 | + </ChipModalHeader> |
| 138 | + <ChipModalFooter |
| 139 | + onCancel={() => onOpenChange(false)} |
| 140 | + primaryAction={{ label: 'Save', onClick: () => {} }} |
| 141 | + /> |
| 142 | + </ChipModal> |
| 143 | + ) |
| 144 | + |
| 145 | + expect(closeButton().disabled).toBe(true) |
| 146 | + expect(buttonByText('Cancel').disabled).toBe(false) |
| 147 | + }) |
| 148 | +}) |
| 149 | + |
| 150 | +describe('ChipConfirmModal pending', () => { |
| 151 | + it('holds every exit shut while the confirm runs', () => { |
| 152 | + const onOpenChange = vi.fn() |
| 153 | + mount( |
| 154 | + <ChipConfirmModal |
| 155 | + open |
| 156 | + onOpenChange={onOpenChange} |
| 157 | + title='Delete key' |
| 158 | + text='This cannot be undone.' |
| 159 | + confirm={{ label: 'Delete', onClick: () => {}, pending: true, pendingLabel: 'Deleting...' }} |
| 160 | + /> |
| 161 | + ) |
| 162 | + |
| 163 | + expect(closeButton().disabled).toBe(true) |
| 164 | + expect(buttonByText('Cancel').disabled).toBe(true) |
| 165 | + expect(buttonByText('Deleting...').disabled).toBe(true) |
| 166 | + |
| 167 | + pressEscape() |
| 168 | + expect(onOpenChange).not.toHaveBeenCalled() |
| 169 | + }) |
| 170 | + |
| 171 | + it('dismisses normally when the confirm is idle', () => { |
| 172 | + const onOpenChange = vi.fn() |
| 173 | + mount( |
| 174 | + <ChipConfirmModal |
| 175 | + open |
| 176 | + onOpenChange={onOpenChange} |
| 177 | + title='Delete key' |
| 178 | + text='This cannot be undone.' |
| 179 | + confirm={{ label: 'Delete', onClick: () => {} }} |
| 180 | + /> |
| 181 | + ) |
| 182 | + |
| 183 | + expect(closeButton().disabled).toBe(false) |
| 184 | + act(() => closeButton().click()) |
| 185 | + expect(onOpenChange).toHaveBeenCalledWith(false) |
| 186 | + }) |
| 187 | +}) |
0 commit comments