Files
tatort/tests/TatortList.test.ts

95 lines
3.9 KiB
TypeScript

import { fireEvent, getByTestId, queryAllByTestId, render, screen, within } from '@testing-library/svelte';
import { describe, expect, it, test, vi } from "vitest";
import TatortListPage from "../src/routes/(token-based)/list/[vorgang]/+page.svelte";
import { baseData } from './fixtures';
import { invalidateAll } from '$app/navigation';
// Mock für invalidateAll
vi.mock('$app/navigation', () => ({
invalidateAll: vi.fn()
}));
describe('Seite: Vorgangsansicht', () => {
test.todo('Share Link disabled wenn Liste leer');
describe('Szenario: Admin + Liste gefüllt', () => {
test.todo('Share Link Link generierung richtig');
it('ändert den Namen nach Speichern', async () => {
const testData = structuredClone(baseData);
const oldName = testData.crimesList[0].name;
const newName = 'Fall-B';
const list = testData.crimesList
const vorgangToken = testData.vorgang.vorgangToken
// Minimaler fetch-Mock
global.fetch = vi.fn().mockResolvedValue({ ok: true }) as typeof fetch;
const { getAllByTestId } = render(TatortListPage, { props: { data: testData } });
const firstItem = getAllByTestId('test-list-item')[0];
const editButton = within(firstItem).getByTestId('edit-button');
await fireEvent.click(editButton);
const input = within(firstItem).getByTestId('test-input');
expect(input).toHaveValue(oldName)
await fireEvent.input(input, { target: { value: newName } });
const commitButton = within(firstItem).getByTestId('commit-button');
await fireEvent.click(commitButton);
// const fetchMock = global.fetch as ReturnType<typeof vi.fn>;
// console.log('Fetch calls:', fetchMock.mock.calls);
// // Erwartung: fetch wurde aufgerufen
// expect(global.fetch).toHaveBeenCalledWith(
// expect.stringContaining(`/api/list/${vorgangToken}/${oldName}`),
// expect.objectContaining({
// method: 'PUT',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify({vorgangToken, oldName, newName})
// })
// );
// expect(invalidateAll).toHaveBeenCalled();
// Erwartung: neuer Name ist sofort im DOM sichtbar
// expect(within(firstItem).getByRole('textbox')).toHaveValue(newName);
// const editedLink = within(firstItem).getByRole('link');
// const editedExpectedHref = `/view/${vorgangToken}/${newName}?pin=${testData.vorgang.vorgangPIN}`;
// expect(editedLink).toBeInTheDocument();
// expect(editedLink).toHaveAttribute('href', editedExpectedHref);
// expect(editedLink).toHaveAttribute('title', newName);
});
// it('entfernt das Listenelement nach Löschen', async () => {
// const testData = structuredClone(baseData);
// testData.url = new URL('https://example.com/vorgang-1'); // Fix für Invalid URL
// const toDelete = testData.crimesList[0];
// global.fetch = vi.fn().mockResolvedValue({ ok: true });
// render(TatortListPage, { props: { data: testData } });
// const deletedFirstItem = screen.getAllByTestId('test-list-item')[0];
// const deletedLink = within(deletedFirstItem).getByRole('link');
// const deletedExpectedHref = `/view/${testData.vorgang.vorgangToken}/${toDelete.name}?pin=${testData.vorgang.vorgangPIN}`;
// expect(deletedLink).toBeInTheDocument();
// expect(deletedLink).toHaveAttribute('href', deletedExpectedHref);
// expect(deletedLink).toHaveAttribute('title', toDelete.name);
// await fireEvent.click(within(deletedFirstItem).getByTestId('delete-button'));
// // Erwartung: fetch wurde aufgerufen
// expect(global.fetch).toHaveBeenCalledWith(
// expect.stringContaining(`/api/vorgang-1/${toDelete.name}`),
// expect.any(Object)
// );
// // Erwartung: Element ist nicht mehr im DOM
// expect(within(deletedFirstItem).getByRole('textbox')).toHaveValue(toDelete.name);
// });
});
});