|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, createRef } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { |
| 8 | + CodeSearchOverlay, |
| 9 | + type CodeSearchOverlayProps, |
| 10 | +} from '@/app/workspace/[workspaceId]/components/code-search-overlay/code-search-overlay' |
| 11 | + |
| 12 | +let host: HTMLDivElement |
| 13 | +let root: Root |
| 14 | +const inputRef = createRef<HTMLInputElement>() |
| 15 | + |
| 16 | +const callbacks = { |
| 17 | + onQueryChange: vi.fn(), |
| 18 | + onPrevious: vi.fn(), |
| 19 | + onNext: vi.fn(), |
| 20 | + onClose: vi.fn(), |
| 21 | +} |
| 22 | +const parentClick = vi.fn() |
| 23 | + |
| 24 | +function renderOverlay(props: Partial<CodeSearchOverlayProps> = {}) { |
| 25 | + act(() => |
| 26 | + root.render( |
| 27 | + <div onClick={parentClick}> |
| 28 | + <CodeSearchOverlay |
| 29 | + className='top-0 right-0' |
| 30 | + inputKind='chip' |
| 31 | + inputRef={inputRef} |
| 32 | + query='error' |
| 33 | + matchCount={3} |
| 34 | + currentMatchIndex={1} |
| 35 | + {...callbacks} |
| 36 | + {...props} |
| 37 | + /> |
| 38 | + </div> |
| 39 | + ) |
| 40 | + ) |
| 41 | + const overlay = host.firstElementChild?.firstElementChild as HTMLDivElement |
| 42 | + const input = overlay.querySelector('input') as HTMLInputElement |
| 43 | + return { overlay, input } |
| 44 | +} |
| 45 | + |
| 46 | +beforeEach(() => { |
| 47 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 48 | + vi.clearAllMocks() |
| 49 | + host = document.createElement('div') |
| 50 | + document.body.appendChild(host) |
| 51 | + root = createRoot(host) |
| 52 | +}) |
| 53 | + |
| 54 | +afterEach(() => { |
| 55 | + act(() => root.unmount()) |
| 56 | + host.remove() |
| 57 | +}) |
| 58 | + |
| 59 | +describe('CodeSearchOverlay', () => { |
| 60 | + it('shares the floating chrome and routes query, navigation, and close actions', () => { |
| 61 | + const { overlay, input } = renderOverlay() |
| 62 | + expect(overlay.className).toContain('h-[34px]') |
| 63 | + expect(overlay.className).toContain('rounded-sm bg-[var(--surface-1)]') |
| 64 | + expect(overlay.className).toContain('top-0 right-0') |
| 65 | + expect(overlay.getAttribute('role')).toBe('presentation') |
| 66 | + expect(inputRef.current).toBe(input) |
| 67 | + expect(input.getAttribute('aria-label')).toBe('Search code') |
| 68 | + expect(input.value).toBe('error') |
| 69 | + expect(overlay.textContent).toContain('2/3') |
| 70 | + |
| 71 | + const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set |
| 72 | + act(() => { |
| 73 | + setter?.call(input, 'failed') |
| 74 | + input.dispatchEvent(new Event('input', { bubbles: true })) |
| 75 | + }) |
| 76 | + expect(callbacks.onQueryChange).toHaveBeenCalledWith('failed') |
| 77 | + act(() => { |
| 78 | + overlay.querySelector<HTMLButtonElement>('[aria-label="Previous match"]')?.click() |
| 79 | + overlay.querySelector<HTMLButtonElement>('[aria-label="Next match"]')?.click() |
| 80 | + overlay.querySelector<HTMLButtonElement>('[aria-label="Close search"]')?.click() |
| 81 | + }) |
| 82 | + expect(callbacks.onPrevious).toHaveBeenCalledTimes(1) |
| 83 | + expect(callbacks.onNext).toHaveBeenCalledTimes(1) |
| 84 | + expect(callbacks.onClose).toHaveBeenCalledTimes(1) |
| 85 | + expect(parentClick).not.toHaveBeenCalled() |
| 86 | + }) |
| 87 | + |
| 88 | + it('keeps preview search compact in a floating overlay with a usable input ref', () => { |
| 89 | + const { overlay, input } = renderOverlay({ |
| 90 | + inputKind: 'plain', |
| 91 | + className: 'top-10 right-[8px]', |
| 92 | + }) |
| 93 | + expect(overlay.getAttribute('role')).toBe('presentation') |
| 94 | + expect(overlay.className).toContain('top-10 right-[8px]') |
| 95 | + expect(overlay.hasAttribute('data-toolbar-root')).toBe(false) |
| 96 | + expect(input.parentElement?.className).toContain('h-[23px]') |
| 97 | + expect(inputRef.current).toBe(input) |
| 98 | + |
| 99 | + const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set |
| 100 | + act(() => { |
| 101 | + setter?.call(input, 'preview') |
| 102 | + input.dispatchEvent(new Event('input', { bubbles: true })) |
| 103 | + overlay.querySelector<HTMLButtonElement>('[aria-label="Next match"]')?.click() |
| 104 | + }) |
| 105 | + expect(callbacks.onQueryChange).toHaveBeenCalledWith('preview') |
| 106 | + expect(callbacks.onNext).toHaveBeenCalledTimes(1) |
| 107 | + expect(parentClick).not.toHaveBeenCalled() |
| 108 | + }) |
| 109 | + |
| 110 | + it('retains the attached terminal edge, marker, wider tally, and disabled navigation', () => { |
| 111 | + const { overlay, input } = renderOverlay({ |
| 112 | + appearance: 'attached', |
| 113 | + inputKind: 'plain', |
| 114 | + className: 'top-[30px] right-[8px]', |
| 115 | + query: '', |
| 116 | + matchCount: 0, |
| 117 | + currentMatchIndex: 0, |
| 118 | + }) |
| 119 | + expect(overlay.className).toContain('rounded-b-[4px] border-t-0 bg-[var(--bg)]') |
| 120 | + expect(overlay.getAttribute('data-toolbar-root')).toBe('true') |
| 121 | + expect(overlay.getAttribute('data-search-active')).toBe('true') |
| 122 | + expect(overlay.hasAttribute('role')).toBe(false) |
| 123 | + expect(input.parentElement?.className).toContain('h-[23px]') |
| 124 | + expect(input.parentElement?.className).toContain('w-[94px]') |
| 125 | + expect(input.className).toContain('text-caption') |
| 126 | + expect(overlay.textContent).toContain('No results') |
| 127 | + expect(overlay.querySelector('span.w-\\[58px\\]')).not.toBeNull() |
| 128 | + const previous = overlay.querySelector<HTMLButtonElement>('[aria-label="Previous match"]') |
| 129 | + const next = overlay.querySelector<HTMLButtonElement>('[aria-label="Next match"]') |
| 130 | + const close = overlay.querySelector<HTMLButtonElement>('[aria-label="Close search"]') |
| 131 | + expect(previous?.disabled).toBe(true) |
| 132 | + expect(next?.disabled).toBe(true) |
| 133 | + expect(close?.disabled).toBe(false) |
| 134 | + expect(previous?.className).toContain('-m-1.5') |
| 135 | + expect(previous?.querySelector('svg')?.getAttribute('class')).toContain('size-[14px]') |
| 136 | + act(() => { |
| 137 | + previous?.click() |
| 138 | + next?.click() |
| 139 | + close?.click() |
| 140 | + }) |
| 141 | + expect(callbacks.onPrevious).not.toHaveBeenCalled() |
| 142 | + expect(callbacks.onNext).not.toHaveBeenCalled() |
| 143 | + expect(callbacks.onClose).toHaveBeenCalledTimes(1) |
| 144 | + }) |
| 145 | + |
| 146 | + it('shows the compact no-results tally for other code panels', () => { |
| 147 | + const { overlay } = renderOverlay({ matchCount: 0, currentMatchIndex: 0 }) |
| 148 | + expect(overlay.textContent).toContain('0/0') |
| 149 | + expect(overlay.getAttribute('data-toolbar-root')).toBeNull() |
| 150 | + expect( |
| 151 | + overlay.querySelector<HTMLButtonElement>('[aria-label="Previous match"]')?.disabled |
| 152 | + ).toBe(true) |
| 153 | + }) |
| 154 | +}) |
0 commit comments