|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectui#10293 — the `ui:calendar` primitive selects the day an ISO string |
| 11 | + * names, the same day the equivalent `Date` selects. |
| 12 | + * |
| 13 | + * ── What was measured ─────────────────────────────────────────────────────── |
| 14 | + * The renderer handed `schema.value || schema.defaultValue` to `DayPicker` |
| 15 | + * untouched. `DayPicker` compares through date-fns, which parses a string |
| 16 | + * with the engine's own `Date` parse, so a date-only string was UTC midnight |
| 17 | + * read back in local time: `2026-09-15` selected the 14th in |
| 18 | + * `America/Los_Angeles` and the 15th in UTC, while `new Date(2026, 8, 15)` |
| 19 | + * selected the 15th in both. A date-time string already selected its local |
| 20 | + * day. The read site now coerces a string through `toDisplayDate` |
| 21 | + * (`@object-ui/core`, the objectui#10183 convention). |
| 22 | + * |
| 23 | + * ── ⚠️ The zone cases run ONLY when driven, in a FORKS child ──────────────── |
| 24 | + * `process.env.TZ` written inside a test of the normal run does not move the |
| 25 | + * zone (the root config runs `pool: 'threads'`), so the zone cases are |
| 26 | + * skipped there and `scripts/__tests__/date-only-zone-pins-10183.test.ts` |
| 27 | + * runs them on the forks pool and fails unless every one ran and passed. Each |
| 28 | + * zone opens with a rig case, so a child whose zone did not move reds instead |
| 29 | + * of going quietly green. The UTC cases below run in the normal run too; they |
| 30 | + * cannot tell the repair from its absence, because UTC is the one offset where |
| 31 | + * the two parses agree, and they are here for the `Date` / date-time faces. |
| 32 | + * |
| 33 | + * `Asia/Shanghai` is the control: east of UTC the UTC-midnight parse already |
| 34 | + * landed on the named day, and an hour-offset "repair" would break it. |
| 35 | + */ |
| 36 | +import React from 'react'; |
| 37 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 38 | +import { render, cleanup } from '@testing-library/react'; |
| 39 | +import { ComponentRegistry } from '@object-ui/core'; |
| 40 | +// Registers the renderers at module scope, NOT inside a hook (objectui#3010). |
| 41 | +import '../../../renderers'; |
| 42 | + |
| 43 | +const DRIVEN = process.env.OBJECTUI_DATE_ZONE_CHILD === '1'; |
| 44 | + |
| 45 | +/** UTC-7 in September — the card's direction. */ |
| 46 | +const WEST = 'America/Los_Angeles'; |
| 47 | +/** UTC+8 — the control. */ |
| 48 | +const EAST = 'Asia/Shanghai'; |
| 49 | + |
| 50 | +/** Frozen inside September 2026, so the calendar opens on the month under test. */ |
| 51 | +const CLOCK = '2026-09-03T10:35:00.000Z'; |
| 52 | +const DATE_ONLY = '2026-09-15'; |
| 53 | +/** A fixed instant: 21:00 on the 15th in the west, 12:00 on the 16th in the east. */ |
| 54 | +const INSTANT = '2026-09-16T04:00:00.000Z'; |
| 55 | + |
| 56 | +/** Freeze the clock, moving this forked process into `zone` first when given. */ |
| 57 | +function enter(zone?: string): void { |
| 58 | + if (zone) process.env.TZ = zone; |
| 59 | + vi.useFakeTimers({ toFake: ['Date'] }); |
| 60 | + vi.setSystemTime(new Date(CLOCK)); |
| 61 | +} |
| 62 | + |
| 63 | +afterEach(() => { |
| 64 | + cleanup(); |
| 65 | + vi.useRealTimers(); |
| 66 | +}); |
| 67 | + |
| 68 | +/** The `data-day` of every selected cell the calendar paints. */ |
| 69 | +function selectedDays(extra: Record<string, unknown>): string[] { |
| 70 | + const Calendar = ComponentRegistry.get('ui:calendar')!; |
| 71 | + const { container } = render(<Calendar schema={{ type: 'ui:calendar', mode: 'single', ...extra }} />); |
| 72 | + const days = Array.from(container.querySelectorAll('td[data-selected="true"]')) |
| 73 | + .map((td) => td.getAttribute('data-day') ?? ''); |
| 74 | + cleanup(); |
| 75 | + return days; |
| 76 | +} |
| 77 | + |
| 78 | +describe('ui:calendar selected date, in the suite zone (objectui#10293)', () => { |
| 79 | + it('a local `Date` selects the day it names (the reference face)', () => { |
| 80 | + enter(); |
| 81 | + expect(selectedDays({ value: new Date(2026, 8, 15) })).toEqual(['2026-09-15']); |
| 82 | + }); |
| 83 | + |
| 84 | + it('a date-only string selects that same day, through `value` and `defaultValue`', () => { |
| 85 | + enter(); |
| 86 | + expect(selectedDays({ value: DATE_ONLY })).toEqual(['2026-09-15']); |
| 87 | + expect(selectedDays({ defaultValue: DATE_ONLY })).toEqual(['2026-09-15']); |
| 88 | + }); |
| 89 | + |
| 90 | + it('an invalid string still selects nothing, as it did before the coercion', () => { |
| 91 | + enter(); |
| 92 | + expect(selectedDays({ value: 'not a date' })).toEqual([]); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe.runIf(DRIVEN)('ui:calendar date-only value west of UTC (objectui#10293)', () => { |
| 97 | + it('rig: the zone really moved', () => { |
| 98 | + enter(WEST); |
| 99 | + expect(Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(WEST); |
| 100 | + expect(new Date(INSTANT).getHours()).toBe(21); |
| 101 | + }); |
| 102 | + |
| 103 | + it('fixture validity: the engine parse of the string lands on the day before here', () => { |
| 104 | + enter(WEST); |
| 105 | + // Without this the cases below would be green for free. |
| 106 | + expect(new Date(DATE_ONLY).getDate()).toBe(14); |
| 107 | + }); |
| 108 | + |
| 109 | + it('`2026-09-15` selects the 15th, the day `new Date(2026, 8, 15)` selects', () => { |
| 110 | + enter(WEST); |
| 111 | + expect(selectedDays({ value: new Date(2026, 8, 15) })).toEqual(['2026-09-15']); |
| 112 | + expect(selectedDays({ value: DATE_ONLY })).toEqual(['2026-09-15']); |
| 113 | + expect(selectedDays({ defaultValue: DATE_ONLY })).toEqual(['2026-09-15']); |
| 114 | + }); |
| 115 | + |
| 116 | + it('an instant keeps selecting its local day, the 15th here', () => { |
| 117 | + enter(WEST); |
| 118 | + expect(selectedDays({ value: INSTANT })).toEqual(['2026-09-15']); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe.runIf(DRIVEN)('ui:calendar date-only value east of UTC, the control (objectui#10293)', () => { |
| 123 | + it('rig: the zone really moved', () => { |
| 124 | + enter(EAST); |
| 125 | + expect(Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(EAST); |
| 126 | + expect(new Date(INSTANT).getHours()).toBe(12); |
| 127 | + }); |
| 128 | + |
| 129 | + it('`2026-09-15` selects the 15th, as it already did', () => { |
| 130 | + enter(EAST); |
| 131 | + expect(selectedDays({ value: DATE_ONLY })).toEqual(['2026-09-15']); |
| 132 | + }); |
| 133 | + |
| 134 | + it('an instant keeps selecting its local day, the 16th here', () => { |
| 135 | + enter(EAST); |
| 136 | + expect(selectedDays({ value: INSTANT })).toEqual(['2026-09-16']); |
| 137 | + }); |
| 138 | +}); |
0 commit comments