U2 Task 7 (A2, A4, A5, A7, A12, A15; R-3, R-4, R-12, R-13, R-14). The work store's `creation` owns the new shape, its form, its view and F: a click with the point tool, a closed tracing or a keyboard « Ajouter … » opens « Nouveau point / zone / accès », a modal without a history layer over the right column, mounted beside EditOnly so a read-only flip keeps it (« Créer », « Replacer », « Reprendre » off). A new zone or link is a creation from its first vertex. The ghost takes the form's category colour; the point tool previews under a mouse; the canvas carries data-creating (other markers dimmed, unselectable). « Replacer le point » / « Reprendre le tracé » hide the dialog and keep the form; the strip offers « Revenir aux propriétés », « Retirer le dernier sommet », « Abandonner ». « Créer »: one insert, tool back to « Sélectionner », the new element selected, « Point créé · Défaire », focus on its row. ✕, Échap, « Abandonner » and Back always ask « Abandonner la création ? » (DiscardConfirm inside the dialog; DrawingGuardDialog elsewhere); answerDrawing drops the creation. Undo, its shortcuts and the line's « Défaire » are inactive during a creation. missingFields replaces formProblem (MissingSummary shared with the edit form); the C26 notice has no numbers. CreationPanel, formProblem, withGeometry and the keys required.fields, required.names and create_cancel are gone; GeometryGroup is imported by RepairDialog only. Removed or rewritten assertions: - tab « holds a point at the frame centre »: « Centre x » = 50 -> the ghost circle cx = 50; plus tool back to Sélectionner, « Point créé », one undo step. - tab « refuses a creation without laterality »: the general line « Choisissez la catégorie et la latéralité » -> only « Choisissez une latéralité. » and « À compléter : Latéralité. », focus on it. - tab « applies the geometry group to a pending zone »: removed (A8, no coordinate field) -> « places a triangle at the centre: the ghost shows it, Créer inserts it »; vertex edits after Créer are the e2e keyboard test. - tab C26 « proposes C »: « x = 15, y = 15 » in the notice -> the R-4 text, the notice inside the dialog; cancel test « Centre x » = 0 -> ghost cx = 0. - tab « offers the correction when the core refuses a pending point » (typed cx 98): removed, no typed coordinate is possible; the verdict path stays covered by the C26 click tests and correctionFor units. - tab H5 « a typed radius shows the core issue »: removed (A8); the size refusal is the slider's (Task 4 tests). - tab H6 « a second click moves the pending point »: -> « Replacer le point », then the click moves the ghost, the form kept. - tab H7 « Centre x/y » = 30/50 -> ghost cx/cy. - tab tracing Enter: « x du sommet 3 » -> ghost points; « drops the tracing on Escape » -> Escape asks first (board 32 row 3), Continuer keeps, Abandonner drops; new « Annuler le tracé acts at once ». - keep-alive « a pending creation is kept and focused » and « a tracing … is kept and focused »: the tab now asks first (R-12) -> « Continuer » keeps the tracing / the form, « Abandonner » goes. - sections « its panel takes the focus » -> its dialog's [data-creation-focus] takes it. - no-loss « pending creation with its name » (hidden with the body) -> the dialog stays across the crossing, read-only, Créer off (R-14). - point-edit A5 « Rayon » = 7 / 9 -> ghost r = 7 / 9. - e2e editor-annotations keyboard test « numeric position » (cx field 250) -> UX_TESTS Keyboard: zone added, vertex added and removed, moved by the move mode, validated, saved. The « Fermer le tracé » step runs under expectNoShift again (shortcut removed). - e2e editor-geometry C26: the proposed x, y read from the notice text -> from the overlay's correction circle; the R-4 text checked in the dialog. - e2e editor-deselect « Escape cancels the tracing only » -> Escape asks, « Abandonner » cancels it, the selection kept. - e2e editor-shell-crossing « keeps its tool and its pending creation » (field hidden) -> the dialog stays read-only, Créer off, then editable on widening.
459 lines
16 KiB
TypeScript
459 lines
16 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
act,
|
|
cleanup,
|
|
render,
|
|
screen,
|
|
waitFor,
|
|
within,
|
|
} from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import {
|
|
Link,
|
|
MemoryRouter,
|
|
Route,
|
|
Routes,
|
|
useLocation,
|
|
} from 'react-router-dom'
|
|
import {
|
|
createFakeSupabase,
|
|
fakeProfile,
|
|
fakeUser,
|
|
type FakeSupabase,
|
|
} from '../helpers/fakeSupabase'
|
|
|
|
const mocks = vi.hoisted(() => ({ fake: null as unknown }))
|
|
vi.mock('@/lib/supabase', () => ({
|
|
getSupabaseClient: () => (mocks.fake as FakeSupabase | null)?.client ?? null,
|
|
isSupabaseConfigured: () => mocks.fake !== null,
|
|
}))
|
|
|
|
import AppLayout from '@/components/shell/AppLayout'
|
|
import { monogram } from '@/components/shell/UserBadge'
|
|
import { useAuthStore } from '@/state/authStore'
|
|
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { fr } from '@/i18n/fr'
|
|
import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog'
|
|
import { emptyForm } from '@/editor/annotations/creation'
|
|
import { traceVertex } from '@/editor/annotations/creationWork'
|
|
import { guardDrawing } from '@/editor/annotations/drawingGuard'
|
|
import {
|
|
guardWork,
|
|
resetWork,
|
|
markRedraw,
|
|
setModePreview,
|
|
startMove,
|
|
startRedraw,
|
|
useAnnotationWork,
|
|
} from '@/editor/annotations/workStore'
|
|
|
|
let fake: FakeSupabase
|
|
|
|
beforeEach(() => {
|
|
useLocaleStore.setState({ locale: 'fr' })
|
|
fake = createFakeSupabase()
|
|
mocks.fake = fake
|
|
useAuthStore.setState({
|
|
status: 'signed_in',
|
|
user: fakeUser(),
|
|
profile: fakeProfile({ display_name: 'Alice Local' }),
|
|
roles: [],
|
|
avatar: null,
|
|
avatarUrl: null,
|
|
})
|
|
})
|
|
afterEach(() => {
|
|
cleanup()
|
|
vi.unstubAllGlobals()
|
|
useLeaveGuardStore.getState().arm(false)
|
|
})
|
|
|
|
/** A matchMedia whose queries match from `widthRem`; `fire` flips it to a wider screen. */
|
|
function stubScreen(widthRem: number) {
|
|
const listeners = new Map<string, Set<(e: { matches: boolean }) => void>>()
|
|
const min = (query: string) => Number(/min-width: (\d+)rem/.exec(query)![1])
|
|
vi.stubGlobal('matchMedia', (query: string) => ({
|
|
matches: widthRem >= min(query),
|
|
addEventListener: (_: string, fn: (e: { matches: boolean }) => void) => {
|
|
listeners.set(query, (listeners.get(query) ?? new Set()).add(fn))
|
|
},
|
|
removeEventListener: (_: string, fn: (e: { matches: boolean }) => void) => {
|
|
listeners.get(query)?.delete(fn)
|
|
},
|
|
}))
|
|
const fire = (query: string) =>
|
|
act(() => listeners.get(query)?.forEach((fn) => fn({ matches: true })))
|
|
return Object.assign(fire, {
|
|
listening: (query: string) => (listeners.get(query)?.size ?? 0) > 0,
|
|
})
|
|
}
|
|
|
|
function mount(path = '/app') {
|
|
return render(
|
|
<MemoryRouter initialEntries={[path]}>
|
|
<Routes>
|
|
<Route path="/" element={<p>local tool</p>} />
|
|
<Route path="/app" element={<AppLayout />}>
|
|
<Route index element={<p>dashboard page</p>} />
|
|
<Route path="account" element={<p>account page</p>} />
|
|
<Route
|
|
path="admin/sheets/:sheetId/draft"
|
|
element={<p>draft page</p>}
|
|
/>
|
|
<Route path="admin/sheets/:sheetId" element={<p>overview page</p>} />
|
|
</Route>
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
)
|
|
}
|
|
|
|
describe('AppLayout', () => {
|
|
it('renders the sidebar navigation, the user and the page', () => {
|
|
mount()
|
|
const nav = screen.getByRole('navigation', { name: fr['shell.nav_label'] })
|
|
expect(nav).toHaveTextContent(fr['shell.nav.dashboard'])
|
|
expect(nav).toHaveTextContent(fr['shell.nav.account'])
|
|
expect(nav).toHaveTextContent(fr['shell.nav.local_tool'])
|
|
expect(screen.getAllByText('Alice Local').length).toBeGreaterThan(0)
|
|
expect(screen.getByText('dashboard page')).toBeInTheDocument()
|
|
})
|
|
|
|
it('gives the draft editor the whole width, every other page the 4xl column', () => {
|
|
const column = (path: string) => {
|
|
const view = mount(path)
|
|
const wrapper = screen.getByText(/page$/).parentElement
|
|
view.unmount()
|
|
return wrapper
|
|
}
|
|
expect(column('/app/admin/sheets/s-1/draft')).not.toHaveClass('max-w-4xl')
|
|
expect(column('/app/admin/sheets/s-1')).toHaveClass('max-w-4xl')
|
|
expect(column('/app')).toHaveClass('max-w-4xl')
|
|
})
|
|
|
|
it('links the top-bar avatar and name to the account page', async () => {
|
|
const user = userEvent.setup()
|
|
mount()
|
|
const link = screen.getByTestId('topbar-account')
|
|
expect(link).toHaveAttribute('href', '/app/account')
|
|
expect(link).toHaveTextContent('Alice Local')
|
|
expect(link).toHaveAttribute('aria-label', fr['shell.nav.account'])
|
|
await user.click(link)
|
|
expect(await screen.findByText('account page')).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows the photo everywhere once the store holds a blob URL, the monogram otherwise', () => {
|
|
mount()
|
|
expect(
|
|
screen.queryAllByRole('img', { name: fr['account.avatar.alt'] }),
|
|
).toHaveLength(0)
|
|
expect(screen.getAllByText('AL').length).toBeGreaterThan(0)
|
|
cleanup()
|
|
useAuthStore.setState({ avatarUrl: 'blob:photo' })
|
|
mount()
|
|
const imgs = screen.getAllByRole('img', { name: fr['account.avatar.alt'] })
|
|
expect(imgs.length).toBeGreaterThanOrEqual(2)
|
|
imgs.forEach((img) => expect(img).toHaveAttribute('src', 'blob:photo'))
|
|
expect(screen.queryByText('AL')).toBeNull()
|
|
})
|
|
|
|
it('marks the active destination', () => {
|
|
mount('/app/account')
|
|
const nav = screen.getByRole('navigation', { name: fr['shell.nav_label'] })
|
|
const link = within(nav).getByRole('link', {
|
|
name: new RegExp(fr['shell.nav.account']),
|
|
})
|
|
expect(link).toHaveAttribute('aria-current', 'page')
|
|
})
|
|
|
|
it('opens the mobile drawer from the menu button and closes it on navigation', async () => {
|
|
const user = userEvent.setup()
|
|
stubScreen(40)
|
|
mount()
|
|
await user.click(
|
|
screen.getByRole('button', { name: fr['shell.open_menu'] }),
|
|
)
|
|
const drawer = await screen.findByRole('dialog', {
|
|
name: fr['shell.nav_label'],
|
|
})
|
|
expect(drawer).toBeInTheDocument()
|
|
const links = screen.getAllByRole('link', {
|
|
name: new RegExp(fr['shell.nav.account']),
|
|
})
|
|
await user.click(links[links.length - 1])
|
|
expect(await screen.findByText('account page')).toBeInTheDocument()
|
|
})
|
|
|
|
it('signs out and returns to the local tool', async () => {
|
|
const user = userEvent.setup()
|
|
mount()
|
|
const [button] = screen.getAllByRole('button', {
|
|
name: fr['shell.sign_out'],
|
|
})
|
|
await user.click(button)
|
|
expect(fake.auth.signOut).toHaveBeenCalled()
|
|
expect(await screen.findByText('local tool')).toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
describe('AppLayout, editor sidebar from 1 280 px', () => {
|
|
const sidebar = () => document.querySelector('aside') as HTMLElement
|
|
const topBarMenu = () =>
|
|
screen.getByRole('button', { name: fr['shell.open_menu'], hidden: true })
|
|
|
|
it('uses the xl class set on the editor path, the lg set elsewhere', () => {
|
|
mount('/app/admin/sheets/s-1/draft')
|
|
expect(sidebar()).toHaveClass('xl:flex')
|
|
expect(sidebar()).not.toHaveClass('lg:flex')
|
|
expect(topBarMenu()).toHaveClass('xl:hidden')
|
|
expect(screen.getByRole('heading', { level: 1, hidden: true })).toHaveClass(
|
|
'xl:block',
|
|
)
|
|
cleanup()
|
|
mount('/app/admin/sheets/s-1')
|
|
expect(sidebar()).toHaveClass('lg:flex')
|
|
expect(sidebar()).not.toHaveClass('xl:flex')
|
|
expect(topBarMenu()).toHaveClass('lg:hidden')
|
|
})
|
|
|
|
it('keeps the sidebar fixed while the content scrolls, with its own scroll', () => {
|
|
mount('/app/admin/sheets/s-1/draft')
|
|
expect(sidebar()).toHaveClass(
|
|
'sticky',
|
|
'top-0',
|
|
'h-dvh',
|
|
'self-start',
|
|
'overflow-y-auto',
|
|
)
|
|
cleanup()
|
|
mount('/app/admin/sheets/s-1')
|
|
expect(sidebar()).toHaveClass(
|
|
'sticky',
|
|
'top-0',
|
|
'h-dvh',
|
|
'self-start',
|
|
'overflow-y-auto',
|
|
)
|
|
})
|
|
|
|
it('keeps the drawer open at 1 180 px in the editor, closes it from 1 280 px', async () => {
|
|
const user = userEvent.setup()
|
|
const fire = stubScreen(73.75)
|
|
mount('/app/admin/sheets/s-1/draft')
|
|
await user.click(topBarMenu())
|
|
expect(
|
|
await screen.findByRole('dialog', { name: fr['shell.nav_label'] }),
|
|
).toBeInTheDocument()
|
|
expect(fire.listening('(min-width: 64rem)')).toBe(false)
|
|
expect(fire.listening('(min-width: 80rem)')).toBe(true)
|
|
fire('(min-width: 80rem)')
|
|
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
})
|
|
|
|
it('closes the drawer on the 64rem query on other pages', async () => {
|
|
const user = userEvent.setup()
|
|
const fire = stubScreen(50)
|
|
mount('/app')
|
|
await user.click(topBarMenu())
|
|
await screen.findByRole('dialog', { name: fr['shell.nav_label'] })
|
|
fire('(min-width: 64rem)')
|
|
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
})
|
|
|
|
it('closes at setup when its query already matches', async () => {
|
|
const user = userEvent.setup()
|
|
stubScreen(73.75)
|
|
mount('/app')
|
|
await user.click(topBarMenu())
|
|
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
cleanup()
|
|
mount('/app/admin/sheets/s-1/draft')
|
|
await user.click(topBarMenu())
|
|
expect(
|
|
await screen.findByRole('dialog', { name: fr['shell.nav_label'] }),
|
|
).toBeInTheDocument()
|
|
})
|
|
|
|
it('asks before leaving from a drawer link while changes are unsaved', async () => {
|
|
const user = userEvent.setup()
|
|
stubScreen(73.75)
|
|
mount('/app/admin/sheets/s-1/draft')
|
|
useLeaveGuardStore.getState().arm(true)
|
|
await user.click(topBarMenu())
|
|
const drawer = await screen.findByRole('dialog', {
|
|
name: fr['shell.nav_label'],
|
|
})
|
|
const account = new RegExp(fr['shell.nav.account'])
|
|
await user.click(within(drawer).getByRole('link', { name: account }))
|
|
expect(screen.queryByText('account page')).toBeNull()
|
|
expect(await screen.findByText(fr['leave.title'])).toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
describe('AppLayout and a started move on the drawing (R-12)', () => {
|
|
const STORED = { type: 'circle' as const, cx: 1, cy: 2, r: 3 }
|
|
afterEach(() => {
|
|
useLeaveGuardStore.getState().setAsk(null)
|
|
resetWork()
|
|
})
|
|
function mountDraft() {
|
|
render(
|
|
<MemoryRouter initialEntries={['/app/admin/sheets/s-1/draft']}>
|
|
<Routes>
|
|
<Route path="/app" element={<AppLayout />}>
|
|
<Route path="account" element={<AccountPage />} />
|
|
<Route
|
|
path="admin/sheets/:sheetId/draft"
|
|
element={
|
|
<>
|
|
<p>draft page</p>
|
|
<Link to="/app/account" replace state={{ from: 'draft' }}>
|
|
page link
|
|
</Link>
|
|
<a href="/export.json" download onClick={onDownload}>
|
|
download link
|
|
</a>
|
|
<DrawingGuardDialog />
|
|
</>
|
|
}
|
|
/>
|
|
</Route>
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
)
|
|
// As the draft route registers it: the question, and whether anything is at stake.
|
|
useLeaveGuardStore
|
|
.getState()
|
|
.setAsk(
|
|
guardDrawing,
|
|
() => guardWork(useAnnotationWork.getState()) !== null,
|
|
)
|
|
}
|
|
/** The download anchor's own click: whether a handler before it took the click. */
|
|
const downloads: boolean[] = []
|
|
function onDownload(event: {
|
|
defaultPrevented: boolean
|
|
preventDefault(): void
|
|
}) {
|
|
downloads.push(event.defaultPrevented)
|
|
// happy-dom would try to follow it.
|
|
event.preventDefault()
|
|
}
|
|
function AccountPage() {
|
|
const location = useLocation()
|
|
const from = (location.state as { from?: string } | null)?.from ?? 'none'
|
|
return <p>account page from {from}</p>
|
|
}
|
|
const account = () =>
|
|
screen.getAllByRole('link', {
|
|
name: new RegExp(fr['shell.nav.account']),
|
|
})[0]
|
|
const started = () => {
|
|
startMove('a', 'doc')
|
|
setModePreview({ ...STORED, cx: 9 }, STORED)
|
|
}
|
|
|
|
it('a link asks the drawing question, not the form one; « Abandonner » follows it', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
started()
|
|
await user.click(account())
|
|
expect(
|
|
screen.getByRole('dialog', { name: 'Abandonner le déplacement ?' }),
|
|
).toBeInTheDocument()
|
|
expect(screen.queryByText(fr['leave.title'])).toBeNull()
|
|
expect(screen.queryByText(/account page/)).toBeNull()
|
|
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
|
|
expect(await screen.findByText(/account page/)).toBeInTheDocument()
|
|
})
|
|
|
|
it('with sheet changes too: the drawing question, then the leave question', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
started()
|
|
useLeaveGuardStore.getState().arm(true)
|
|
await user.click(account())
|
|
expect(screen.queryByText(fr['leave.title'])).toBeNull()
|
|
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
|
|
expect(await screen.findByText(fr['leave.title'])).toBeInTheDocument()
|
|
expect(screen.queryByText(/account page/)).toBeNull()
|
|
})
|
|
|
|
it('a started redraw asks « Abandonner le nouveau tracé ? » before a link', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
startRedraw('a', 'doc')
|
|
markRedraw(true)
|
|
await user.click(account())
|
|
expect(
|
|
screen.getByRole('dialog', { name: 'Abandonner le nouveau tracé ?' }),
|
|
).toBeInTheDocument()
|
|
expect(screen.queryByText(/account page/)).toBeNull()
|
|
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
|
|
expect(await screen.findByText(/account page/)).toBeInTheDocument()
|
|
expect(useAnnotationWork.getState().mode).toBeNull()
|
|
})
|
|
|
|
it('a move with nothing started is not at stake: the link goes at once', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
startMove('a', 'doc')
|
|
await user.click(account())
|
|
expect(await screen.findByText(/account page/)).toBeInTheDocument()
|
|
})
|
|
|
|
it("with nothing at stake a page link is the router's own: its state reaches the page", async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await user.click(screen.getByRole('link', { name: 'page link' }))
|
|
expect(
|
|
await screen.findByText('account page from draft'),
|
|
).toBeInTheDocument()
|
|
})
|
|
|
|
it('during a creation a link asks « Abandonner la création ? » (R-12)', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
act(() =>
|
|
traceVertex(
|
|
{
|
|
viewId: 'v',
|
|
frame: null,
|
|
origin: 'tool',
|
|
form: emptyForm({ categories: [] } as never),
|
|
},
|
|
'zone',
|
|
[1, 1],
|
|
),
|
|
)
|
|
await user.click(account())
|
|
const ask = screen.getByRole('dialog', { name: 'Abandonner la création ?' })
|
|
await user.click(within(ask).getByRole('button', { name: 'Continuer' }))
|
|
expect(screen.queryByText(/account page/)).toBeNull()
|
|
expect(useAnnotationWork.getState().creation).not.toBeNull()
|
|
await user.click(account())
|
|
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
|
|
expect(await screen.findByText(/account page/)).toBeInTheDocument()
|
|
expect(useAnnotationWork.getState().creation).toBeNull()
|
|
})
|
|
|
|
it('a download link is never taken, even with a started move', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
started()
|
|
downloads.length = 0
|
|
await user.click(screen.getByRole('link', { name: 'download link' }))
|
|
expect(downloads).toEqual([false])
|
|
expect(screen.queryByRole('dialog')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('monogram', () => {
|
|
it('takes initials, else two letters, else ?', () => {
|
|
expect(monogram('Alice Local', undefined)).toBe('AL')
|
|
expect(monogram('Alice', undefined)).toBe('AL')
|
|
expect(monogram(undefined, 'bob@x.test')).toBe('BO')
|
|
expect(monogram('', '')).toBe('?')
|
|
})
|
|
})
|