Files
faunosteo/apps/app/tests/unit/editor-verification.test.tsx
T

240 lines
8.3 KiB
TypeScript

/** 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 } 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<typeof import('@faunosteo/supabase')>()),
getSheetVersion: vi.fn(),
downloadSheetDrawing: vi.fn(),
saveSheetDraft: vi.fn(),
getOfficialSheetAdmin: vi.fn(),
submitSheetReview: vi.fn(),
}))
const onSelectTab = vi.fn()
const reloadSheet = vi.fn()
const onGoTo = vi.fn()
const props = { readOnly: false, onSelectTab, onGoTo, 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()
onSelectTab.mockReset()
onGoTo.mockReset()
reloadSheet.mockReset()
})
describe('Vérification panel', () => {
it('shows the counts, the warning apart, the size and the groups', async () => {
await openReady()
render(<VerificationTab {...props} />)
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(<VerificationTab {...props} />)
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(onGoTo).toHaveBeenCalledWith('annotations', {
kind: 'annotation',
id: 'anno-bare',
})
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(<VerificationTab {...props} />)
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(<VerificationTab {...props} />)
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(<VerificationTab {...props} readOnly />)
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(<VerificationTab {...props} />)
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 trimmed note on the saved revision and reloads the sheet', async () => {
await openReady()
sb.submitSheetReview.mockResolvedValue('review-1' as never)
const user = userEvent.setup()
render(<VerificationTab {...props} />)
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(<VerificationTab {...props} />)
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(<VerificationTab {...props} />)
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('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(<VerificationTab {...props} />)
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())
})
})