Files
TREK/client/src/components/Collections/PlaceHistorySection.test.tsx
T

73 lines
3.0 KiB
TypeScript

// FE-COMP-PLACEHISTSECTION-001 to FE-COMP-PLACEHISTSECTION-005: the change history of a
// place in the desktop sheet of All places (PLC-14), against a mocked hook.
import { historyEntries, historyEntry, historyState } from '../../../tests/helpers/placeExtras';
import { libraryEntry } from '../../../tests/helpers/placesLibrary';
import { fireEvent, render, screen } from '../../../tests/helpers/render';
import PlaceHistorySection from './PlaceHistorySection';
import { usePlaceHistory } from './usePlaceHistory';
vi.mock('./usePlaceHistory', () => ({ usePlaceHistory: vi.fn() }));
const hook = vi.mocked(usePlaceHistory);
const t = (k: string) => k;
function show(state = historyState()) {
hook.mockReturnValue(state);
render(<PlaceHistorySection entry={libraryEntry()} language="en-GB" t={t} />);
return state;
}
beforeEach(() => {
hook.mockReset();
});
describe('PlaceHistorySection', () => {
it('FE-COMP-PLACEHISTSECTION-001: is not there before the first change', () => {
show();
expect(screen.queryByText('placeHistory.title')).toBeNull();
});
it('FE-COMP-PLACEHISTSECTION-002: a change shows the detail, from and to, the trip, who and when', () => {
show(historyState({ entries: [historyEntry()] }));
expect(screen.getByText('placeHistory.field.name')).toBeInTheDocument();
expect(screen.getByText('Colosseo')).toHaveClass('line-through');
expect(screen.getByText('Colosseum')).toBeInTheDocument();
expect(screen.getByText(/Rome 2026/)).toHaveTextContent(/anna/);
expect(screen.queryByRole('button')).toBeNull();
});
it('FE-COMP-PLACEHISTSECTION-003: a list change names the list, a picture change only its detail', () => {
show(
historyState({
entries: [
historyEntry({
field: 'image',
old_value: '/a.jpg',
new_value: '/b.jpg',
origin: { kind: 'list', collection_id: 9, collection_name: 'Favourites', collection_place_id: 900 },
}),
],
})
);
expect(screen.getByText(/Favourites/)).toBeInTheDocument();
expect(screen.queryByText('/b.jpg')).toBeNull();
});
it('FE-COMP-PLACEHISTSECTION-004: shows three at first, then all, then reads more', () => {
const state = show(historyState({ entries: historyEntries(5), hasMore: true }));
expect(screen.getAllByRole('listitem')).toHaveLength(3);
fireEvent.click(screen.getByRole('button', { name: 'placeHistory.showAll' }));
expect(screen.getAllByRole('listitem')).toHaveLength(5);
expect(state.more).not.toHaveBeenCalled();
fireEvent.click(screen.getByRole('button', { name: 'placeHistory.more' }));
expect(state.more).toHaveBeenCalledTimes(1);
});
it('FE-COMP-PLACEHISTSECTION-005: once all are open and none are left, the button goes', () => {
show(historyState({ entries: historyEntries(4) }));
fireEvent.click(screen.getByRole('button', { name: 'placeHistory.showAll' }));
expect(screen.getAllByRole('listitem')).toHaveLength(4);
expect(screen.queryByRole('button')).toBeNull();
});
});