Files
TREK/client/src/api/placesLibrary.test.ts
T
Maurice 20e3d2d9ae test(places): cover the place view, the rating row and the rating filter
Tests for the vote by entry key in the api and the repository, the rating
row and its voters, the rating filter and sort on both views, the edit form
without a choice of copy, and the place view's sections, aligned with the
renamed tag strings.
2026-11-29 23:36:43 +00:00

74 lines
3.4 KiB
TypeScript

// FE-API-PLACES-LIBRARY-001 to FE-API-PLACES-LIBRARY-004
//
// PLC-07/PLC-08: each method pins its verb, its path and its query, and hands
// back what the server answered.
import { http, HttpResponse } from 'msw';
import { beforeEach, describe, expect, it } from 'vitest';
import { server } from '../../tests/helpers/msw/server';
import { placesLibraryApi } from './placesLibrary';
const BASE = '/api/addons/collections/library/places';
const page = { entries: [], total: 0, counts: { all: 0, trip: 0, list: 0 }, next_cursor: null };
let calls: Array<{ path: string; query: Record<string, string> }> = [];
beforeEach(() => {
calls = [];
});
function record(data: object) {
return ({ request }: { request: Request }) => {
const url = new URL(request.url);
calls.push({ path: url.pathname, query: Object.fromEntries(url.searchParams) });
return HttpResponse.json(data);
};
}
describe('placesLibraryApi', () => {
it('FE-API-PLACES-LIBRARY-001: lists a page with the filters as query parameters', async () => {
server.use(http.get(BASE, record(page)));
expect(await placesLibraryApi.list()).toEqual(page);
expect(
await placesLibraryApi.list({ q: 'cafe', kind: 'list', collection_id: 3, limit: 20, cursor: 'abc' })
).toEqual(page);
expect(calls).toEqual([
{ path: BASE, query: {} },
{ path: BASE, query: { q: 'cafe', kind: 'list', collection_id: '3', limit: '20', cursor: 'abc' } },
]);
});
it('FE-API-PLACES-LIBRARY-002: opens one entry by the trip place or the saved place it names', async () => {
const answer = { entry: { key: 'trip:4' }, details: [] };
server.use(http.get(`${BASE}/trip/4`, record(answer)), http.get(`${BASE}/list/9`, record(answer)));
expect(await placesLibraryApi.detail('trip', 4)).toEqual(answer);
expect(await placesLibraryApi.detail('list', 9)).toEqual(answer);
expect(calls.map((call) => call.path)).toEqual([`${BASE}/trip/4`, `${BASE}/list/9`]);
});
it('FE-API-PLACES-LIBRARY-003: sends a tag filter as its names joined by commas, a comma in a name escaped', async () => {
server.use(http.get(BASE, record(page)));
await placesLibraryApi.list({ tags: ['Coffee', 'Food, drinks'], q: 'x' });
await placesLibraryApi.list({ tags: 'View' });
await placesLibraryApi.list({ tags: [] });
expect(calls.map((call) => call.query)).toEqual([{ q: 'x', tags: 'Coffee,Food\\, drinks' }, { tags: 'View' }, {}]);
});
it('FE-API-PLACES-LIBRARY-004: votes by the entry key, PUT with the stars and DELETE with the key to take it back', async () => {
const answer = { key: 'trip:4', rating: { average: 3, count: 1, mine: 3, votes: [] } };
const sent: Array<{ method: string; query: Record<string, string>; body: unknown }> = [];
const handle = async ({ request }: { request: Request }) => {
const url = new URL(request.url);
const body = request.method === 'PUT' ? await request.json() : null;
sent.push({ method: request.method, query: Object.fromEntries(url.searchParams), body });
return HttpResponse.json(answer);
};
server.use(http.put(`${BASE}/rating`, handle), http.delete(`${BASE}/rating`, handle));
expect(await placesLibraryApi.rate('trip:4', 3)).toEqual(answer);
expect(await placesLibraryApi.rate('trip:4', null)).toEqual(answer);
expect(sent).toEqual([
{ method: 'PUT', query: {}, body: { key: 'trip:4', rating: 3 } },
{ method: 'DELETE', query: { key: 'trip:4' }, body: null },
]);
});
});