|
| 1 | +/** |
| 2 | + * Tests for ConnectGitHubBanner — the persistent "Connect your GitHub |
| 3 | + * account" nag rendered directly under the navbar on every page. |
| 4 | + * |
| 5 | + * Visibility rule: signed-in + hasGitHubLink === false + lastLoginMethod |
| 6 | + * ∈ {legacy_password, password_reset} + not dismissed. |
| 7 | + */ |
| 8 | +import { describe, expect, it, vi, afterEach } from 'vitest'; |
| 9 | +import { fireEvent, screen, waitFor } from '@testing-library/react'; |
| 10 | +import { renderScreen, mockOk } from './test-utils.js'; |
| 11 | +import { ConnectGitHubBanner } from '../src/components/ConnectGitHubBanner.js'; |
| 12 | +import { AuthProvider } from '../src/hooks/useAuth.js'; |
| 13 | + |
| 14 | +interface MeShape { |
| 15 | + person: { id: string; slug: string; fullName: string; accountLevel: string; avatarUrl: string | null } | null; |
| 16 | + accountLevel: string; |
| 17 | + hasGitHubLink: boolean; |
| 18 | + lastLoginMethod: 'github' | 'legacy_password' | 'password_reset' | null; |
| 19 | +} |
| 20 | + |
| 21 | +function mockMe(me: MeShape): void { |
| 22 | + vi.spyOn(globalThis, 'fetch').mockImplementation(((input: string) => { |
| 23 | + if (input.startsWith('/api/auth/me')) { |
| 24 | + return Promise.resolve( |
| 25 | + new Response(JSON.stringify(mockOk(me)), { |
| 26 | + status: 200, |
| 27 | + headers: { 'content-type': 'application/json' }, |
| 28 | + }), |
| 29 | + ); |
| 30 | + } |
| 31 | + return Promise.resolve(new Response(null, { status: 404 })); |
| 32 | + }) as typeof fetch); |
| 33 | +} |
| 34 | + |
| 35 | +const baseLegacyPerson: MeShape = { |
| 36 | + person: { |
| 37 | + id: '01951a3c-0000-7000-8000-0000ffffff01', |
| 38 | + slug: 'legacy-user', |
| 39 | + fullName: 'Legacy User', |
| 40 | + accountLevel: 'user', |
| 41 | + avatarUrl: null, |
| 42 | + }, |
| 43 | + accountLevel: 'user', |
| 44 | + hasGitHubLink: false, |
| 45 | + lastLoginMethod: 'legacy_password', |
| 46 | +}; |
| 47 | + |
| 48 | +const anonMe: MeShape = { |
| 49 | + person: null, |
| 50 | + accountLevel: 'anonymous', |
| 51 | + hasGitHubLink: false, |
| 52 | + lastLoginMethod: null, |
| 53 | +}; |
| 54 | + |
| 55 | +function render() { |
| 56 | + return renderScreen( |
| 57 | + <AuthProvider> |
| 58 | + <ConnectGitHubBanner /> |
| 59 | + </AuthProvider>, |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +describe('ConnectGitHubBanner', () => { |
| 64 | + afterEach(() => { |
| 65 | + vi.restoreAllMocks(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('renders for legacy-password user with no GitHub link', async () => { |
| 69 | + mockMe(baseLegacyPerson); |
| 70 | + render(); |
| 71 | + await waitFor(() => { |
| 72 | + expect( |
| 73 | + screen.getByRole('region', { name: /connect github/i }), |
| 74 | + ).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + // CTA form posts to the link endpoint. |
| 77 | + const region = screen.getByRole('region', { name: /connect github/i }); |
| 78 | + expect(region.querySelector('form[action="/api/auth/link-github"]')).not.toBeNull(); |
| 79 | + expect(screen.getByRole('button', { name: /dismiss/i })).toBeInTheDocument(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('renders for a user whose session was minted via password reset', async () => { |
| 83 | + mockMe({ ...baseLegacyPerson, lastLoginMethod: 'password_reset' }); |
| 84 | + render(); |
| 85 | + await waitFor(() => { |
| 86 | + expect( |
| 87 | + screen.getByRole('region', { name: /connect github/i }), |
| 88 | + ).toBeInTheDocument(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('does not render for a github-signed-in user', async () => { |
| 93 | + mockMe({ |
| 94 | + ...baseLegacyPerson, |
| 95 | + hasGitHubLink: true, |
| 96 | + lastLoginMethod: 'github', |
| 97 | + }); |
| 98 | + render(); |
| 99 | + // Wait for /api/auth/me to settle (loading → resolved). The |
| 100 | + // simplest signal is a tick: a known-true assertion plus a short |
| 101 | + // microtask gap. |
| 102 | + await new Promise((r) => setTimeout(r, 0)); |
| 103 | + expect( |
| 104 | + screen.queryByRole('region', { name: /connect github/i }), |
| 105 | + ).not.toBeInTheDocument(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('does not render for anonymous viewers', async () => { |
| 109 | + mockMe(anonMe); |
| 110 | + render(); |
| 111 | + await new Promise((r) => setTimeout(r, 0)); |
| 112 | + expect( |
| 113 | + screen.queryByRole('region', { name: /connect github/i }), |
| 114 | + ).not.toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('hides after the user clicks dismiss', async () => { |
| 118 | + mockMe(baseLegacyPerson); |
| 119 | + render(); |
| 120 | + const dismissBtn = await screen.findByRole('button', { name: /dismiss/i }); |
| 121 | + fireEvent.click(dismissBtn); |
| 122 | + await waitFor(() => { |
| 123 | + expect( |
| 124 | + screen.queryByRole('region', { name: /connect github/i }), |
| 125 | + ).not.toBeInTheDocument(); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments