|
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import userEvent from '@testing-library/user-event' |
| 3 | +import { FluentProvider, webLightTheme } from '@fluentui/react-components' |
| 4 | +import { UserAccountButton } from './UserAccountButton' |
| 5 | + |
| 6 | +const mockLoginRedirect = jest.fn() |
| 7 | +const mockLogoutRedirect = jest.fn() |
| 8 | +const mockGetActiveAccount = jest.fn() |
| 9 | +let mockAccounts: { name?: string; username?: string }[] = [] |
| 10 | + |
| 11 | +jest.mock('@azure/msal-react', () => ({ |
| 12 | + useMsal: () => ({ |
| 13 | + instance: { |
| 14 | + getActiveAccount: mockGetActiveAccount, |
| 15 | + loginRedirect: mockLoginRedirect, |
| 16 | + logoutRedirect: mockLogoutRedirect, |
| 17 | + }, |
| 18 | + accounts: mockAccounts, |
| 19 | + }), |
| 20 | +})) |
| 21 | + |
| 22 | +let mockAuthConfig = { clientId: '', tenantId: '', allowedGroupIds: '' } |
| 23 | + |
| 24 | +jest.mock('../auth/AuthConfigContext', () => ({ |
| 25 | + useAuthConfig: () => mockAuthConfig, |
| 26 | +})) |
| 27 | + |
| 28 | +jest.mock('../auth/msalConfig', () => ({ |
| 29 | + buildLoginRequest: (clientId: string) => ({ scopes: [`${clientId}/.default`] }), |
| 30 | +})) |
| 31 | + |
| 32 | +const TestWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => ( |
| 33 | + <FluentProvider theme={webLightTheme}>{children}</FluentProvider> |
| 34 | +) |
| 35 | + |
| 36 | +describe('UserAccountButton', () => { |
| 37 | + beforeEach(() => { |
| 38 | + jest.clearAllMocks() |
| 39 | + mockGetActiveAccount.mockReturnValue(null) |
| 40 | + mockLoginRedirect.mockResolvedValue(undefined) |
| 41 | + mockLogoutRedirect.mockResolvedValue(undefined) |
| 42 | + mockAuthConfig = { clientId: '', tenantId: '', allowedGroupIds: '' } |
| 43 | + mockAccounts = [] |
| 44 | + }) |
| 45 | + |
| 46 | + it('returns null when auth is disabled (no clientId)', () => { |
| 47 | + render( |
| 48 | + <TestWrapper> |
| 49 | + <UserAccountButton /> |
| 50 | + </TestWrapper> |
| 51 | + ) |
| 52 | + |
| 53 | + // UserAccountButton renders null — no buttons appear |
| 54 | + expect(screen.queryByRole('button')).toBeNull() |
| 55 | + }) |
| 56 | + |
| 57 | + it('renders Log In button when auth enabled but no account', () => { |
| 58 | + mockAuthConfig = { clientId: 'test-client-id', tenantId: 'test-tenant', allowedGroupIds: '' } |
| 59 | + |
| 60 | + render( |
| 61 | + <TestWrapper> |
| 62 | + <UserAccountButton /> |
| 63 | + </TestWrapper> |
| 64 | + ) |
| 65 | + |
| 66 | + expect(screen.getByRole('button', { name: /log in/i })).toBeInTheDocument() |
| 67 | + }) |
| 68 | + |
| 69 | + it('calls loginRedirect when Log In is clicked', async () => { |
| 70 | + mockAuthConfig = { clientId: 'test-client-id', tenantId: 'test-tenant', allowedGroupIds: '' } |
| 71 | + const user = userEvent.setup() |
| 72 | + |
| 73 | + render( |
| 74 | + <TestWrapper> |
| 75 | + <UserAccountButton /> |
| 76 | + </TestWrapper> |
| 77 | + ) |
| 78 | + |
| 79 | + await user.click(screen.getByRole('button', { name: /log in/i })) |
| 80 | + |
| 81 | + expect(mockLoginRedirect).toHaveBeenCalledWith({ scopes: ['test-client-id/.default'] }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('renders user display name and Sign Out when account exists', () => { |
| 85 | + mockAuthConfig = { clientId: 'test-client-id', tenantId: 'test-tenant', allowedGroupIds: '' } |
| 86 | + mockGetActiveAccount.mockReturnValue({ |
| 87 | + name: 'Alice Smith', |
| 88 | + username: 'alice@example.com', |
| 89 | + }) |
| 90 | + |
| 91 | + render( |
| 92 | + <TestWrapper> |
| 93 | + <UserAccountButton /> |
| 94 | + </TestWrapper> |
| 95 | + ) |
| 96 | + |
| 97 | + expect(screen.getByText('Alice Smith')).toBeInTheDocument() |
| 98 | + }) |
| 99 | + |
| 100 | + it('renders user display name when account comes from accounts[0] (no active account)', () => { |
| 101 | + mockAuthConfig = { clientId: 'test-client-id', tenantId: 'test-tenant', allowedGroupIds: '' } |
| 102 | + mockAccounts = [{ name: 'Bob Jones', username: 'bob@example.com' }] |
| 103 | + |
| 104 | + render( |
| 105 | + <TestWrapper> |
| 106 | + <UserAccountButton /> |
| 107 | + </TestWrapper> |
| 108 | + ) |
| 109 | + |
| 110 | + expect(screen.getByText('Bob Jones')).toBeInTheDocument() |
| 111 | + }) |
| 112 | + |
| 113 | + it('calls logoutRedirect when Sign Out is clicked', async () => { |
| 114 | + mockAuthConfig = { clientId: 'test-client-id', tenantId: 'test-tenant', allowedGroupIds: '' } |
| 115 | + mockGetActiveAccount.mockReturnValue({ |
| 116 | + name: 'Alice Smith', |
| 117 | + username: 'alice@example.com', |
| 118 | + }) |
| 119 | + const user = userEvent.setup() |
| 120 | + |
| 121 | + render( |
| 122 | + <TestWrapper> |
| 123 | + <UserAccountButton /> |
| 124 | + </TestWrapper> |
| 125 | + ) |
| 126 | + |
| 127 | + // Open the popover by clicking the user button |
| 128 | + await user.click(screen.getByRole('button', { name: /alice smith/i })) |
| 129 | + |
| 130 | + await user.click(screen.getByRole('button', { name: /sign out/i })) |
| 131 | + |
| 132 | + expect(mockLogoutRedirect).toHaveBeenCalled() |
| 133 | + }) |
| 134 | +}) |
0 commit comments