fix(editor): Task 8 round 1, sync tag, cascaded leave ask
This commit is contained in:
1 parent
07bebbc855
commit
579d8afff4
10 files changed
+359
-83
No files matched your search
@@ -16,6 +16,7 @@ import {
|
||||
resolveSessionTag,
|
||||
} from '@/editor/history/sessionTag'
|
||||
import { useCitationDraft } from '@/editor/findings/citationDraft'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { edit, insert } from '../helpers/editorFixtures'
|
||||
import { defaultView } from '@/editor/navigation'
|
||||
@@ -625,7 +626,10 @@ describe('final review: leaving the editor with a dirty dialog (criterion 17)',
|
||||
await openFinding(user, /Node A/)
|
||||
await modify(user)
|
||||
await user.type(nameField(), ' bis')
|
||||
act(() => forgetSessionTag())
|
||||
act(() => {
|
||||
forgetSessionTag()
|
||||
useAuthStore.setState({ user: null })
|
||||
})
|
||||
await waitFor(() => expect(findingDialog()).toBeNull())
|
||||
const flags = Object.keys(sessionStorage).filter((k) =>
|
||||
k.startsWith('faunosteo:editor:left-draft:'),
|
||||
@@ -666,11 +670,16 @@ describe('a jump of several entries out of the editor (criterion 23)', () => {
|
||||
})
|
||||
|
||||
/** A history jump to `pathname`, as Chromium dispatches it before it commits. */
|
||||
function traverse(pathname: string, key: string, cancelable = true) {
|
||||
function traverse(
|
||||
pathname: string,
|
||||
key: string,
|
||||
cancelable = true,
|
||||
sameDocument = true,
|
||||
) {
|
||||
const url = new URL(pathname, window.location.origin).href
|
||||
const event = Object.assign(new Event('navigate', { cancelable }), {
|
||||
navigationType: 'traverse',
|
||||
destination: { url, key, sameDocument: true },
|
||||
destination: { url, key, sameDocument },
|
||||
})
|
||||
act(() => {
|
||||
api.dispatchEvent(event)
|
||||
@@ -733,6 +742,33 @@ describe('a jump of several entries out of the editor (criterion 23)', () => {
|
||||
expect(sessionStorage.getItem(flagKey())).not.toBeNull()
|
||||
})
|
||||
|
||||
it('a failed jump closes the question and keeps the draft', async () => {
|
||||
const user = await dirtyDialog()
|
||||
api.traverseTo.mockImplementation(() => ({
|
||||
committed: Promise.reject(new Error('gone')),
|
||||
finished: Promise.reject(new Error('gone')),
|
||||
}))
|
||||
traverse('/app/admin/sheets', 'list')
|
||||
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
|
||||
await waitFor(() =>
|
||||
expect(screen.queryByText('Abandonner les modifications ?')).toBeNull(),
|
||||
)
|
||||
expect(nameField()).toHaveValue('Node A bis')
|
||||
cleanup()
|
||||
expect(sessionStorage.getItem(flagKey())).not.toBeNull()
|
||||
})
|
||||
|
||||
it('a jump to another sheet is a leave; another document is not cancelled', async () => {
|
||||
await dirtyDialog()
|
||||
const other = '/app/admin/sheets/other-sheet/draft'
|
||||
expect(traverse('/app/admin', 'admin', true, false).defaultPrevented).toBe(
|
||||
false,
|
||||
)
|
||||
expect(screen.queryByText('Abandonner les modifications ?')).toBeNull()
|
||||
expect(traverse(other, 'other').defaultPrevented).toBe(true)
|
||||
expect(inDialog().getByText('Abandonner les modifications ?')).toBeVisible()
|
||||
})
|
||||
|
||||
it('a traversal inside the editor, or a clean dialog, is left alone', async () => {
|
||||
const user = await mount()
|
||||
await openFinding(user, /Node A/)
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/** Criterion 23: a history jump out of the editor asks every dirty layer, top down. */
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { act, cleanup, render, screen, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useState } from 'react'
|
||||
import { MemoryRouter } from 'react-router-dom'
|
||||
import DiscardConfirm from '@/components/editor/findings/DiscardConfirm'
|
||||
import { useDirtyGuard } from '@/components/editor/findings/useDirtyGuard'
|
||||
import useLeaveConfirm from '@/editor/history/useLeaveConfirm'
|
||||
import {
|
||||
useEditorNavigation,
|
||||
type Guards,
|
||||
type LayerName,
|
||||
} from '@/editor/useEditorNavigation'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
const path = '/app/admin/sheets/S/draft'
|
||||
|
||||
/** A dirty layer: its question and buttons once asked, gone once closed. */
|
||||
function Layer({ guards, layer }: { guards: Guards; layer: LayerName }) {
|
||||
const [open, setOpen] = useState(true)
|
||||
return open ? <Asking guards={guards} layer={layer} close={setOpen} /> : null
|
||||
}
|
||||
|
||||
function Asking(props: {
|
||||
guards: Guards
|
||||
layer: LayerName
|
||||
close: (open: boolean) => void
|
||||
}) {
|
||||
const guard = useDirtyGuard(props.guards, true, props.layer)
|
||||
return (
|
||||
<section data-testid={props.layer}>
|
||||
{guard.confirming && (
|
||||
<DiscardConfirm
|
||||
onKeepTyping={guard.keepTyping}
|
||||
onDiscard={guard.discard(() => props.close(false))}
|
||||
/>
|
||||
)}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function Editor() {
|
||||
const nav = useEditorNavigation('findings')
|
||||
useLeaveConfirm(nav.guards)
|
||||
return (
|
||||
<>
|
||||
<Layer guards={nav.guards} layer="finding" />
|
||||
<Layer guards={nav.guards} layer="reference" />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
class NavigationStub extends EventTarget {
|
||||
traverseTo = vi.fn((key: string) => {
|
||||
void key
|
||||
return { committed: Promise.resolve(), finished: Promise.resolve() }
|
||||
})
|
||||
}
|
||||
|
||||
let api: NavigationStub
|
||||
beforeEach(() => {
|
||||
useLocaleStore.setState({ locale: 'fr' })
|
||||
api = new NavigationStub()
|
||||
Object.defineProperty(window, 'navigation', {
|
||||
value: api,
|
||||
configurable: true,
|
||||
})
|
||||
render(
|
||||
<MemoryRouter initialEntries={[path]}>
|
||||
<Editor />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
})
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
Reflect.deleteProperty(window, 'navigation')
|
||||
})
|
||||
|
||||
function jump() {
|
||||
const url = new URL('/app/admin/sheets', window.location.origin).href
|
||||
const event = Object.assign(new Event('navigate', { cancelable: true }), {
|
||||
navigationType: 'traverse',
|
||||
destination: { url, key: 'list', sameDocument: true },
|
||||
})
|
||||
act(() => {
|
||||
api.dispatchEvent(event)
|
||||
})
|
||||
return event
|
||||
}
|
||||
|
||||
const layer = (name: LayerName) => screen.queryByTestId(name)
|
||||
const button = (name: LayerName, label: string) =>
|
||||
within(layer(name)!).getByRole('button', { name: label })
|
||||
|
||||
describe('a reference dialog over a finding dialog, both dirty', () => {
|
||||
it('asks the reference, then the finding, then the jump goes', async () => {
|
||||
const user = userEvent.setup()
|
||||
expect(jump().defaultPrevented).toBe(true)
|
||||
expect(within(layer('finding')!).queryByRole('group')).toBeNull()
|
||||
await user.click(button('reference', 'Abandonner'))
|
||||
expect(layer('reference')).toBeNull()
|
||||
expect(api.traverseTo).not.toHaveBeenCalled()
|
||||
await user.click(button('finding', 'Abandonner'))
|
||||
expect(api.traverseTo).toHaveBeenCalledTimes(1)
|
||||
expect(api.traverseTo).toHaveBeenCalledWith('list')
|
||||
})
|
||||
|
||||
it('« Continuer la saisie » on the second keeps the finding, no jump', async () => {
|
||||
const user = userEvent.setup()
|
||||
jump()
|
||||
await user.click(button('reference', 'Abandonner'))
|
||||
await user.click(button('finding', 'Continuer la saisie'))
|
||||
expect(layer('finding')).not.toBeNull()
|
||||
expect(within(layer('finding')!).queryByRole('group')).toBeNull()
|
||||
expect(api.traverseTo).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('« Continuer la saisie » on the first question keeps every draft', async () => {
|
||||
const user = userEvent.setup()
|
||||
jump()
|
||||
await user.click(button('reference', 'Continuer la saisie'))
|
||||
expect(layer('reference')).not.toBeNull()
|
||||
expect(layer('finding')).not.toBeNull()
|
||||
expect(api.traverseTo).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -27,12 +27,12 @@ afterEach(() => {
|
||||
})
|
||||
|
||||
describe('resolveSessionTag', () => {
|
||||
it('draws a 128-bit hex tag, stored with the account digest, never the id', async () => {
|
||||
it('draws a 128-bit hex tag, stored with the account fingerprint, never the id', async () => {
|
||||
const tag = await resolveSessionTag('account-a')
|
||||
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||
const record = stored()
|
||||
expect(record?.tag).toBe(tag)
|
||||
expect(record?.account).toMatch(/^[0-9a-f]{64}$/)
|
||||
expect(record?.account).toMatch(/^[0-9a-f]{16}$/)
|
||||
expect(sessionStorage.getItem(STORAGE_KEY)).not.toContain('account-a')
|
||||
})
|
||||
|
||||
@@ -69,12 +69,27 @@ describe('resolveSessionTag', () => {
|
||||
expect(await resolveSessionTag('account-a')).not.toBe(tag)
|
||||
})
|
||||
|
||||
it('keeps the tag in memory when no digest is available', async () => {
|
||||
vi.spyOn(crypto.subtle, 'digest').mockRejectedValue(new Error('insecure'))
|
||||
const tag = await resolveSessionTag('account-a')
|
||||
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||
expect(stored()).toBeNull()
|
||||
expect(await resolveSessionTag('account-a')).toBe(tag)
|
||||
it('works without crypto.subtle (http): kept on reload, rotated per account', () => {
|
||||
const subtle = Object.getOwnPropertyDescriptor(crypto, 'subtle')
|
||||
Object.defineProperty(crypto, 'subtle', {
|
||||
value: undefined,
|
||||
configurable: true,
|
||||
})
|
||||
try {
|
||||
const tag = resolveSessionTag('account-a')
|
||||
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||
expect(sessionStorage.getItem(STORAGE_KEY)).not.toContain('account-a')
|
||||
reload()
|
||||
expect(resolveSessionTag('account-a')).toBe(tag)
|
||||
reload()
|
||||
expect(resolveSessionTag('account-b')).not.toBe(tag)
|
||||
} finally {
|
||||
if (subtle) Object.defineProperty(crypto, 'subtle', subtle)
|
||||
}
|
||||
})
|
||||
|
||||
it('resolves at once: no wait before the first render', () => {
|
||||
expect(typeof resolveSessionTag('account-a')).toBe('string')
|
||||
})
|
||||
|
||||
it('reads a tampered record as none', async () => {
|
||||
@@ -85,6 +100,18 @@ describe('resolveSessionTag', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('the sweep of flags keyed by an account id (older shape)', () => {
|
||||
it('removes them on start and keeps the flags of a tag', () => {
|
||||
const older = 'faunosteo:editor:left-draft:S:account-a'
|
||||
const mine = leftDraftKey({ sheetId: 'S', tag: 'a'.repeat(32) })
|
||||
sessionStorage.setItem(older, '{}')
|
||||
sessionStorage.setItem(mine, '{}')
|
||||
startSessionTagSync()()
|
||||
expect(sessionStorage.getItem(older)).toBeNull()
|
||||
expect(sessionStorage.getItem(mine)).not.toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('forgetSessionTag and the sign-out', () => {
|
||||
it('rotates the tag of the same account and clears its flags', async () => {
|
||||
const old = await resolveSessionTag('account-a')
|
||||
|
||||
Reference in new issue
Block a user