|
| 1 | +// app/contributors/page.accessibility.test.tsx |
| 2 | + |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { render, screen } from '@testing-library/react'; |
| 5 | +import '@testing-library/jest-dom'; |
| 6 | + |
| 7 | +type ContributorsClientProps = { |
| 8 | + contributors: unknown[]; |
| 9 | + totalContributions: number; |
| 10 | + topContributors: unknown[]; |
| 11 | +}; |
| 12 | + |
| 13 | +const mockContributorsClient = vi.fn((_props?: ContributorsClientProps) => ( |
| 14 | + <div data-testid="contributors-client">Contributors Client</div> |
| 15 | +)); |
| 16 | + |
| 17 | +vi.mock('./ContributorsClient', () => ({ |
| 18 | + default: (props: ContributorsClientProps) => mockContributorsClient(props), |
| 19 | +})); |
| 20 | + |
| 21 | +describe('ContributorsPage Accessibility', () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('renders the contributors client container', async () => { |
| 27 | + const { default: ContributorsPage } = await import('./page'); |
| 28 | + |
| 29 | + const page = await ContributorsPage(); |
| 30 | + |
| 31 | + render(page); |
| 32 | + |
| 33 | + expect(screen.getByTestId('contributors-client')).toBeInTheDocument(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('passes contributors array to accessible client layer', async () => { |
| 37 | + const { default: ContributorsPage } = await import('./page'); |
| 38 | + |
| 39 | + const page = await ContributorsPage(); |
| 40 | + |
| 41 | + render(page); |
| 42 | + |
| 43 | + expect(mockContributorsClient).toHaveBeenCalled(); |
| 44 | + |
| 45 | + const props = mockContributorsClient.mock.calls[0][0] as ContributorsClientProps; |
| 46 | + |
| 47 | + expect(Array.isArray(props.contributors)).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + it('passes totalContributions to client component', async () => { |
| 51 | + const { default: ContributorsPage } = await import('./page'); |
| 52 | + |
| 53 | + const page = await ContributorsPage(); |
| 54 | + |
| 55 | + render(page); |
| 56 | + |
| 57 | + const props = mockContributorsClient.mock.calls[0][0] as ContributorsClientProps; |
| 58 | + |
| 59 | + expect(typeof props.totalContributions).toBe('number'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('passes topContributors collection to client component', async () => { |
| 63 | + const { default: ContributorsPage } = await import('./page'); |
| 64 | + |
| 65 | + const page = await ContributorsPage(); |
| 66 | + |
| 67 | + render(page); |
| 68 | + |
| 69 | + const props = mockContributorsClient.mock.calls[0][0] as ContributorsClientProps; |
| 70 | + |
| 71 | + expect(Array.isArray(props.topContributors)).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + it('renders successfully when contributor data is empty', async () => { |
| 75 | + const originalFetch = global.fetch; |
| 76 | + |
| 77 | + global.fetch = vi.fn().mockResolvedValue({ |
| 78 | + ok: false, |
| 79 | + status: 500, |
| 80 | + headers: { |
| 81 | + get: () => null, |
| 82 | + }, |
| 83 | + } as unknown as Response); |
| 84 | + |
| 85 | + const { default: ContributorsPage } = await import('./page'); |
| 86 | + |
| 87 | + const page = await ContributorsPage(); |
| 88 | + |
| 89 | + render(page); |
| 90 | + |
| 91 | + expect(screen.getByTestId('contributors-client')).toBeInTheDocument(); |
| 92 | + |
| 93 | + global.fetch = originalFetch; |
| 94 | + }); |
| 95 | +}); |
0 commit comments