|
| 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 type { DisplayColumn } from '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/types' |
| 8 | + |
| 9 | +vi.mock('@sim/emcn', () => ({ |
| 10 | + Badge: ({ children }: { children: React.ReactNode }) => <span>{children}</span>, |
| 11 | + Checkbox: () => null, |
| 12 | + Chip: ({ children, ...props }: React.ButtonHTMLAttributes<HTMLButtonElement>) => ( |
| 13 | + <button {...props}>{children}</button> |
| 14 | + ), |
| 15 | + cn: (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(' '), |
| 16 | + Tooltip: { |
| 17 | + Root: ({ children }: { children: React.ReactNode }) => children, |
| 18 | + Trigger: ({ children }: { children: React.ReactNode }) => children, |
| 19 | + Content: ({ children }: { children: React.ReactNode }) => children, |
| 20 | + }, |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/app/workspace/[workspaceId]/logs/utils', () => ({ |
| 24 | + StatusBadge: () => null, |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock( |
| 28 | + '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/cells/sim-resource-cell', |
| 29 | + () => ({ SimResourceCell: () => null }) |
| 30 | +) |
| 31 | + |
| 32 | +vi.mock('@/app/workspace/[workspaceId]/tables/[tableId]/components/select-field', () => ({ |
| 33 | + resolveSelectOptions: () => [], |
| 34 | + SelectPill: () => null, |
| 35 | +})) |
| 36 | + |
| 37 | +import { |
| 38 | + CellRender, |
| 39 | + resolveCellRender, |
| 40 | +} from '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/cells/cell-render' |
| 41 | + |
| 42 | +const REFERENCE_COLUMN: DisplayColumn = { |
| 43 | + id: 'col-account', |
| 44 | + key: 'col-account', |
| 45 | + name: 'Account', |
| 46 | + type: 'reference', |
| 47 | + referenceTableId: 'table-accounts', |
| 48 | + groupSize: 1, |
| 49 | + groupStartColIndex: 0, |
| 50 | + headerLabel: 'Account', |
| 51 | + isGroupStart: true, |
| 52 | +} |
| 53 | + |
| 54 | +let container: HTMLDivElement |
| 55 | +let root: Root |
| 56 | + |
| 57 | +beforeEach(() => { |
| 58 | + globalThis.IS_REACT_ACT_ENVIRONMENT = true |
| 59 | + container = document.createElement('div') |
| 60 | + document.body.appendChild(container) |
| 61 | + act(() => { |
| 62 | + root = createRoot(container) |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +afterEach(() => { |
| 67 | + act(() => root.unmount()) |
| 68 | + container.remove() |
| 69 | +}) |
| 70 | + |
| 71 | +describe('reference cell rendering', () => { |
| 72 | + it('resolves a stored row ID to a chip labeled with the reference column name', () => { |
| 73 | + expect( |
| 74 | + resolveCellRender({ |
| 75 | + value: 'row-account-1', |
| 76 | + exec: undefined, |
| 77 | + column: REFERENCE_COLUMN, |
| 78 | + waitingOnLabels: undefined, |
| 79 | + }) |
| 80 | + ).toMatchObject({ kind: 'column-chip', label: 'Account' }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('keeps an empty reference cell empty', () => { |
| 84 | + expect( |
| 85 | + resolveCellRender({ |
| 86 | + value: '', |
| 87 | + exec: undefined, |
| 88 | + column: REFERENCE_COLUMN, |
| 89 | + waitingOnLabels: undefined, |
| 90 | + }) |
| 91 | + ).toEqual({ kind: 'empty' }) |
| 92 | + }) |
| 93 | + |
| 94 | + it('opens the referenced row from the chip without exposing its stored row ID', () => { |
| 95 | + const onReferenceClick = vi.fn() |
| 96 | + |
| 97 | + act(() => { |
| 98 | + root.render( |
| 99 | + <CellRender |
| 100 | + kind={resolveCellRender({ |
| 101 | + value: 'row-account-1', |
| 102 | + exec: undefined, |
| 103 | + column: REFERENCE_COLUMN, |
| 104 | + waitingOnLabels: undefined, |
| 105 | + })} |
| 106 | + isEditing={false} |
| 107 | + referenceAction={{ expanded: false, onClick: onReferenceClick }} |
| 108 | + /> |
| 109 | + ) |
| 110 | + }) |
| 111 | + |
| 112 | + const chip = container.querySelector('button') |
| 113 | + expect(chip?.textContent).toBe('Account') |
| 114 | + |
| 115 | + act(() => chip?.click()) |
| 116 | + |
| 117 | + expect(onReferenceClick).toHaveBeenCalledOnce() |
| 118 | + expect(container.textContent).not.toContain('row-account-1') |
| 119 | + }) |
| 120 | +}) |
0 commit comments