import { TestBed } from '@angular/core/testing'; import { describe, it, expect } from 'vitest'; import { Result, ok } from '@shared/kernel/fp'; import { BLOB_PRESENTER, BlobPresenter } from '@shared/application/blob-presenter'; import { StamRow, StamTable, toJson } from '@beheer/domain/stamdata'; import { StamdataAdapter } from '@beheer/infrastructure/stamdata.adapter'; import { StamdataStore } from './stamdata.store'; const table: StamTable = { id: 'professions', label: 'Opleiding → beroep', temporal: false, columns: [ { name: 'program', type: 'text', isKey: true, options: [] }, { name: 'beroep', type: 'text', isKey: false, options: [] }, ], }; const rows: StamRow[] = [{ program: 'geneeskunde', beroep: 'Arts' }]; /** A recording fake of BLOB_PRESENTER — records every call instead of touching the DOM, which is what TE-006's seam is for: the store's success path becomes assertable. */ function fakeBlobPresenter() { const opened: Blob[] = []; const downloaded: { blob: Blob; filename: string }[] = []; const presenter: BlobPresenter = { open: (blob) => opened.push(blob), download: (blob, filename) => downloaded.push({ blob, filename }), }; return { presenter, opened, downloaded }; } function setup(blobPresenter?: BlobPresenter): StamdataStore { const adapter: Partial = { list: (): Promise> => Promise.resolve(ok([table])), load: (): Promise> => Promise.resolve(ok({ table, rows: rows.map((r) => ({ ...r })) })), }; TestBed.configureTestingModule({ providers: [ { provide: StamdataAdapter, useValue: adapter }, ...(blobPresenter ? [{ provide: BLOB_PRESENTER, useValue: blobPresenter }] : []), ], }); return TestBed.inject(StamdataStore); } describe('StamdataStore undo/redo (WP-32)', () => { it('records a cell edit, undoes and redoes it', async () => { const store = setup(); await store.load(); expect(store.canUndo()).toBe(false); store.editCell(0, 'beroep', 'Chirurg'); expect(store.rows()[0]['beroep']).toBe('Chirurg'); expect(store.canUndo()).toBe(true); store.undo(); expect(store.rows()[0]['beroep']).toBe('Arts'); expect(store.canRedo()).toBe(true); store.redo(); expect(store.rows()[0]['beroep']).toBe('Chirurg'); expect(store.canRedo()).toBe(false); }); it('records addRow and undoes it', async () => { const store = setup(); await store.load(); store.addRow(); expect(store.rows().length).toBe(2); store.undo(); expect(store.rows().length).toBe(1); }); it('clears history when switching table', async () => { const store = setup(); await store.load(); store.addRow(); expect(store.canUndo()).toBe(true); await store.selectTable('professions'); expect(store.canUndo()).toBe(false); }); }); // --- RB-28 (TE-006): download() ends in BLOB_PRESENTER.download, not raw DOM calls, // so the seam makes both the guard's branches and the success path assertable. --- describe('StamdataStore.download (RB-28)', () => { it('does not call the presenter while the two-clause guard blocks (nothing dirty yet)', async () => { // Given a freshly loaded table with no edits — canDownload() is false. const { presenter, downloaded } = fakeBlobPresenter(); const store = setup(presenter); await store.load(); expect(store.canDownload()).toBe(false); // When download() is called... store.download(); // Then the guard's true branch fires and the presenter is never reached. expect(downloaded).toHaveLength(0); }); it('does not call the presenter while previewing a date, even with edits', async () => { // Given a loaded table with a real edit, but a preview date filter active. const { presenter, downloaded } = fakeBlobPresenter(); const store = setup(presenter); await store.load(); store.editCell(0, 'beroep', 'Chirurg'); store.setPreviewDate('2024-01-01'); expect(store.canDownload()).toBe(false); // When download() is called... store.download(); // Then the guard still blocks it. expect(downloaded).toHaveLength(0); }); it("passes toJson(...)'s exact output and the table id as the filename (success path)", async () => { // Given a loaded table with a valid, dirty edit — canDownload() is true. const { presenter, downloaded } = fakeBlobPresenter(); const store = setup(presenter); await store.load(); store.editCell(0, 'beroep', 'Chirurg'); expect(store.canDownload()).toBe(true); const expectedJson = toJson(store.table()!, store.rows()); // When download() is called... store.download(); // Then the presenter receives exactly one call, with toJson's output reaching the // file byte-for-byte and the table id as the file name. expect(downloaded).toHaveLength(1); expect(downloaded[0].filename).toBe('professions.json'); await expect(downloaded[0].blob.text()).resolves.toBe(expectedJson); }); });