|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 4 | +import ShareButtons from './ShareButtons'; |
| 5 | +import React from 'react'; |
| 6 | + |
| 7 | +describe('ShareButtons - Timezone Boundaries & Date Formatting', () => { |
| 8 | + const OriginalDate = global.Date; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + // Mock the timezone to UTC for consistent testing |
| 12 | + process.env.TZ = 'UTC'; |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + global.Date = OriginalDate; |
| 17 | + delete process.env.TZ; |
| 18 | + }); |
| 19 | + |
| 20 | + it('normalizes sharing parameters to standard UTC boundaries to avoid local date drift', () => { |
| 21 | + // Sharing standard text that might contain dates |
| 22 | + const dateText = new Date('2026-06-02T12:00:00Z').toISOString(); |
| 23 | + render(<ShareButtons url="https://example.com" title={`Shared on ${dateText}`} />); |
| 24 | + |
| 25 | + const twitterButton = screen.getByLabelText(/Share on X/i); |
| 26 | + expect(twitterButton).toHaveAttribute('href'); |
| 27 | + expect(twitterButton.getAttribute('href')).toContain('2026-06-02T12%3A00%3A00.000Z'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('aligns calendar data effectively during cross-timezone sharing operations', () => { |
| 31 | + process.env.TZ = 'Asia/Tokyo'; |
| 32 | + render(<ShareButtons url="https://example.com/event?time=1717329600" title="Event" />); |
| 33 | + const linkedinButton = screen.getByLabelText(/Share on LinkedIn/i); |
| 34 | + expect(linkedinButton).toBeInTheDocument(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('handles negative timezone offsets gracefully in generated sharing urls', () => { |
| 38 | + process.env.TZ = 'America/Los_Angeles'; |
| 39 | + render(<ShareButtons url="https://example.com" title="LA Time Event" />); |
| 40 | + const twitterButton = screen.getByLabelText(/Share on X/i); |
| 41 | + expect(twitterButton.getAttribute('href')).toContain('LA%20Time'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('preserves absolute time epoch values regardless of local execution context', () => { |
| 45 | + const epochTime = 1685707200000; |
| 46 | + render(<ShareButtons url={`https://example.com/share?t=${epochTime}`} />); |
| 47 | + const twitterButton = screen.getByLabelText(/Share on X/i); |
| 48 | + expect(twitterButton.getAttribute('href')).toContain(epochTime.toString()); |
| 49 | + }); |
| 50 | + |
| 51 | + it('maintains boundary precision at midnight transitions for shared content', () => { |
| 52 | + const midnight = new Date('2026-01-01T00:00:00.000Z').toISOString(); |
| 53 | + render(<ShareButtons url="https://example.com" title={`Happy New Year ${midnight}`} />); |
| 54 | + const linkedinButton = screen.getByLabelText(/Share on LinkedIn/i); |
| 55 | + expect(linkedinButton).toBeInTheDocument(); |
| 56 | + }); |
| 57 | +}); |
0 commit comments