fix(store): hold session texts whose set-aside fails, never overwrite them
This commit is contained in:
1 parent
1312b366b1
commit
e34b7f282e
19 files changed
+1093
-86
No files matched your search
@@ -51,7 +51,7 @@ const draft = () => ({
|
||||
|
||||
describe('initReport', () => {
|
||||
it('stamps version 3, id, createdAt, the sheet ref and one empty observation', () => {
|
||||
useReportStore.getState().initReport(draft(), sheet.sheet.ref)
|
||||
useReportStore.getState().initReport(draft(), sheet.sheet.ref, sheet)
|
||||
const report = useReportStore.getState().report!
|
||||
expect(report.version).toBe(3)
|
||||
expect(report.id).toBeTruthy()
|
||||
@@ -66,7 +66,7 @@ describe('initReport', () => {
|
||||
describe('observation actions', () => {
|
||||
it('adds, updates, moves and removes observations, keeping the active index sane', () => {
|
||||
const s = useReportStore.getState()
|
||||
s.initReport(draft(), sheet.sheet.ref)
|
||||
s.initReport(draft(), sheet.sheet.ref, sheet)
|
||||
s.addObservation()
|
||||
expect(useReportStore.getState().report!.observations).toHaveLength(2)
|
||||
expect(useReportStore.getState().activeObservationIndex).toBe(1)
|
||||
@@ -91,7 +91,7 @@ describe('observation actions', () => {
|
||||
|
||||
it('setters replace identity, conclusion and appointment; nextAppointment can be cleared', () => {
|
||||
const s = useReportStore.getState()
|
||||
s.initReport(draft(), sheet.sheet.ref)
|
||||
s.initReport(draft(), sheet.sheet.ref, sheet)
|
||||
s.setPractitioner({ name: 'Dr. X' })
|
||||
s.setAnimal({ speciesId: 'equine', name: 'Y' })
|
||||
s.setOwner({ name: 'Z' })
|
||||
@@ -124,7 +124,7 @@ describe('observation actions', () => {
|
||||
|
||||
describe('sessionStorage mirror and restore', () => {
|
||||
it('mirrors every change under osteo:draft and removes it on reset', () => {
|
||||
useReportStore.getState().initReport(draft(), sheet.sheet.ref)
|
||||
useReportStore.getState().initReport(draft(), sheet.sheet.ref, sheet)
|
||||
expect(JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).version).toBe(3)
|
||||
useReportStore.getState().reset()
|
||||
expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull()
|
||||
@@ -154,15 +154,17 @@ describe('sessionStorage mirror and restore', () => {
|
||||
kind: 'quarantined',
|
||||
})
|
||||
expect(useReportStore.getState().report).toBeNull()
|
||||
// R2 maintenance B: the slot already holds the first text, which is never replaced.
|
||||
sessionStorage.setItem(DRAFT_KEY, JSON.stringify({ version: 1 }))
|
||||
expect(useReportStore.getState().restoreDraft(sheet)).toEqual({
|
||||
kind: 'quarantined',
|
||||
expect(useReportStore.getState().restoreDraft(sheet)).toMatchObject({
|
||||
kind: 'held',
|
||||
held: [{ text: 'draft', reason: 'unreadable' }],
|
||||
})
|
||||
expect(useReportStore.getState().report).toBeNull()
|
||||
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBe(
|
||||
JSON.stringify({ version: 1 }),
|
||||
)
|
||||
useReportStore.getState().initReport(draft(), sheet.sheet.ref)
|
||||
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBe('{not json')
|
||||
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(JSON.stringify({ version: 1 }))
|
||||
useReportStore.getState().discardSessionText('draft')
|
||||
useReportStore.getState().initReport(draft(), sheet.sheet.ref, sheet)
|
||||
const before = useReportStore.getState().report
|
||||
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet)))
|
||||
useReportStore.getState().restoreDraft(sheet)
|
||||
@@ -172,10 +174,10 @@ describe('sessionStorage mirror and restore', () => {
|
||||
it('createReportStore accepts another persistence adapter', () => {
|
||||
const saved: unknown[] = []
|
||||
const store = createReportStore(
|
||||
{ loadText: () => null, save: (r) => saved.push(r) },
|
||||
{ loadText: () => null, save: () => undefined },
|
||||
{ loadText: () => null, save: (r) => saved.push(r) > 0 },
|
||||
{ loadText: () => null, save: () => true },
|
||||
)
|
||||
store.getState().initReport(draft(), sheet.sheet.ref)
|
||||
store.getState().initReport(draft(), sheet.sheet.ref, sheet)
|
||||
expect(saved.at(-1)).toMatchObject({ version: 3 })
|
||||
})
|
||||
})
|
||||
@@ -200,7 +202,7 @@ describe('archive of an imported Step 6 PDF', () => {
|
||||
it('hydrateImport keeps the archive and mirrors it under osteo:archive', () => {
|
||||
const report = minimalReport(sheet)
|
||||
const archive = archiveEnvelope(sheet, report)
|
||||
useReportStore.getState().hydrateImport({ report, archive })
|
||||
useReportStore.getState().hydrateImport({ report, archive }, sheet)
|
||||
expect(useReportStore.getState().archive).toEqual(archive)
|
||||
expect(JSON.parse(sessionStorage.getItem(ARCHIVE_KEY)!).report.id).toBe(
|
||||
report.id,
|
||||
@@ -211,12 +213,12 @@ describe('archive of an imported Step 6 PDF', () => {
|
||||
it('the archive is written at the session edges only, never on an edit', () => {
|
||||
const archiveSaves: unknown[] = []
|
||||
const store = createReportStore(
|
||||
{ loadText: () => null, save: () => undefined },
|
||||
{ loadText: () => null, save: (a) => archiveSaves.push(a) },
|
||||
{ loadText: () => null, save: () => true },
|
||||
{ loadText: () => null, save: (a) => archiveSaves.push(a) > 0 },
|
||||
)
|
||||
const report = minimalReport(sheet)
|
||||
const archive = archiveEnvelope(sheet, report)
|
||||
store.getState().hydrateImport({ report, archive })
|
||||
store.getState().hydrateImport({ report, archive }, sheet)
|
||||
expect(archiveSaves).toEqual([archive])
|
||||
store.getState().setConclusion('edited')
|
||||
store.getState().addObservation()
|
||||
@@ -230,7 +232,7 @@ describe('archive of an imported Step 6 PDF', () => {
|
||||
it('restoreDraft brings the archive back with the draft', () => {
|
||||
const report = minimalReport(sheet, { conclusion: 'kept' })
|
||||
const archive = archiveEnvelope(sheet, report)
|
||||
useReportStore.getState().hydrateImport({ report, archive })
|
||||
useReportStore.getState().hydrateImport({ report, archive }, sheet)
|
||||
// Emptying the state clears the draft mirror, not the archive mirror.
|
||||
useReportStore.setState({ report: null, archive: null })
|
||||
expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull()
|
||||
@@ -248,21 +250,21 @@ describe('archive of an imported Step 6 PDF', () => {
|
||||
it.each([
|
||||
[
|
||||
'initReport',
|
||||
() => useReportStore.getState().initReport(draft(), sheet.sheet.ref),
|
||||
() => useReportStore.getState().initReport(draft(), sheet.sheet.ref, sheet),
|
||||
],
|
||||
[
|
||||
'hydrateImport without an archive',
|
||||
() =>
|
||||
useReportStore
|
||||
.getState()
|
||||
.hydrateImport({ report: minimalReport(sheet), archive: null }),
|
||||
.hydrateImport({ report: minimalReport(sheet), archive: null }, sheet),
|
||||
],
|
||||
['reset', () => useReportStore.getState().reset()],
|
||||
])('%s clears the archive and removes its mirror', (_name, act) => {
|
||||
const report = minimalReport(sheet)
|
||||
useReportStore
|
||||
.getState()
|
||||
.hydrateImport({ report, archive: archiveEnvelope(sheet, report) })
|
||||
.hydrateImport({ report, archive: archiveEnvelope(sheet, report) }, sheet)
|
||||
act()
|
||||
expect(useReportStore.getState().archive).toBeNull()
|
||||
expect(sessionStorage.getItem(ARCHIVE_KEY)).toBeNull()
|
||||
|
||||
Reference in new issue
Block a user