UNPKG

@websolutespa/payload-plugin-bowl

Version:

Bowl PayloadCms plugin of the BOM Repository

318 lines (317 loc) 11.6 kB
import { Collection, config } from '@/test'; import { clearContext, endUsers, getContext, users } from '@websolutespa/test/payload'; import * as Papa from 'papaparse'; import { v4 as uuid } from 'uuid'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; describe('collection.service', ()=>{ let payload; let client; let userToken; let endUserToken; let apiKey; beforeAll(async ()=>{ const context = await getContext(config); payload = context.payload; client = context.client; userToken = await client.getToken('users', users.admin); endUserToken = await client.getToken('end_users', endUsers.user); apiKey = users.api.apiKey; }); afterAll(async ()=>{ await clearContext(); }); describe('Collection index get', ()=>{ const items = [ { name: uuid() }, { name: uuid() } ]; beforeAll(async ()=>{ await Promise.all(items.map(async (item)=>await client.post(Collection.slug, item, { headers: { Authorization: `JWT ${userToken}` } }))); }); it('should return all documents in the collection if "pagination" is set to false', async ()=>{ const docs = await client.get(`/${Collection.slug}?pagination=false`, { headers: { Authorization: `JWT ${userToken}` } }); items.forEach((item)=>expect(docs.map((doc)=>doc.name)).toContain(item.name)); }); }); describe('Collection bulk patch', ()=>{ it('should allow to insert new documents', async ()=>{ const items = [ { name: uuid() }, { name: uuid() } ]; const bulkRequest = items.map((item)=>({ action: 'insert', item: item })); const result = await client.patch(`/${Collection.slug}/bulk`, bulkRequest, { headers: { Authorization: `JWT ${userToken}` } }); const { docs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); const names = docs.map((doc)=>doc.name); items.forEach((item)=>expect(names).toContain(item.name)); }); it('should allow to update existing documents', async ()=>{ const { docs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); docs.forEach((doc)=>doc.name = uuid()); const bulkRequest = docs.map((item)=>({ action: 'update', item: { id: item.id, name: item.name } })); const result = await client.patch(`/${Collection.slug}/bulk`, bulkRequest, { headers: { Authorization: `JWT ${userToken}` } }); const { docs: updatedDocs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); updatedDocs.forEach((doc)=>delete doc.updatedAt); docs.forEach((doc)=>delete doc.updatedAt); expect(updatedDocs).toEqual(docs); }); describe('bulk delete action', ()=>{ beforeAll(async ()=>{ const items = [ { name: uuid() }, { name: uuid() } ]; await Promise.all(items.map(async (item)=>await client.post(Collection.slug, item, { headers: { Authorization: `JWT ${userToken}` } }))); }); it('should allow to delete existing documents', async ()=>{ const { docs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); const bulkRequest = docs.map((item)=>({ action: 'delete', item: { id: item.id } })); const result = await client.patch(`/${Collection.slug}/bulk`, bulkRequest, { headers: { Authorization: `JWT ${userToken}` } }); const { docs: updatedDocs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); const updatedIds = updatedDocs.map((doc)=>doc.id); const initialIds = docs.map((doc)=>doc.id); expect(updatedIds).not.toContain(initialIds); }); }); }); describe('Collection import post', ()=>{ let items = [ { name: uuid() }, { name: uuid() } ]; it('should import a list of documents', async ()=>{ await client.post(`/${Collection.slug}/import`, { items }, { headers: { Authorization: `JWT ${userToken}` } }); const { docs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); docs.forEach((doc)=>expect(items.map((item)=>item.name)).toContain(doc.name)); }); it('should import a list of documents (append)', async ()=>{ const appendItem = { name: uuid() }; items.push(appendItem); await client.post(`/${Collection.slug}/import?mode=append`, { items: [ appendItem ] }, { headers: { Authorization: `JWT ${userToken}` } }); const { docs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); docs.forEach((doc)=>expect(items.map((item)=>item.name)).toContain(doc.name)); }); it('should import a list of documents (replace)', async ()=>{ items = [ { name: uuid() } ]; await client.post(`/${Collection.slug}/import?mode=replace`, { items }, { headers: { Authorization: `JWT ${userToken}` } }); const { docs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); docs.forEach((doc)=>expect(items.map((item)=>item.name)).toContain(doc.name)); }); it('should import a list of documents (update)', async ()=>{ const { docs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); docs.forEach((doc)=>doc.name = uuid()); await client.post(`/${Collection.slug}/import?mode=update`, { items: docs }, { headers: { Authorization: `JWT ${userToken}` } }); const { docs: updatedDocs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); updatedDocs.forEach((doc)=>delete doc.updatedAt); docs.forEach((doc)=>delete doc.updatedAt); expect(updatedDocs).toEqual(docs); }); }); describe('Collection export get', ()=>{ beforeAll(async ()=>{ const items = [ { name: uuid() }, { name: uuid() } ]; await Promise.all(items.map(async (item)=>await client.post(Collection.slug, item, { headers: { Authorization: `JWT ${userToken}` } }))); }); it('should return a csv file containing all documents in the collection', async ()=>{ const { docs: items } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); const response = await client.GET(`/${Collection.slug}/export`, { headers: { Authorization: `JWT ${userToken}` } }); const csvString = await response.text(); const csvData = Papa.parse(csvString, { header: true }); expect(csvData.data).toEqual(items); }); }); // known issue: collectionUpdatePatch() does not work, needs to be fixed const testCollectionUpdatePatch = false; if (testCollectionUpdatePatch) { describe('Collection update patch', ()=>{ beforeAll(async ()=>{ const items = [ { name: uuid() }, { name: uuid() } ]; await Promise.all(items.map(async (item)=>await client.post(Collection.slug, item, { headers: { Authorization: `JWT ${userToken}` } }))); }); it('should allow to update existing documents', async ()=>{ const { docs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); docs.forEach((doc)=>doc.name = uuid()); const updateRequest = docs.map((doc)=>({ id: doc.id, name: doc.name })); const result = await client.patch(`/${Collection.slug}/update`, updateRequest, { headers: { Authorization: `JWT ${userToken}` } }); const { docs: updatedDocs } = await client.get(Collection.slug, { headers: { Authorization: `JWT ${userToken}` } }); updatedDocs.forEach((doc)=>delete doc.updatedAt); docs.forEach((doc)=>delete doc.updatedAt); expect(updatedDocs).toEqual(docs); }); }); } }); //# sourceMappingURL=collection.service.test.js.map