mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
test(places): cover the place sheet sections, tags, trip lists and the visited filter
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// FE-HOOK-TRIPVISITED-001 to FE-HOOK-TRIPVISITED-004: the trip's visited places for the place filter.
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { buildUser } from '../../tests/helpers/factories';
|
||||
import { resetAllStores, seedStore } from '../../tests/helpers/store';
|
||||
import { addListener, removeListener } from '../api/websocket';
|
||||
import { placeVisitsRepo } from '../repo/placeVisitsRepo';
|
||||
import { useAuthStore } from '../store/authStore';
|
||||
import { usePlaceVisitsStore } from '../store/placeVisitsStore';
|
||||
import { useTripVisitedIds } from './useTripVisitedIds';
|
||||
|
||||
const VISITED = { place_ids: [3, 7], places: [] };
|
||||
|
||||
beforeEach(() => {
|
||||
resetAllStores();
|
||||
vi.restoreAllMocks();
|
||||
vi.mocked(addListener).mockClear();
|
||||
vi.mocked(removeListener).mockClear();
|
||||
usePlaceVisitsStore.setState({ visited: {}, revision: 0 });
|
||||
seedStore(useAuthStore, { user: buildUser({ id: 1 }), isAuthenticated: true });
|
||||
});
|
||||
|
||||
describe('useTripVisitedIds', () => {
|
||||
it('FE-HOOK-TRIPVISITED-001: reads the trip and hands back its visited place ids', async () => {
|
||||
const read = vi.spyOn(placeVisitsRepo, 'tripVisited').mockResolvedValue(VISITED);
|
||||
const { result } = renderHook(() => useTripVisitedIds(4));
|
||||
expect(result.current.size).toBe(0);
|
||||
await waitFor(() => expect(result.current.size).toBe(2));
|
||||
expect(read).toHaveBeenCalledWith(4);
|
||||
expect(result.current.has(3)).toBe(true);
|
||||
expect(result.current.has(5)).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-HOOK-TRIPVISITED-002: without a trip nothing is read and the set stays empty', () => {
|
||||
const read = vi.spyOn(placeVisitsRepo, 'tripVisited').mockResolvedValue(VISITED);
|
||||
const { result } = renderHook(() => useTripVisitedIds(null));
|
||||
expect(read).not.toHaveBeenCalled();
|
||||
expect(result.current.size).toBe(0);
|
||||
});
|
||||
|
||||
it('FE-HOOK-TRIPVISITED-003: offline the set stays empty and nothing throws', async () => {
|
||||
const read = vi.spyOn(placeVisitsRepo, 'tripVisited').mockRejectedValue(new Error('offline'));
|
||||
const { result } = renderHook(() => useTripVisitedIds(4));
|
||||
await waitFor(() => expect(read).toHaveBeenCalledTimes(1));
|
||||
expect(result.current.size).toBe(0);
|
||||
});
|
||||
|
||||
it('FE-HOOK-TRIPVISITED-004: follows the trip events while mounted and stops on unmount', async () => {
|
||||
vi.spyOn(placeVisitsRepo, 'tripVisited').mockResolvedValue(VISITED);
|
||||
const { result, unmount, rerender } = renderHook(() => useTripVisitedIds(4));
|
||||
await waitFor(() => expect(result.current.size).toBe(2));
|
||||
const first = result.current;
|
||||
rerender();
|
||||
// The same answer keeps the same set, so the filters do not run again.
|
||||
expect(result.current).toBe(first);
|
||||
expect(addListener).toHaveBeenCalledTimes(1);
|
||||
const listener = vi.mocked(addListener).mock.calls[0][0];
|
||||
unmount();
|
||||
expect(removeListener).toHaveBeenCalledWith(listener);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user