import { act, cleanup, fireEvent, render, screen, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { afterEach, expect, it, vi } from 'vitest' import { MemoryRouter } from 'react-router-dom' import { SummaryQA } from '../src/components/SummaryQA' import { MobileNav } from '../src/components/layout/MobileNav' vi.mock('@/api/summaryQA', () => ({ summaryQAApi: { get: vi.fn().mockResolvedValue({ id: 'c', messages: [], web_search_supported: true }) } })) vi.mock('@/lib/auth', () => ({ useAuth: () => ({ isAdmin: false }) })) afterEach(() => { cleanup(); vi.restoreAllMocks(); vi.unstubAllGlobals() }) it('porte le trigger dans body hors du main scrollable, avec wrapper fixe et retour du focus', async () => { vi.spyOn(window, 'scrollTo').mockImplementation(() => {}) const result = render(
) Object.defineProperty(result.container.querySelector('main'), 'scrollTo', { value: vi.fn() }) const action = screen.getByRole('button', { name: 'Ouvrir le chat de cette synthèse' }) const wrapper = action.parentElement! expect(wrapper.parentElement).toBe(document.body) expect(wrapper).toHaveClass('fixed') expect(wrapper.className).toContain('safe-area-inset-right') expect(within(result.container).queryByRole('button', { name: 'Ouvrir le chat de cette synthèse' })).not.toBeInTheDocument() fireEvent.scroll(result.container.querySelector('main')!, { target: { scrollTop: 700 } }) expect(action.parentElement).toBe(wrapper) await userEvent.click(action) expect(await screen.findByRole('dialog')).toBeInTheDocument() await userEvent.keyboard('{Escape}') expect(action).toHaveFocus() }) it('publie la distance réelle nav→bas viewport et réagit au ResizeObserver, resize et rotation', () => { let top = 730, height = 120 vi.stubGlobal('innerHeight', 850) vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(() => new DOMRect(0, top, 375, height)) let notify!: () => void const observe = vi.fn(), disconnect = vi.fn() vi.stubGlobal('ResizeObserver', class { constructor(callback: () => void) { notify = callback } observe = observe disconnect = disconnect }) const result = render() const offset = () => document.documentElement.style.getPropertyValue('--mobile-nav-offset') expect(offset()).toBe('120px') expect(observe).toHaveBeenCalledWith(screen.getByRole('navigation'), { box: 'border-box' }) top = 700; height = 150 act(() => notify()) expect(offset()).toBe('150px') // A moved nav with the same height must still update on viewport changes. top = 680 fireEvent(window, new Event('resize')) expect(offset()).toBe('170px') top = 650 fireEvent(window, new Event('orientationchange')) expect(offset()).toBe('200px') height = 0; top = 0 fireEvent(window, new Event('resize')) expect(offset()).toBe('0px') result.unmount() expect(disconnect).toHaveBeenCalledOnce() expect(offset()).toBe('') fireEvent(window, new Event('resize')) expect(offset()).toBe('') })