// @vitest-environment jsdom
import { act } from 'react';
import { createRoot } from 'react-dom/client';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { initialAppState, type SavedViewState } from '../../src/app/app-state';
const applyViewBookmark = vi.fn<(view: SavedViewState) => void>();
const app = {
...initialAppState,
dataset: { fileName: 'synthetic.csv' },
workScopeId: 'synthetic-project',
activeSheetId: 'sheet-a',
view: 'records' as const,
search: 'visible subset',
sortColumn: 'Severity',
sortDirection: 'asc' as const,
titleColumn: 'Title',
statusFilter: 'deviation',
facetFilters: { severity: { values: ['high'] } },
pathFindingIds: ['visible-a', 'visible-b'],
actionOnly: true,
selectedRecordId: 'visible-b',
applyViewBookmark
};
vi.mock('../../src/app/AppProvider', () => ({ useApp: () => app }));
import { ViewBookmarks } from '../../src/features/view-bookmarks/ViewBookmarks';
afterEach(() => {
applyViewBookmark.mockReset();
localStorage.clear();
});
describe('view snapshot interaction', () => {
it('saves and reopens the exact view that is currently displayed', async () => {
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
const container = document.createElement('div');
const root = createRoot(container);
try {
await act(async () => root.render());
await act(async () => container.querySelector('.view-bookmark-create')!.click());
const input = container.querySelector('#view-bookmark-name')!;
await act(async () => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, 'Current view');
input.dispatchEvent(new Event('input', { bubbles: true }));
});
await act(async () => container.querySelector('.view-bookmark-dialog')!
.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })));
const reopen = [...container.querySelectorAll('.view-bookmark-entry')]
.find(button => button.textContent === 'Current view');
expect(reopen).toBeDefined();
await act(async () => reopen!.click());
expect(applyViewBookmark).toHaveBeenCalledWith(expect.objectContaining({
activeSheetId: 'sheet-a',
view: 'records',
search: 'visible subset',
statusFilter: 'deviation',
facetFilters: { severity: { values: ['high'] } },
pathFindingIds: ['visible-a', 'visible-b'],
actionOnly: true,
selectedRecordId: 'visible-b'
}));
} finally {
await act(async () => root.unmount());
container.remove();
}
});
});