|
1 | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 3 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
2 | 4 | import { render, screen } from '@testing-library/react'; |
3 | | -import { describe, expect, it, vi } from 'vitest'; |
4 | 5 | import StatsCard from './StatsCard'; |
5 | 6 |
|
| 7 | +// Mock framer-motion |
6 | 8 | vi.mock('framer-motion', () => ({ |
7 | 9 | motion: { |
8 | | - div: ({ children, className, ...props }: any) => { |
9 | | - delete props.initial; |
10 | | - delete props.whileInView; |
11 | | - delete props.viewport; |
12 | | - delete props.whileHover; |
13 | | - delete props.transition; |
14 | | - |
15 | | - return ( |
16 | | - <div className={className} {...props}> |
17 | | - {children} |
18 | | - </div> |
19 | | - ); |
20 | | - }, |
| 10 | + div: ({ |
| 11 | + children, |
| 12 | + whileHover, |
| 13 | + whileTap, |
| 14 | + whileInView, |
| 15 | + initial, |
| 16 | + animate, |
| 17 | + exit, |
| 18 | + transition, |
| 19 | + viewport, |
| 20 | + layoutId, |
| 21 | + ...props |
| 22 | + }: any) => ( |
| 23 | + <div {...props} data-testid="motion-div"> |
| 24 | + {children} |
| 25 | + </div> |
| 26 | + ), |
21 | 27 | }, |
22 | 28 | })); |
23 | 29 |
|
| 30 | +// Mock lucide-react |
| 31 | +vi.mock('lucide-react', () => ({ |
| 32 | + Flame: (props: any) => <div data-testid="icon-flame" {...props} />, |
| 33 | + TrendingUp: (props: any) => <div data-testid="icon-trending-up" {...props} />, |
| 34 | + GitCommit: (props: any) => <div data-testid="icon-git-commit" {...props} />, |
| 35 | + LucideIcon: () => null, |
| 36 | +})); |
| 37 | + |
24 | 38 | describe('StatsCard', () => { |
25 | | - it('renders a clean UTC disclaimer prefix and preserves the UTC date text', () => { |
| 39 | + beforeEach(() => { |
| 40 | + vi.clearAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + // Test 1: renders title prop text |
| 44 | + it('should render the title prop text', () => { |
| 45 | + render(<StatsCard title="Active Streak" value="42" description="days in a row" icon="Flame" />); |
| 46 | + |
| 47 | + expect(screen.getByText('Active Streak')).toBeDefined(); |
| 48 | + }); |
| 49 | + |
| 50 | + // Test 2: renders value prop text |
| 51 | + it('should render the value prop text', () => { |
| 52 | + render(<StatsCard title="Active Streak" value="42" description="days in a row" icon="Flame" />); |
| 53 | + |
| 54 | + expect(screen.getByText('42')).toBeDefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + // Test 3: renders description prop text |
| 58 | + it('should render the description prop text', () => { |
| 59 | + render(<StatsCard title="Active Streak" value="42" description="days in a row" icon="Flame" />); |
| 60 | + |
| 61 | + expect(screen.getByText('days in a row')).toBeDefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + // Test 4: UTC disclaimer appears when showUTCDisclaimer=true |
| 65 | + it('should display UTC disclaimer when showUTCDisclaimer is true', () => { |
| 66 | + render( |
| 67 | + <StatsCard |
| 68 | + title="Active Streak" |
| 69 | + value="42" |
| 70 | + description="days in a row" |
| 71 | + icon="Flame" |
| 72 | + showUTCDisclaimer={true} |
| 73 | + /> |
| 74 | + ); |
| 75 | + |
| 76 | + expect( |
| 77 | + screen.getByText(/Streaks are calculated in UTC and may differ from your local timezone/i) |
| 78 | + ).toBeDefined(); |
| 79 | + }); |
| 80 | + |
| 81 | + // Test 5: UTC disclaimer is absent when showUTCDisclaimer=false |
| 82 | + it('should not display UTC disclaimer when showUTCDisclaimer is false', () => { |
| 83 | + render( |
| 84 | + <StatsCard |
| 85 | + title="Active Streak" |
| 86 | + value="42" |
| 87 | + description="days in a row" |
| 88 | + icon="Flame" |
| 89 | + showUTCDisclaimer={false} |
| 90 | + /> |
| 91 | + ); |
| 92 | + |
| 93 | + expect( |
| 94 | + screen.queryByText(/Streaks are calculated in UTC and may differ from your local timezone/i) |
| 95 | + ).toBeNull(); |
| 96 | + }); |
| 97 | + |
| 98 | + // Test 6: utcDate string appears when provided with disclaimer |
| 99 | + it('should display utcDate when provided along with showUTCDisclaimer=true', () => { |
| 100 | + render( |
| 101 | + <StatsCard |
| 102 | + title="Active Streak" |
| 103 | + value="42" |
| 104 | + description="days in a row" |
| 105 | + icon="Flame" |
| 106 | + showUTCDisclaimer={true} |
| 107 | + utcDate="2024-01-15" |
| 108 | + /> |
| 109 | + ); |
| 110 | + |
| 111 | + expect(screen.getByText(/UTC Date: 2024-01-15/)).toBeDefined(); |
| 112 | + }); |
| 113 | + |
| 114 | + // Test 7: falls back to Flame icon for unknown icon name |
| 115 | + it('should fall back to Flame icon when icon name is unknown', () => { |
| 116 | + render( |
| 117 | + <StatsCard title="Active Streak" value="42" description="days in a row" icon="UnknownIcon" /> |
| 118 | + ); |
| 119 | + |
| 120 | + expect(screen.getByTestId('icon-flame')).toBeDefined(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('renders a clean info symbol in the UTC disclaimer without mojibake text', () => { |
26 | 124 | const { container } = render( |
27 | 125 | <StatsCard |
28 | 126 | title="Current Streak" |
29 | 127 | value="12" |
30 | 128 | description="Consecutive contribution days" |
31 | 129 | icon="Flame" |
32 | | - showUTCDisclaimer |
| 130 | + showUTCDisclaimer={true} |
33 | 131 | utcDate="2026-06-01" |
34 | 132 | /> |
35 | 133 | ); |
36 | 134 |
|
37 | | - expect( |
38 | | - screen.getByText('Streaks are calculated in UTC and may differ from your local timezone.') |
39 | | - ).toBeDefined(); |
40 | | - expect(container.textContent).toContain('ℹ'); |
41 | | - expect(container.textContent).not.toContain('ℹ'); |
42 | | - expect(screen.getByText('UTC Date: 2026-06-01')).toBeDefined(); |
| 135 | + expect(container.textContent).toContain('\u2139'); |
| 136 | + expect(container.textContent).not.toContain('\u00E2\u20AC\u2039'); |
43 | 137 | }); |
44 | 138 | }); |
0 commit comments