|
| 1 | +import { describe, expect, it, vi, afterEach } from 'vitest'; |
| 2 | +import { CodeBlock } from './code-block'; |
| 3 | + |
| 4 | +const ORIGINAL_TIMEZONE = Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 5 | + |
| 6 | +function mockTimezone(timeZone: string) { |
| 7 | + vi.spyOn(Intl, 'DateTimeFormat').mockImplementation((() => ({ |
| 8 | + resolvedOptions: () => ({ |
| 9 | + locale: 'en-US', |
| 10 | + calendar: 'gregory', |
| 11 | + numberingSystem: 'latn', |
| 12 | + timeZone, |
| 13 | + }), |
| 14 | + })) as typeof Intl.DateTimeFormat); |
| 15 | +} |
| 16 | + |
| 17 | +describe('CodeBlock Timezone Normalization & Calendar Data Boundary Alignment', () => { |
| 18 | + afterEach(() => { |
| 19 | + vi.restoreAllMocks(); |
| 20 | + mockTimezone(ORIGINAL_TIMEZONE); |
| 21 | + }); |
| 22 | + |
| 23 | + it('keeps the component stable across common timezones', () => { |
| 24 | + ['UTC', 'America/New_York', 'Asia/Kolkata', 'Asia/Tokyo'].forEach((tz) => { |
| 25 | + vi.restoreAllMocks(); |
| 26 | + mockTimezone(tz); |
| 27 | + |
| 28 | + expect(Intl.DateTimeFormat().resolvedOptions().timeZone).toBe(tz); |
| 29 | + expect(CodeBlock).toBeDefined(); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('remains stable around leap year calendar boundaries', () => { |
| 34 | + const leap = new Date('2024-02-29T12:00:00Z'); |
| 35 | + const afterLeap = new Date('2024-03-01T12:00:00Z'); |
| 36 | + |
| 37 | + expect(leap.getUTCDate()).toBe(29); |
| 38 | + expect(afterLeap.getUTCDate()).toBe(1); |
| 39 | + |
| 40 | + expect(CodeBlock).toBeDefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('remains stable during daylight saving transitions', () => { |
| 44 | + mockTimezone('America/New_York'); |
| 45 | + |
| 46 | + const dstDate = new Date('2025-03-09T07:00:00Z'); |
| 47 | + |
| 48 | + expect(dstDate).toBeInstanceOf(Date); |
| 49 | + expect(CodeBlock).toBeDefined(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('creates a valid React element across timezone settings', () => { |
| 53 | + const element = <CodeBlock code='console.log("timezone");' />; |
| 54 | + |
| 55 | + expect(element).toBeDefined(); |
| 56 | + expect(element.props.code).toBe('console.log("timezone");'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('preserves the required code prop regardless of timezone', () => { |
| 60 | + expect(typeof CodeBlock).toBe('function'); |
| 61 | + expect(CodeBlock).toBeDefined(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments