|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + render, |
| 4 | + screen, |
| 5 | + waitFor, |
| 6 | + within, |
| 7 | +} from '@testing-library/react'; |
| 8 | +import { mockDeep } from 'jest-mock-extended'; |
| 9 | +import NhsNotifyHeaderWithAccount from '@molecules/HeaderWithAccount/HeaderWithAccount'; |
| 10 | +import { type UseAuthenticator, useAuthenticator } from '@aws-amplify/ui-react'; |
| 11 | + |
| 12 | +jest.mock('@aws-amplify/ui-react'); |
| 13 | + |
| 14 | +const mockFetchAuthSession = jest.fn(); |
| 15 | +jest.mock('aws-amplify/auth', () => ({ |
| 16 | + fetchAuthSession: (...args: any[]) => mockFetchAuthSession(...args), |
| 17 | +})); |
| 18 | + |
| 19 | +const mockGetIdTokenClaims = jest.fn(); |
| 20 | +jest.mock('@utils/token-utils', () => ({ |
| 21 | + getIdTokenClaims: (...args: any[]) => mockGetIdTokenClaims(...args), |
| 22 | +})); |
| 23 | + |
| 24 | +const setAuthStatus = ( |
| 25 | + status: 'authenticated' | 'unauthenticated' | 'configuring' |
| 26 | +) => { |
| 27 | + jest.mocked(useAuthenticator).mockImplementation((selector) => { |
| 28 | + const context = mockDeep<UseAuthenticator>({ |
| 29 | + authStatus: status, |
| 30 | + }); |
| 31 | + |
| 32 | + if (selector) { |
| 33 | + selector(context); |
| 34 | + } |
| 35 | + |
| 36 | + return context; |
| 37 | + }); |
| 38 | +}; |
| 39 | + |
| 40 | +describe('NhsNotifyHeaderWithAccount', () => { |
| 41 | + describe('when unauthenticated', () => { |
| 42 | + beforeEach(() => { |
| 43 | + jest.resetAllMocks(); |
| 44 | + setAuthStatus('unauthenticated'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('renders the logo and service name with the correct url', () => { |
| 48 | + render(<NhsNotifyHeaderWithAccount />); |
| 49 | + |
| 50 | + const logoServiceLink = screen.getByTestId('header-logo-service-link'); |
| 51 | + |
| 52 | + expect(logoServiceLink).toContainElement( |
| 53 | + screen.getByRole('img', { name: 'NHS logo' }) |
| 54 | + ); |
| 55 | + expect(logoServiceLink).toHaveAttribute('href', '/message-templates'); |
| 56 | + expect(logoServiceLink).toHaveTextContent('Notify'); |
| 57 | + }); |
| 58 | + |
| 59 | + it(`renders the authentication link as 'sign in'`, () => { |
| 60 | + render(<NhsNotifyHeaderWithAccount />); |
| 61 | + |
| 62 | + expect(screen.getByTestId('auth-link')).toHaveTextContent('Sign in'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('does not fetch session or claims', async () => { |
| 66 | + render(<NhsNotifyHeaderWithAccount />); |
| 67 | + |
| 68 | + await Promise.resolve(); |
| 69 | + |
| 70 | + expect(mockFetchAuthSession).not.toHaveBeenCalled(); |
| 71 | + expect(mockGetIdTokenClaims).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('matches snapshot (unauthenticated)', () => { |
| 75 | + const container = render(<NhsNotifyHeaderWithAccount />); |
| 76 | + |
| 77 | + expect(container.asFragment()).toMatchSnapshot(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('when authenticated', () => { |
| 82 | + beforeEach(() => { |
| 83 | + jest.resetAllMocks(); |
| 84 | + setAuthStatus('authenticated'); |
| 85 | + |
| 86 | + mockFetchAuthSession.mockResolvedValue({ |
| 87 | + tokens: { |
| 88 | + idToken: { toString: () => 'fake.id.token' }, |
| 89 | + accessToken: { toString: () => 'fake.access.token' }, |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + mockGetIdTokenClaims.mockReturnValue({ |
| 94 | + displayName: 'Dr Test Example', |
| 95 | + clientName: 'NHS England', |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('renders the users display name', async () => { |
| 100 | + render(<NhsNotifyHeaderWithAccount />); |
| 101 | + |
| 102 | + await waitFor(() => { |
| 103 | + expect(screen.getByTestId('account-display-name')).toHaveTextContent( |
| 104 | + 'Dr Test Example' |
| 105 | + ); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('renders the client name', async () => { |
| 110 | + render(<NhsNotifyHeaderWithAccount />); |
| 111 | + |
| 112 | + await waitFor(() => { |
| 113 | + expect(screen.getByTestId('account-client-name')).toHaveTextContent( |
| 114 | + 'NHS England' |
| 115 | + ); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it(`renders auth link as 'Sign out'`, async () => { |
| 120 | + render(<NhsNotifyHeaderWithAccount />); |
| 121 | + |
| 122 | + await waitFor(() => { |
| 123 | + expect(screen.getByTestId('auth-link')).toHaveTextContent('Sign out'); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('handles missing id token by clearing names', async () => { |
| 128 | + mockFetchAuthSession.mockResolvedValueOnce({ |
| 129 | + tokens: { |
| 130 | + idToken: undefined, |
| 131 | + accessToken: { toString: () => 'fake.access.token' }, |
| 132 | + }, |
| 133 | + }); |
| 134 | + |
| 135 | + render(<NhsNotifyHeaderWithAccount />); |
| 136 | + |
| 137 | + await waitFor(() => { |
| 138 | + expect(screen.queryByTestId('account-display-name')).toBeNull(); |
| 139 | + expect(screen.queryByTestId('account-client-name')).toBeNull(); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it('handles fetchAuthSession errors by clearing names', async () => { |
| 144 | + mockFetchAuthSession.mockRejectedValueOnce(new Error('boom')); |
| 145 | + |
| 146 | + render(<NhsNotifyHeaderWithAccount />); |
| 147 | + |
| 148 | + await waitFor(() => { |
| 149 | + expect(screen.queryByTestId('account-display-name')).toBeNull(); |
| 150 | + expect(screen.queryByTestId('account-client-name')).toBeNull(); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + it('matches snapshot (authenticated)', () => { |
| 155 | + const container = render(<NhsNotifyHeaderWithAccount />); |
| 156 | + |
| 157 | + expect(container.asFragment()).toMatchSnapshot(); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe(`with 'routing' flag enabled`, () => { |
| 162 | + it('renders both the navigation links with correct hrefs', () => { |
| 163 | + render(<NhsNotifyHeaderWithAccount features={{ routing: true }} />); |
| 164 | + |
| 165 | + const nav = screen.getByTestId('navigation-links'); |
| 166 | + |
| 167 | + const templatesLink = within(nav).getByRole('link', { |
| 168 | + name: 'Templates', |
| 169 | + }); |
| 170 | + expect(templatesLink).toHaveAttribute('href', '/message-templates'); |
| 171 | + |
| 172 | + const plansLink = within(nav).getByRole('link', { |
| 173 | + name: 'Message plans', |
| 174 | + }); |
| 175 | + expect(plansLink).toHaveAttribute( |
| 176 | + 'href', |
| 177 | + '/templates-and-message-plans/message-plans' |
| 178 | + ); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe(`with 'routing' flag disabled`, () => { |
| 183 | + it('renders the templates link with correct href', () => { |
| 184 | + render(<NhsNotifyHeaderWithAccount features={{ routing: false }} />); |
| 185 | + |
| 186 | + const nav = screen.getByTestId('navigation-links'); |
| 187 | + |
| 188 | + const templatesLink = within(nav).getByRole('link', { |
| 189 | + name: 'Templates', |
| 190 | + }); |
| 191 | + expect(templatesLink).toHaveAttribute('href', '/message-templates'); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should not render the message plans link', () => { |
| 195 | + render(<NhsNotifyHeaderWithAccount features={{ routing: false }} />); |
| 196 | + |
| 197 | + const nav = screen.getByTestId('navigation-links'); |
| 198 | + |
| 199 | + const plansLink = within(nav).queryByRole('link', { |
| 200 | + name: 'Message plans', |
| 201 | + }); |
| 202 | + expect(plansLink).not.toBeInTheDocument(); |
| 203 | + }); |
| 204 | + }); |
| 205 | +}); |
0 commit comments