/* eslint-env jest */ import { act } from 'react'; import { createRoot, type Root } from 'react-dom/client'; import { SessionNotice } from '../session-notice'; import { isRejectedSession } from '../../../lib/session'; import { renewSiteSignIn } from '../../../service/auth/auth.service'; jest.mock('../../../service/auth/auth.service', () => ({ renewSiteSignIn: jest.fn(), })); ( globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean } ).IS_REACT_ACT_ENVIRONMENT = true; let root: Root; let host: HTMLDivElement; beforeEach(() => { host = document.createElement('div'); document.body.appendChild(host); root = createRoot(host); (renewSiteSignIn as jest.Mock).mockReset(); }); afterEach(() => { act(() => root.unmount()); host.remove(); }); /** * Presses the notice's Reconnect button and waits for the answer. * * @returns {Promise} */ const pressReconnect = async (): Promise => { const button = host.querySelector('button') as HTMLButtonElement; await act(async () => { button.click(); }); }; it('reads a refused or expired token as a sign-in to redo, and nothing else', () => { expect(isRejectedSession(401)).toBe(true); expect(isRejectedSession(403)).toBe(true); for (const status of [undefined, 404, 422, 500, 502]) { expect(isRejectedSession(status)).toBe(false); } }); it('says nothing while the token is accepted', async () => { await act(async () => { root.render(); }); expect(host.textContent).toBe(''); }); it('offers Reconnect for a refused token and says when it did not work', async () => { (renewSiteSignIn as jest.Mock).mockResolvedValue({ renewed: false }); await act(async () => { root.render(); }); expect(host.textContent).toContain('turned down this site’s sign-in'); expect(host.textContent).toContain('Reconnect'); await pressReconnect(); expect(renewSiteSignIn).toHaveBeenCalledTimes(1); expect(host.textContent).toContain('Reconnecting did not work'); }); it('treats a renewal call that fails outright like one that gave no token', async () => { (renewSiteSignIn as jest.Mock).mockRejectedValue(new Error('rest down')); await act(async () => { root.render(); }); await pressReconnect(); expect(host.textContent).toContain('Reconnecting did not work'); });