|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import BrandParticles from './BrandParticles'; |
| 5 | + |
| 6 | +// Mock framer-motion to control particles rendering and inspect attributes |
| 7 | +vi.mock('framer-motion', () => ({ |
| 8 | + motion: { |
| 9 | + div: ({ |
| 10 | + animate, |
| 11 | + transition, |
| 12 | + style, |
| 13 | + ...props |
| 14 | + }: { |
| 15 | + animate?: unknown; |
| 16 | + transition?: unknown; |
| 17 | + style?: React.CSSProperties; |
| 18 | + [key: string]: unknown; |
| 19 | + }) => ( |
| 20 | + <div |
| 21 | + {...props} |
| 22 | + style={style} |
| 23 | + data-testid="motion-div" |
| 24 | + data-animate={JSON.stringify(animate)} |
| 25 | + data-transition={JSON.stringify(transition)} |
| 26 | + /> |
| 27 | + ), |
| 28 | + }, |
| 29 | + useReducedMotion: () => false, |
| 30 | +})); |
| 31 | + |
| 32 | +describe('BrandParticles - Timezone Boundaries & Calendar Alignment', () => { |
| 33 | + beforeEach(() => { |
| 34 | + vi.restoreAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + vi.unstubAllGlobals(); |
| 39 | + }); |
| 40 | + |
| 41 | + // --- Test Case 1 --- |
| 42 | + it('renders successfully across multiple timezone offsets (UTC, EST, IST, JST)', () => { |
| 43 | + const timezones = [0, 300, -330, -540]; // Offsets in minutes |
| 44 | + |
| 45 | + timezones.forEach((offset) => { |
| 46 | + vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(offset); |
| 47 | + const { unmount } = render(<BrandParticles />); |
| 48 | + |
| 49 | + const particles = screen.getAllByTestId('motion-div'); |
| 50 | + expect(particles).toHaveLength(40); |
| 51 | + |
| 52 | + unmount(); |
| 53 | + vi.restoreAllMocks(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + // --- Test Case 2 --- |
| 58 | + it('renders correctly under leap year boundary system clocks', () => { |
| 59 | + vi.useFakeTimers(); |
| 60 | + vi.setSystemTime(new Date('2024-02-29T23:59:59Z')); // Leap day rollover |
| 61 | + |
| 62 | + render(<BrandParticles />); |
| 63 | + const particles = screen.getAllByTestId('motion-div'); |
| 64 | + expect(particles).toHaveLength(40); |
| 65 | + |
| 66 | + vi.useRealTimers(); |
| 67 | + }); |
| 68 | + |
| 69 | + // --- Test Case 3 --- |
| 70 | + it('retains visual animation paths during Daylight Saving Time (DST) transitions', () => { |
| 71 | + vi.useFakeTimers(); |
| 72 | + // Simulate spring forward transition |
| 73 | + vi.setSystemTime(new Date('2026-03-08T02:00:00Z')); |
| 74 | + |
| 75 | + render(<BrandParticles />); |
| 76 | + const particles = screen.getAllByTestId('motion-div'); |
| 77 | + expect(particles).toHaveLength(40); |
| 78 | + |
| 79 | + // Verify visual animate parameters are generated and valid |
| 80 | + const firstParticle = particles[0]; |
| 81 | + const animateAttr = firstParticle.getAttribute('data-animate'); |
| 82 | + expect(animateAttr).toContain('y'); |
| 83 | + expect(animateAttr).toContain('x'); |
| 84 | + expect(animateAttr).toContain('rotate'); |
| 85 | + |
| 86 | + vi.useRealTimers(); |
| 87 | + }); |
| 88 | + |
| 89 | + // --- Test Case 4 --- |
| 90 | + it('validates calendar date format utility stubs do not impact layout mounting tree', () => { |
| 91 | + // Mock standard DateTimeFormat to emulate locale-specific changes |
| 92 | + const mockDateTimeFormat = function () { |
| 93 | + return { |
| 94 | + format: () => '01/01/2026', |
| 95 | + resolvedOptions: () => |
| 96 | + ({ locale: 'en-US', timeZone: 'UTC' }) as Intl.ResolvedDateTimeFormatOptions, |
| 97 | + }; |
| 98 | + } as unknown as typeof Intl.DateTimeFormat; |
| 99 | + |
| 100 | + vi.stubGlobal('Intl', { |
| 101 | + ...Intl, |
| 102 | + DateTimeFormat: mockDateTimeFormat, |
| 103 | + }); |
| 104 | + |
| 105 | + render(<BrandParticles />); |
| 106 | + const particles = screen.getAllByTestId('motion-div'); |
| 107 | + expect(particles).toHaveLength(40); |
| 108 | + |
| 109 | + vi.unstubAllGlobals(); |
| 110 | + }); |
| 111 | + |
| 112 | + // --- Test Case 5 --- |
| 113 | + it('remains visual-stable and aligned when timezone offset shifts dynamically', () => { |
| 114 | + // Start with EST offset |
| 115 | + const offsetSpy = vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(300); |
| 116 | + const { rerender } = render(<BrandParticles />); |
| 117 | + |
| 118 | + let particles = screen.getAllByTestId('motion-div'); |
| 119 | + expect(particles).toHaveLength(40); |
| 120 | + |
| 121 | + // Shift to JST offset dynamically |
| 122 | + offsetSpy.mockReturnValue(-540); |
| 123 | + rerender(<BrandParticles />); |
| 124 | + |
| 125 | + particles = screen.getAllByTestId('motion-div'); |
| 126 | + expect(particles).toHaveLength(40); |
| 127 | + }); |
| 128 | +}); |
0 commit comments