mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
99 lines
4.4 KiB
TypeScript
99 lines
4.4 KiB
TypeScript
// FE-COMP-PLACEHISTORY-001 to FE-COMP-PLACEHISTORY-007: the change history of a place of
|
|
// All places (PLC-14), behind the desktop section and the phone section.
|
|
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
import type { PlaceHistoryPageResponse, PlacesLibraryEntry } from '@trek/shared';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { historyEntries } from '../../../tests/helpers/placeExtras';
|
|
import { libraryEntry, listOrigin } from '../../../tests/helpers/placesLibrary';
|
|
import { placeHistoryRepo } from '../../repo/placeHistoryRepo';
|
|
import { usePlaceHistory } from './usePlaceHistory';
|
|
|
|
vi.mock('../../repo/placeHistoryRepo', () => ({ placeHistoryRepo: { history: vi.fn() } }));
|
|
|
|
const history = vi.mocked(placeHistoryRepo.history);
|
|
|
|
function mount(entry: PlacesLibraryEntry | null = libraryEntry()) {
|
|
return renderHook(({ e }: { e: PlacesLibraryEntry | null }) => usePlaceHistory(e), { initialProps: { e: entry } });
|
|
}
|
|
|
|
function page(from: number, count: number, next: string | null): PlaceHistoryPageResponse {
|
|
return { entries: historyEntries(count, from), next_cursor: next };
|
|
}
|
|
|
|
beforeEach(() => {
|
|
history.mockReset();
|
|
});
|
|
|
|
describe('usePlaceHistory', () => {
|
|
it('FE-COMP-PLACEHISTORY-001: reads the first page through the first origin', async () => {
|
|
history.mockResolvedValue(page(1, 2, null));
|
|
const { result } = mount();
|
|
await waitFor(() => expect(result.current.entries).toHaveLength(2));
|
|
expect(history).toHaveBeenCalledWith('trip', 42, { limit: 20 }, expect.any(AbortSignal));
|
|
expect(result.current.hasMore).toBe(false);
|
|
});
|
|
|
|
it('FE-COMP-PLACEHISTORY-002: a list origin reads through its saved place; no origin reads nothing', async () => {
|
|
history.mockResolvedValue(page(1, 1, null));
|
|
mount(libraryEntry({ origins: [listOrigin()] }));
|
|
await waitFor(() => expect(history).toHaveBeenCalledWith('list', 900, { limit: 20 }, expect.any(AbortSignal)));
|
|
history.mockClear();
|
|
const { result } = mount(null);
|
|
expect(history).not.toHaveBeenCalled();
|
|
expect(result.current.entries).toEqual([]);
|
|
expect(result.current.hasMore).toBe(false);
|
|
});
|
|
|
|
it('FE-COMP-PLACEHISTORY-003: a failed read shows no history', async () => {
|
|
history.mockRejectedValue(new Error('offline'));
|
|
const { result } = mount();
|
|
await waitFor(() => expect(history).toHaveBeenCalled());
|
|
expect(result.current.entries).toEqual([]);
|
|
expect(result.current.hasMore).toBe(false);
|
|
});
|
|
|
|
it('FE-COMP-PLACEHISTORY-004: more appends the next page with the cursor', async () => {
|
|
history.mockResolvedValueOnce(page(1, 2, '10')).mockResolvedValueOnce(page(3, 1, null));
|
|
const { result } = mount();
|
|
await waitFor(() => expect(result.current.hasMore).toBe(true));
|
|
await act(async () => {
|
|
await result.current.more();
|
|
});
|
|
expect(history).toHaveBeenLastCalledWith('trip', 42, { limit: 20, cursor: '10' });
|
|
expect(result.current.entries.map((e) => e.id)).toEqual([1, 2, 3]);
|
|
expect(result.current.hasMore).toBe(false);
|
|
expect(result.current.loadingMore).toBe(false);
|
|
});
|
|
|
|
it('FE-COMP-PLACEHISTORY-005: more does nothing on the last page', async () => {
|
|
history.mockResolvedValue(page(1, 1, null));
|
|
const { result } = mount();
|
|
await waitFor(() => expect(result.current.entries).toHaveLength(1));
|
|
await act(async () => {
|
|
await result.current.more();
|
|
});
|
|
expect(history).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('FE-COMP-PLACEHISTORY-006: a failed "more" keeps the list and lets another tap try again', async () => {
|
|
history.mockResolvedValueOnce(page(1, 2, '10')).mockRejectedValueOnce(new Error('offline'));
|
|
const { result } = mount();
|
|
await waitFor(() => expect(result.current.hasMore).toBe(true));
|
|
await act(async () => {
|
|
await result.current.more();
|
|
});
|
|
expect(result.current.entries).toHaveLength(2);
|
|
expect(result.current.hasMore).toBe(true);
|
|
expect(result.current.loadingMore).toBe(false);
|
|
});
|
|
|
|
it('FE-COMP-PLACEHISTORY-007: another entry hides the old history until its own arrives', async () => {
|
|
history.mockResolvedValueOnce(page(1, 2, null)).mockReturnValueOnce(new Promise(() => undefined));
|
|
const view = mount();
|
|
await waitFor(() => expect(view.result.current.entries).toHaveLength(2));
|
|
view.rerender({ e: libraryEntry({ origins: [listOrigin()] }) });
|
|
expect(view.result.current.entries).toEqual([]);
|
|
});
|
|
});
|