|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, createElement, type ReactNode } from 'react' |
| 5 | +import { createRoot } from 'react-dom/client' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import type { ColumnDefinition } from '@/lib/table' |
| 8 | +import { |
| 9 | + dateEditorRawValue, |
| 10 | + InlineEditor, |
| 11 | +} from '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/cells/inline-editors' |
| 12 | +import { cleanCellValue } from '@/app/workspace/[workspaceId]/tables/[tableId]/utils' |
| 13 | + |
| 14 | +const { mockUseTimezone } = vi.hoisted(() => ({ mockUseTimezone: vi.fn() })) |
| 15 | + |
| 16 | +vi.mock('@/hooks/queries/general-settings', () => ({ useTimezone: mockUseTimezone })) |
| 17 | +vi.mock('@sim/emcn', () => { |
| 18 | + const passthrough = ({ children }: { children?: ReactNode }) => children ?? null |
| 19 | + return { |
| 20 | + Calendar: () => null, |
| 21 | + cn: (...classes: unknown[]) => classes.filter(Boolean).join(' '), |
| 22 | + DropdownMenu: passthrough, |
| 23 | + DropdownMenuContent: passthrough, |
| 24 | + DropdownMenuItem: passthrough, |
| 25 | + DropdownMenuTrigger: passthrough, |
| 26 | + Popover: passthrough, |
| 27 | + PopoverAnchor: () => null, |
| 28 | + PopoverContent: passthrough, |
| 29 | + toast: { error: vi.fn() }, |
| 30 | + } |
| 31 | +}) |
| 32 | +const column = (type: ColumnDefinition['type']): ColumnDefinition => ({ name: 'expires_at', type }) |
| 33 | + |
| 34 | +describe('dateEditorRawValue', () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks() |
| 37 | + mockUseTimezone.mockReturnValue('America/Los_Angeles') |
| 38 | + }) |
| 39 | + |
| 40 | + it('leaves TTL drafts for TTL coercion to resolve safely', () => { |
| 41 | + const ttlColumn = column('ttl') |
| 42 | + const timezone = 'America/New_York' |
| 43 | + const repeatedWallClock = '11/01/2026 1:30:00 AM' |
| 44 | + |
| 45 | + const repeatedRaw = dateEditorRawValue(repeatedWallClock, ttlColumn, timezone) |
| 46 | + expect(repeatedRaw).toBe(repeatedWallClock) |
| 47 | + expect(cleanCellValue(repeatedRaw, ttlColumn, timezone)).toBe( |
| 48 | + Date.parse('2026-11-01T06:30:00Z') / 1000 |
| 49 | + ) |
| 50 | + |
| 51 | + const fractionalRaw = dateEditorRawValue('2023-11-14t22:13:20.001Z', ttlColumn, timezone) |
| 52 | + expect(cleanCellValue(fractionalRaw, ttlColumn, timezone)).toBe(1_700_000_001) |
| 53 | + }) |
| 54 | + |
| 55 | + it('keeps ordinary date drafts on their existing display parser', () => { |
| 56 | + expect(dateEditorRawValue('11/01/2026 1:30:00 AM', column('date'), 'America/New_York')).toBe( |
| 57 | + '2026-11-01T01:30:00-04:00' |
| 58 | + ) |
| 59 | + }) |
| 60 | + |
| 61 | + it('keeps an open TTL edit in its starting timezone when the setting changes', () => { |
| 62 | + const container = document.createElement('div') |
| 63 | + document.body.appendChild(container) |
| 64 | + const root = createRoot(container) |
| 65 | + const onSave = vi.fn() |
| 66 | + const value = Date.parse('2026-06-15T13:00:30Z') / 1000 |
| 67 | + const props = { |
| 68 | + value, |
| 69 | + column: column('ttl'), |
| 70 | + onSave, |
| 71 | + onCancel: vi.fn(), |
| 72 | + } |
| 73 | + |
| 74 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 75 | + act(() => root.render(createElement(InlineEditor, props))) |
| 76 | + mockUseTimezone.mockReturnValue('America/New_York') |
| 77 | + act(() => root.render(createElement(InlineEditor, props))) |
| 78 | + |
| 79 | + const input = container.querySelector('input') |
| 80 | + expect(input?.value).toBe('06/15/2026 6:00:30 AM') |
| 81 | + act(() => { |
| 82 | + input?.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })) |
| 83 | + }) |
| 84 | + |
| 85 | + expect(onSave).toHaveBeenCalledWith(value, 'enter') |
| 86 | + act(() => root.unmount()) |
| 87 | + container.remove() |
| 88 | + }) |
| 89 | +}) |
0 commit comments