|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, createRef, type Ref } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import type { ColumnDefinition, TablePredicate } from '@/lib/table' |
| 8 | +import { |
| 9 | + FILTER_DEBOUNCE_MS, |
| 10 | + TableFilter, |
| 11 | + type TableFilterHandle, |
| 12 | +} from '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-filter/table-filter' |
| 13 | + |
| 14 | +const COLUMNS: ColumnDefinition[] = [{ id: 'col-name', name: 'Name', type: 'string' }] |
| 15 | + |
| 16 | +let container: HTMLDivElement |
| 17 | +let root: Root |
| 18 | + |
| 19 | +beforeEach(() => { |
| 20 | + globalThis.IS_REACT_ACT_ENVIRONMENT = true |
| 21 | + vi.useFakeTimers() |
| 22 | + container = document.createElement('div') |
| 23 | + document.body.appendChild(container) |
| 24 | + root = createRoot(container) |
| 25 | +}) |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + act(() => root.unmount()) |
| 29 | + container.remove() |
| 30 | + vi.useRealTimers() |
| 31 | +}) |
| 32 | + |
| 33 | +function renderFilter( |
| 34 | + onChange: (filter: TablePredicate | null) => void, |
| 35 | + filter: TablePredicate | null = null, |
| 36 | + ref?: Ref<TableFilterHandle> |
| 37 | +) { |
| 38 | + act(() => { |
| 39 | + root.render(<TableFilter ref={ref} columns={COLUMNS} filter={filter} onChange={onChange} />) |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +describe('TableFilter', () => { |
| 44 | + it('applies text filters after a short typing delay', () => { |
| 45 | + const onApply = vi.fn() |
| 46 | + renderFilter(onApply) |
| 47 | + const input = container.querySelector<HTMLInputElement>('input[placeholder="Enter a value"]') |
| 48 | + expect(input).not.toBeNull() |
| 49 | + |
| 50 | + act(() => { |
| 51 | + if (!input) return |
| 52 | + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set?.call(input, 'Ada') |
| 53 | + input.dispatchEvent(new Event('input', { bubbles: true })) |
| 54 | + }) |
| 55 | + |
| 56 | + expect(onApply).not.toHaveBeenCalled() |
| 57 | + act(() => vi.advanceTimersByTime(FILTER_DEBOUNCE_MS - 1)) |
| 58 | + expect(onApply).not.toHaveBeenCalled() |
| 59 | + act(() => vi.advanceTimersByTime(1)) |
| 60 | + expect(onApply).toHaveBeenCalledWith({ |
| 61 | + all: [{ field: 'col-name', op: 'eq', value: 'Ada' }], |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + it('uses fixed AND conjunctions without apply or clear actions', () => { |
| 66 | + renderFilter(vi.fn()) |
| 67 | + const addFilter = Array.from(container.querySelectorAll('button')).find((button) => |
| 68 | + button.textContent?.includes('Add filter') |
| 69 | + ) |
| 70 | + |
| 71 | + act(() => addFilter?.click()) |
| 72 | + |
| 73 | + const conjunction = Array.from(container.querySelectorAll('*')).find( |
| 74 | + (element) => element.textContent?.trim() === 'and' |
| 75 | + ) |
| 76 | + expect(conjunction).toBeDefined() |
| 77 | + expect(conjunction?.closest('button')).toBeNull() |
| 78 | + expect(container.textContent).not.toContain('Apply filter') |
| 79 | + expect(container.textContent).not.toContain('Clear filters') |
| 80 | + }) |
| 81 | + |
| 82 | + it('flushes the pending filter when the panel closes before the delay', () => { |
| 83 | + const onChange = vi.fn() |
| 84 | + const filterRef = createRef<TableFilterHandle>() |
| 85 | + renderFilter(onChange, null, filterRef) |
| 86 | + const input = container.querySelector<HTMLInputElement>('input[placeholder="Enter a value"]') |
| 87 | + |
| 88 | + act(() => { |
| 89 | + if (!input) return |
| 90 | + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set?.call(input, 'Ada') |
| 91 | + input.dispatchEvent(new Event('input', { bubbles: true })) |
| 92 | + }) |
| 93 | + act(() => { |
| 94 | + filterRef.current?.flush() |
| 95 | + }) |
| 96 | + |
| 97 | + expect(onChange).toHaveBeenCalledTimes(1) |
| 98 | + expect(onChange).toHaveBeenCalledWith({ |
| 99 | + all: [{ field: 'col-name', op: 'eq', value: 'Ada' }], |
| 100 | + }) |
| 101 | + act(() => vi.advanceTimersByTime(FILTER_DEBOUNCE_MS)) |
| 102 | + expect(onChange).toHaveBeenCalledTimes(1) |
| 103 | + }) |
| 104 | + |
| 105 | + it('cancels the previous debounce when typing continues', () => { |
| 106 | + const onChange = vi.fn() |
| 107 | + renderFilter(onChange) |
| 108 | + const input = container.querySelector<HTMLInputElement>('input[placeholder="Enter a value"]') |
| 109 | + const setInput = (value: string) => { |
| 110 | + if (!input) return |
| 111 | + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set?.call(input, value) |
| 112 | + input.dispatchEvent(new Event('input', { bubbles: true })) |
| 113 | + } |
| 114 | + |
| 115 | + act(() => setInput('Ada')) |
| 116 | + act(() => vi.advanceTimersByTime(FILTER_DEBOUNCE_MS - 1)) |
| 117 | + act(() => setInput('Grace')) |
| 118 | + act(() => vi.advanceTimersByTime(FILTER_DEBOUNCE_MS)) |
| 119 | + |
| 120 | + expect(onChange).toHaveBeenCalledTimes(1) |
| 121 | + expect(onChange).toHaveBeenCalledWith({ |
| 122 | + all: [{ field: 'col-name', op: 'eq', value: 'Grace' }], |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + it('clears the active filter when its last rule is removed', () => { |
| 127 | + const onChange = vi.fn() |
| 128 | + renderFilter(onChange, { |
| 129 | + all: [{ field: 'col-name', op: 'eq', value: 'Ada' }], |
| 130 | + }) |
| 131 | + |
| 132 | + const removeButton = container.querySelector<HTMLButtonElement>( |
| 133 | + 'button[aria-label="Remove filter"]' |
| 134 | + ) |
| 135 | + act(() => removeButton?.click()) |
| 136 | + act(() => vi.advanceTimersByTime(FILTER_DEBOUNCE_MS)) |
| 137 | + |
| 138 | + expect(onChange).toHaveBeenCalledWith(null) |
| 139 | + expect( |
| 140 | + container.querySelector<HTMLInputElement>('input[placeholder="Enter a value"]')?.value |
| 141 | + ).toBe('') |
| 142 | + }) |
| 143 | + |
| 144 | + it('normalizes a previously saved OR filter to AND', () => { |
| 145 | + const onChange = vi.fn() |
| 146 | + renderFilter(onChange, { |
| 147 | + any: [ |
| 148 | + { all: [{ field: 'col-name', op: 'eq', value: 'Ada' }] }, |
| 149 | + { all: [{ field: 'col-name', op: 'eq', value: 'Grace' }] }, |
| 150 | + ], |
| 151 | + }) |
| 152 | + |
| 153 | + act(() => vi.advanceTimersByTime(FILTER_DEBOUNCE_MS)) |
| 154 | + |
| 155 | + expect(onChange).toHaveBeenCalledWith({ |
| 156 | + all: [ |
| 157 | + { field: 'col-name', op: 'eq', value: 'Ada' }, |
| 158 | + { field: 'col-name', op: 'eq', value: 'Grace' }, |
| 159 | + ], |
| 160 | + }) |
| 161 | + }) |
| 162 | +}) |
0 commit comments