The JSON rescue copy could not be restored in the app, a draft that traps the engine left the tab on a reload screen and spent a recovery on every read, and an unsaved or in-progress report could be lost by a reload outside the editor or by New report / Import without warning. Home import reads the copy (content-sniffed, 16 MiB cap, strict UTF-8) through the core's draft.restore before any session change; a trapping (draft, archive) pair is held in the resolution panel and only re-read on Retry; ReplaceSessionDialog guards New report and Import; the beforeunload guard is app-wide while unsaved. Supersedes BDR-055's "never held" for `internal` only.
70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
/**
|
|
* The beforeunload guard is app-wide (R3 final checks): prevented exactly while the report
|
|
* is unsaved, on any page; App mounts it, the editor no longer registers its own.
|
|
*/
|
|
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { act, cleanup, render, renderHook, waitFor } from '@testing-library/react'
|
|
import App from '@/app/App'
|
|
import { useUnsavedGuard } from '@/app/useUnsavedGuard'
|
|
import { useReportStore } from '@/state/reportStore'
|
|
import type { SheetIndex } from '@/services/sheetIndex'
|
|
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
|
|
|
let sheet: SheetIndex
|
|
|
|
beforeAll(async () => {
|
|
sheet = await loadTestSheet()
|
|
})
|
|
beforeEach(() => {
|
|
sessionStorage.clear()
|
|
useReportStore.setState({ report: null, archive: null, saveFailed: false })
|
|
useReportStore.getState().hydrateImport({ report: minimalReport(sheet), archive: null }, sheet)
|
|
})
|
|
afterEach(() => {
|
|
cleanup()
|
|
vi.restoreAllMocks()
|
|
useReportStore.setState({ report: null, saveFailed: false })
|
|
})
|
|
|
|
const leave = () => {
|
|
const event = new Event('beforeunload', { cancelable: true })
|
|
window.dispatchEvent(event)
|
|
return event.defaultPrevented
|
|
}
|
|
|
|
function fillStorage() {
|
|
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
|
throw new DOMException('full', 'QuotaExceededError')
|
|
})
|
|
act(() => useReportStore.getState().setConclusion('edited after the quota'))
|
|
expect(useReportStore.getState().saveFailed).toBe(true)
|
|
}
|
|
|
|
describe('useUnsavedGuard', () => {
|
|
it('prevents the unload exactly while the report is unsaved', async () => {
|
|
renderHook(() => useUnsavedGuard())
|
|
expect(leave()).toBe(false)
|
|
fillStorage()
|
|
await waitFor(() => expect(leave()).toBe(true))
|
|
vi.restoreAllMocks()
|
|
act(() => useReportStore.getState().setConclusion('saved again'))
|
|
await waitFor(() => expect(leave()).toBe(false))
|
|
})
|
|
|
|
it('leaves the listener off once the hook unmounts', async () => {
|
|
const { unmount } = renderHook(() => useUnsavedGuard())
|
|
fillStorage()
|
|
await waitFor(() => expect(leave()).toBe(true))
|
|
unmount()
|
|
expect(leave()).toBe(false)
|
|
})
|
|
|
|
it('is mounted by App, on a page that is not the editor', async () => {
|
|
window.history.pushState({}, '', '/')
|
|
render(<App />)
|
|
expect(leave()).toBe(false)
|
|
fillStorage()
|
|
await waitFor(() => expect(leave()).toBe(true))
|
|
})
|
|
})
|