mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
test(places): cover the place sheet sections, tags, trip lists and the visited filter
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
// FE-API-PLACE-ACTIVITIES-001
|
||||
//
|
||||
// PLC-18: each method pins its verb, its path and its body.
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { server } from '../../tests/helpers/msw/server';
|
||||
import { placeActivitiesApi } from './placeActivities';
|
||||
|
||||
const activities = { activities: [], can_link: true };
|
||||
const candidates = { candidates: [] };
|
||||
const entry = { activities: [], sources: [] };
|
||||
let calls: Array<[string, string, unknown]> = [];
|
||||
|
||||
beforeEach(() => {
|
||||
calls = [];
|
||||
});
|
||||
|
||||
function record(data: object) {
|
||||
return async ({ request }: { request: Request }) => {
|
||||
const text = request.method === 'POST' ? await request.text() : '';
|
||||
calls.push([request.method, new URL(request.url).pathname, text ? JSON.parse(text) : null]);
|
||||
return HttpResponse.json(data);
|
||||
};
|
||||
}
|
||||
|
||||
describe('placeActivitiesApi', () => {
|
||||
it('FE-API-PLACE-ACTIVITIES-001: reads, links and unlinks the activities of trip places, saved places and entries', async () => {
|
||||
server.use(
|
||||
http.get('/api/trips/4/places/7/activities', record(activities)),
|
||||
http.get('/api/trips/4/places/7/activities/candidates', record(candidates)),
|
||||
http.post('/api/trips/4/places/7/activities', record(activities)),
|
||||
http.delete('/api/trips/4/places/7/activities/reservations/9', record(activities)),
|
||||
http.delete('/api/trips/4/places/7/activities/tours/11', record(activities)),
|
||||
http.get('/api/addons/collections/places/8/activities', record(activities)),
|
||||
http.get('/api/addons/collections/places/8/activities/candidates', record(candidates)),
|
||||
http.post('/api/addons/collections/places/8/activities', record(activities)),
|
||||
http.delete('/api/addons/collections/places/8/activities/tours/5', record(activities)),
|
||||
http.get('/api/addons/collections/library/places/list/8/activities', record(entry))
|
||||
);
|
||||
expect(await placeActivitiesApi.tripPlace(4, 7)).toEqual(activities);
|
||||
expect(await placeActivitiesApi.tripCandidates(4, 7)).toEqual(candidates);
|
||||
expect(await placeActivitiesApi.linkTrip(4, 7, { reservation_id: 9 })).toEqual(activities);
|
||||
expect(await placeActivitiesApi.unlinkTripBooking(4, 7, 9)).toEqual(activities);
|
||||
expect(await placeActivitiesApi.unlinkTripTour(4, 7, 11)).toEqual(activities);
|
||||
expect(await placeActivitiesApi.savedPlace(8)).toEqual(activities);
|
||||
expect(await placeActivitiesApi.savedCandidates(8)).toEqual(candidates);
|
||||
expect(await placeActivitiesApi.linkSaved(8, 5)).toEqual(activities);
|
||||
expect(await placeActivitiesApi.unlinkSaved(8, 5)).toEqual(activities);
|
||||
expect(await placeActivitiesApi.libraryEntry('list', 8)).toEqual(entry);
|
||||
expect(calls).toEqual([
|
||||
['GET', '/api/trips/4/places/7/activities', null],
|
||||
['GET', '/api/trips/4/places/7/activities/candidates', null],
|
||||
['POST', '/api/trips/4/places/7/activities', { reservation_id: 9 }],
|
||||
['DELETE', '/api/trips/4/places/7/activities/reservations/9', null],
|
||||
['DELETE', '/api/trips/4/places/7/activities/tours/11', null],
|
||||
['GET', '/api/addons/collections/places/8/activities', null],
|
||||
['GET', '/api/addons/collections/places/8/activities/candidates', null],
|
||||
['POST', '/api/addons/collections/places/8/activities', { collection_tour_id: 5 }],
|
||||
['DELETE', '/api/addons/collections/places/8/activities/tours/5', null],
|
||||
['GET', '/api/addons/collections/library/places/list/8/activities', null],
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
// FE-API-PLACE-HISTORY-001
|
||||
//
|
||||
// PLC-14: the history pins its verb, its path and its query, and hands back
|
||||
// what the server answered.
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { server } from '../../tests/helpers/msw/server';
|
||||
import { placeHistoryApi } from './placeHistory';
|
||||
|
||||
const BASE = '/api/addons/collections/library/places';
|
||||
|
||||
describe('placeHistoryApi', () => {
|
||||
it('FE-API-PLACE-HISTORY-001: reads the history of the trip place or saved place it names, page by page', async () => {
|
||||
const calls: Array<{ path: string; query: Record<string, string> }> = [];
|
||||
const page = { entries: [], next_cursor: null };
|
||||
const record = ({ request }: { request: Request }) => {
|
||||
const url = new URL(request.url);
|
||||
calls.push({ path: url.pathname, query: Object.fromEntries(url.searchParams) });
|
||||
return HttpResponse.json(page);
|
||||
};
|
||||
server.use(http.get(`${BASE}/trip/4/history`, record), http.get(`${BASE}/list/9/history`, record));
|
||||
expect(await placeHistoryApi.history('trip', 4)).toEqual(page);
|
||||
expect(await placeHistoryApi.history('list', 9, { cursor: '12', limit: 20 })).toEqual(page);
|
||||
expect(calls).toEqual([
|
||||
{ path: `${BASE}/trip/4/history`, query: {} },
|
||||
{ path: `${BASE}/list/9/history`, query: { cursor: '12', limit: '20' } },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
// FE-API-PLACE-TAGS-001 to FE-API-PLACE-TAGS-003
|
||||
//
|
||||
// PLC-21: each method pins its verb, its path and its body, and hands back
|
||||
// what the server answered; the All places list sends a tag filter joined by commas.
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { server } from '../../tests/helpers/msw/server';
|
||||
import { placeTagsApi } from './placeTags';
|
||||
import { placesLibraryApi } from './placesLibrary';
|
||||
|
||||
const TAGS = '/api/addons/collections/library/places/tags';
|
||||
const tag = { id: 3, user_id: 1, trip_id: null, name: 'Wine', color: '#aa0000' };
|
||||
|
||||
let calls: Array<{ method: string; path: string; body: unknown; query: Record<string, string> }> = [];
|
||||
|
||||
beforeEach(() => {
|
||||
calls = [];
|
||||
});
|
||||
|
||||
function record(data: object) {
|
||||
return async ({ request }: { request: Request }) => {
|
||||
const url = new URL(request.url);
|
||||
const text = request.method === 'GET' || request.method === 'DELETE' ? '' : await request.text();
|
||||
calls.push({
|
||||
method: request.method,
|
||||
path: url.pathname,
|
||||
body: text ? JSON.parse(text) : null,
|
||||
query: Object.fromEntries(url.searchParams),
|
||||
});
|
||||
return HttpResponse.json(data);
|
||||
};
|
||||
}
|
||||
|
||||
describe('placeTagsApi', () => {
|
||||
it('FE-API-PLACE-TAGS-001: lists, creates, renames and deletes personal tags under /tags', async () => {
|
||||
server.use(
|
||||
http.get('/api/tags', record({ tags: [tag] })),
|
||||
http.post('/api/tags', record({ tag })),
|
||||
http.put('/api/tags/3', record({ tag })),
|
||||
http.delete('/api/tags/3', record({ success: true }))
|
||||
);
|
||||
expect(await placeTagsApi.list()).toEqual({ tags: [tag] });
|
||||
expect(await placeTagsApi.create({ name: 'Wine' })).toEqual({ tag });
|
||||
expect(await placeTagsApi.update(3, { color: '#000000' })).toEqual({ tag });
|
||||
expect(await placeTagsApi.remove(3)).toEqual({ success: true });
|
||||
expect(calls.map(({ method, path, body }) => ({ method, path, body }))).toEqual([
|
||||
{ method: 'GET', path: '/api/tags', body: null },
|
||||
{ method: 'POST', path: '/api/tags', body: { name: 'Wine' } },
|
||||
{ method: 'PUT', path: '/api/tags/3', body: { color: '#000000' } },
|
||||
{ method: 'DELETE', path: '/api/tags/3', body: null },
|
||||
]);
|
||||
});
|
||||
|
||||
it('FE-API-PLACE-TAGS-002: sets the tags of one entry and changes several', async () => {
|
||||
server.use(http.put(TAGS, record({ entry: null })), http.post(`${TAGS}/bulk`, record({ entries: [] })));
|
||||
expect(await placeTagsApi.setForEntry({ key: 'trip:4', tag_ids: [3] })).toEqual({ entry: null });
|
||||
expect(await placeTagsApi.bulk({ keys: ['list:9'], add: [3], remove: [5] })).toEqual({ entries: [] });
|
||||
expect(calls.map(({ method, path, body }) => ({ method, path, body }))).toEqual([
|
||||
{ method: 'PUT', path: TAGS, body: { key: 'trip:4', tag_ids: [3] } },
|
||||
{ method: 'POST', path: `${TAGS}/bulk`, body: { keys: ['list:9'], add: [3], remove: [5] } },
|
||||
]);
|
||||
});
|
||||
|
||||
it('FE-API-PLACE-TAGS-003: All places sends the tag filter as ids joined by commas', async () => {
|
||||
const page = { entries: [], total: 0, counts: { all: 0, trip: 0, list: 0 }, next_cursor: null };
|
||||
server.use(http.get('/api/addons/collections/library/places', record(page)));
|
||||
await placesLibraryApi.list({ tag_ids: [3, 7], q: 'x' });
|
||||
await placesLibraryApi.list({ tag_ids: '5' });
|
||||
await placesLibraryApi.list({ tag_ids: [] });
|
||||
expect(calls.map((call) => call.query)).toEqual([{ q: 'x', tag_ids: '3,7' }, { tag_ids: '5' }, {}]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
// FE-API-PLACE-VISITS-001
|
||||
//
|
||||
// D7/PLC-10 to PLC-23: each read pins its verb, its path and its query; there is nothing to write.
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { server } from '../../tests/helpers/msw/server';
|
||||
import { placeVisitsApi } from './placeVisits';
|
||||
|
||||
let calls: Array<[string, string]> = [];
|
||||
|
||||
beforeEach(() => {
|
||||
calls = [];
|
||||
});
|
||||
|
||||
function record(data: object) {
|
||||
return ({ request }: { request: Request }) => {
|
||||
const url = new URL(request.url);
|
||||
calls.push([request.method, `${url.pathname}${url.search}`]);
|
||||
return HttpResponse.json(data);
|
||||
};
|
||||
}
|
||||
|
||||
describe('placeVisitsApi', () => {
|
||||
it('FE-API-PLACE-VISITS-001: reads the visits and the media at the routes the server serves', async () => {
|
||||
server.use(
|
||||
http.get('/api/place-visits', record({ visits: [], total: 0 })),
|
||||
http.get('/api/place-visits/place', record({ visits: [], visit_count: 0, last_visit: null })),
|
||||
http.get('/api/trips/4/place-visits/mine', record({ place_ids: [], places: [] })),
|
||||
http.get(
|
||||
'/api/addons/collections/library/places/trip/3/media',
|
||||
record({ images: [], journey_photos: [], files: [] })
|
||||
)
|
||||
);
|
||||
|
||||
expect(await placeVisitsApi.list({ trip_id: 4 })).toEqual({ visits: [], total: 0 });
|
||||
expect(await placeVisitsApi.forPlace('trip', 3)).toEqual({ visits: [], visit_count: 0, last_visit: null });
|
||||
expect(await placeVisitsApi.tripVisited(4)).toEqual({ place_ids: [], places: [] });
|
||||
expect(await placeVisitsApi.media('trip', 3)).toEqual({ images: [], journey_photos: [], files: [] });
|
||||
expect(Object.keys(placeVisitsApi).sort()).toEqual(['forPlace', 'list', 'media', 'tripVisited']);
|
||||
|
||||
expect(calls).toEqual([
|
||||
['GET', '/api/place-visits?trip_id=4'],
|
||||
['GET', '/api/place-visits/place?kind=trip&id=3'],
|
||||
['GET', '/api/trips/4/place-visits/mine'],
|
||||
['GET', '/api/addons/collections/library/places/trip/3/media'],
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user