/** Lot J: the Vérification tab and the submission. */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { act, cleanup, render, screen, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { SheetFailure } from '@faunosteo/supabase' import VerificationTab from '@/components/editor/tabs/VerificationTab' import { ISSUE_CODES, issueMessageKey, issueTarget, placeKey, } from '@/editor/verification' import { getDict } from '@/i18n' import { useLocaleStore } from '@/state/localeStore' import { insert, point } from '../helpers/editorFixtures' import { baseText, commitMove, openReady, resetStore, sb, store, } from '../helpers/editorHarness' vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) })) vi.mock('@faunosteo/supabase', async (orig) => ({ ...(await orig()), getSheetVersion: vi.fn(), downloadSheetDrawing: vi.fn(), saveSheetDraft: vi.fn(), getOfficialSheetAdmin: vi.fn(), submitSheetReview: vi.fn(), })) const navigate = vi.fn() const reloadSheet = vi.fn() const props = { readOnly: false, navigate, reloadSheet } const SUBMIT = 'Soumettre à la revue' /** An annotation without geometry: a blocking error of the core. */ function textWithError(): string { const bare = point('anno-bare', 10, 10, 2) const value = { ...bare.value, geometry: null, name: { fr: 'Sans tracé', en: 'Bare' }, } return insert(baseText(), { kind: 'annotation', value } as typeof bare) } beforeEach(() => useLocaleStore.setState({ locale: 'fr' })) afterEach(() => { cleanup() resetStore() navigate.mockReset() reloadSheet.mockReset() }) describe('Vérification panel', () => { it('shows the counts, the warning apart, the size and the groups', async () => { await openReady() render() expect(screen.getByText('0 erreur bloquante')).toBeTruthy() expect(screen.getByText('1 avertissement')).toBeTruthy() expect(screen.getByText('1 élément à confirmer')).toBeTruthy() expect(screen.getByRole('heading', { name: 'Avertissements' })).toBeTruthy() expect( screen.getByText(/lié(e)? à aucune zone|liée à aucune zone/), ).toBeTruthy() expect(screen.getByText(/sur 2 097 152 o$/)).toBeTruthy() expect( screen.getByRole('heading', { name: 'Statuts par groupe' }), ).toBeTruthy() expect(screen.getByText(/n'empêche pas la soumission/)).toBeTruthy() }) it('names an error with its message and goes to the tab of the element', async () => { await openReady({ text: textWithError() }) const user = userEvent.setup() render() expect(screen.getByText('1 erreur bloquante')).toBeTruthy() expect(screen.getAllByText('Sans tracé')).toHaveLength(2) expect(screen.getByText(/n'a pas de géométrie/)).toBeTruthy() await user.click( screen.getAllByRole('button', { name: 'Aller à Sans tracé' })[0], ) expect(navigate).toHaveBeenCalledWith( { place: 'tab', tab: 'annotations', section: 'drawing', key: { kind: 'annotation', id: 'anno-bare' }, }, expect.any(HTMLElement), ) expect(screen.queryByText('Aucun problème détecté')).toBeNull() }) it('disables the submission with the hint while errors exist', async () => { await openReady({ text: textWithError() }) render() const button = screen.getByRole('button', { name: SUBMIT, }) as HTMLButtonElement expect(button.disabled).toBe(true) expect( screen.getByText("Corrigez l'erreur bloquante pour soumettre."), ).toBeTruthy() }) it('phrases the issues in English', async () => { useLocaleStore.setState({ locale: 'en' }) await openReady({ text: textWithError() }) render() expect(screen.getByText('1 blocking error')).toBeTruthy() expect(screen.getByText(/This annotation has no geometry/)).toBeTruthy() expect(screen.getAllByRole('button', { name: 'Go to Bare' })).toHaveLength( 2, ) }) it('hides the submission when read-only', async () => { await openReady() render() expect(screen.queryByRole('button', { name: SUBMIT })).toBeNull() expect(screen.getByText('0 erreur bloquante')).toBeTruthy() }) }) describe('Submission', () => { it('needs a note and the saved draft', async () => { await openReady() const user = userEvent.setup() render() const button = screen.getByRole('button', { name: SUBMIT, }) as HTMLButtonElement expect(button.disabled).toBe(true) expect(screen.getByText(/Saisissez la note/)).toBeTruthy() await user.type( screen.getByLabelText('Note de soumission (obligatoire)'), ' ', ) expect(button.disabled).toBe(true) act(() => void commitMove(60)) await user.type( screen.getByLabelText('Note de soumission (obligatoire)'), 'Prête', ) expect(button.disabled).toBe(true) expect( screen.getByText('Enregistrez le brouillon avant de le soumettre.'), ).toBeTruthy() }) it('submits the note as typed on the saved revision and reloads the sheet', async () => { await openReady() sb.submitSheetReview.mockResolvedValue('review-1' as never) const user = userEvent.setup() render() await user.type( screen.getByLabelText('Note de soumission (obligatoire)'), ' Prête ', ) await user.click(screen.getByRole('button', { name: SUBMIT })) await waitFor(() => expect(reloadSheet).toHaveBeenCalled()) expect(sb.submitSheetReview).toHaveBeenCalledWith(expect.anything(), { version_id: 'ver-1', expected_revision: 3, note: ' Prête ', }) }) it('shows a server refusal and keeps the note', async () => { await openReady() sb.submitSheetReview.mockRejectedValue(new SheetFailure('draft.frozen')) const user = userEvent.setup() render() await user.type( screen.getByLabelText('Note de soumission (obligatoire)'), 'Prête', ) await user.click(screen.getByRole('button', { name: SUBMIT })) expect(await screen.findByRole('alert')).toBeTruthy() expect(reloadSheet).not.toHaveBeenCalled() expect( ( screen.getByLabelText( 'Note de soumission (obligatoire)', ) as HTMLTextAreaElement ).value, ).toBe('Prête') }) it('opens the conflict read on a draft conflict and shows no message', async () => { await openReady() sb.submitSheetReview.mockRejectedValue(new SheetFailure('draft_conflict')) sb.getOfficialSheetAdmin.mockRejectedValue(new Error('x')) const user = userEvent.setup() render() await user.type( screen.getByLabelText('Note de soumission (obligatoire)'), 'Prête', ) await user.click(screen.getByRole('button', { name: SUBMIT })) await waitFor(() => expect(store.getState().conflict).toEqual({ status: 'unknown' }), ) expect(screen.queryByRole('alert')).toBeNull() expect(reloadSheet).not.toHaveBeenCalled() expect(sb.submitSheetReview).toHaveBeenCalledTimes(1) }) }) describe('issueTarget', () => { const at = (path: string) => issueTarget(doc(), path, 'fr')?.target const doc = () => JSON.parse(store.getState().documentText) it('each issue root names its place and the element it resolves to', async () => { await openReady({ text: textWithError() }) expect(at('views[0].frame')).toMatchObject({ place: 'tab', tab: 'annotations', section: 'views', key: { kind: 'view', id: 'view-main' }, }) expect(at('categories[0].name')).toMatchObject({ place: 'tab', tab: 'annotations', section: 'categories', key: { kind: 'category', id: 'cat-a' }, }) expect(at('vertebral_config.regions[0].count')).toMatchObject({ place: 'tab', tab: 'annotations', section: 'regions', }) expect(at('references[0].citation')).toMatchObject({ place: 'references', citedBy: null, }) expect(at('finding_zones[0].name')).toMatchObject({ place: 'tab', tab: 'findings', }) expect(at('annotations[0].geometry')).toMatchObject({ tab: 'annotations', section: 'drawing', }) }) it('a path whose element is gone names the place alone, and the label names it', async () => { await openReady({ text: textWithError() }) const gone = at('views[9].frame') expect(gone).toEqual({ place: 'tab', tab: 'annotations', section: 'views', key: null, }) expect(placeKey(gone!)).toBe('editor.place.views') expect(placeKey(at('references[9].citation')!)).toBe( 'editor.place.references', ) expect(placeKey(at('annotations[9].geometry')!)).toBe( 'editor.tab.annotations', ) }) }) describe('Issue messages', () => { it('has an FR and an EN message for every known code', () => { for (const code of ISSUE_CODES) { const key = issueMessageKey(code) expect(getDict('fr')[key], key).toBeTruthy() expect(getDict('en')[key], key).toBeTruthy() } }) it('falls back to a neutral message for an unknown code', () => { const key = issueMessageKey('something.new') expect(key).toBe('editor.verification.issue.unknown') expect(getDict('fr')[key]).toBeTruthy() }) }) describe('Submission after the tab is gone', () => { it('still reloads the sheet when the tab unmounted during the await', async () => { await openReady() let resolve: (id: never) => void = () => undefined sb.submitSheetReview.mockReturnValue(new Promise((r) => (resolve = r))) const user = userEvent.setup() const view = render() await user.type( screen.getByLabelText('Note de soumission (obligatoire)'), 'Prête', ) await user.click(screen.getByRole('button', { name: SUBMIT })) view.unmount() await act(async () => resolve('review-1' as never)) await waitFor(() => expect(reloadSheet).toHaveBeenCalled()) }) })