// FE-API-PLACE-IDENTITIES-001 to FE-API-PLACE-IDENTITIES-002 // // D5/PLC-13: each method pins its verb, its path and its body or 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 { placeIdentitiesApi } from './placeIdentities'; const BASE = '/api/addons/collections/library'; let calls: Array<[string, string, Record, unknown]> = []; beforeEach(() => { calls = []; }); function record(data: object) { return async ({ request }: { request: Request }) => { const url = new URL(request.url); const text = request.method === 'GET' ? '' : await request.text(); calls.push([request.method, url.pathname, Object.fromEntries(url.searchParams), text ? JSON.parse(text) : null]); return HttpResponse.json(data); }; } describe('placeIdentitiesApi', () => { it('FE-API-PLACE-IDENTITIES-001: links and unlinks uses of an own place', async () => { const answer = { entry: null }; server.use(http.post(`${BASE}/places/link`, record(answer)), http.post(`${BASE}/places/unlink`, record(answer))); expect(await placeIdentitiesApi.link({ uses: [{ kind: 'trip', id: 3 }], identity_id: 2 })).toEqual(answer); expect(await placeIdentitiesApi.unlink({ kind: 'list', id: 4 })).toEqual(answer); expect(calls).toEqual([ ['POST', `${BASE}/places/link`, {}, { uses: [{ kind: 'trip', id: 3 }], identity_id: 2 }], ['POST', `${BASE}/places/unlink`, {}, { kind: 'list', id: 4 }], ]); }); it('FE-API-PLACE-IDENTITIES-002: reads duplicates, dismisses, merges and undoes', async () => { server.use( http.get(`${BASE}/duplicates`, record({ duplicates: [], next_cursor: null })), http.post(`${BASE}/duplicates/dismiss`, record({ success: true })), http.post(`${BASE}/merges`, record({ merge_id: 5, entry: null })), http.post(`${BASE}/merges/5/undo`, record({ success: true })) ); expect(await placeIdentitiesApi.duplicates({ limit: 10 })).toEqual({ duplicates: [], next_cursor: null }); expect(await placeIdentitiesApi.dismiss('trip:1', 'list:2')).toEqual({ success: true }); expect(await placeIdentitiesApi.merge('trip:1', 'list:2')).toEqual({ merge_id: 5, entry: null }); expect(await placeIdentitiesApi.undoMerge(5)).toEqual({ success: true }); expect(calls).toEqual([ ['GET', `${BASE}/duplicates`, { limit: '10' }, null], ['POST', `${BASE}/duplicates/dismiss`, {}, { a: 'trip:1', b: 'list:2' }], ['POST', `${BASE}/merges`, {}, { leading: 'trip:1', other: 'list:2' }], ['POST', `${BASE}/merges/5/undo`, {}, null], ]); }); });