@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
105 lines (104 loc) • 4.67 kB
JavaScript
import { config, getTestConfig, StorePageCollection } from '@/test';
import { clearContext, endUsers, getContext, users } from '@websolutespa/test/payload';
import { v4 as uuid } from 'uuid';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { options } from '../../options';
describe('store.service', ()=>{
let payload;
let client;
let userToken;
let endUserToken;
let apiKey;
let testConfig;
let query;
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;
testConfig = await getTestConfig(client, userToken);
query = `?market=${testConfig.documents.market.id}&locale=${options.defaultLocale}`;
});
afterAll(async ()=>{
await clearContext();
});
describe('Payload config', ()=>{
it('should have a endpoint with "/store" path and method "get"', ()=>{
expect(payload.config.endpoints.find((endpoint)=>endpoint.path === '/store' && endpoint.method === 'get')).toBeDefined();
});
});
describe('endpoint test requests', ()=>{
let pageDoc;
beforeAll(async ()=>{
const { doc } = await client.post(`/${StorePageCollection.slug}?locale=${testConfig.defaultLocale}`, {
title: uuid(),
category: testConfig.documents.category.id,
market: [
testConfig.documents.market.id
],
useSplat: true,
customField: uuid(),
_status: 'published'
}, {
headers: {
Authorization: `JWT ${userToken}`
}
});
pageDoc = await client.get(`/${StorePageCollection.slug}/${doc.id}${query}`, {
headers: {
Authorization: `users API-Key ${users.api.apiKey}`
}
});
});
describe('/store get', ()=>{
let store;
beforeAll(async ()=>{
store = await client.get(`/store?t=${uuid()}`, {
headers: {
Authorization: `users API-Key ${users.api.apiKey}`
}
});
});
it('should return an array containing all collections', async ()=>{
// console.log(payload.config.collections.map(x => x.slug).join(','));
const allCollections = payload.config.collections.map((collection)=>collection.slug).filter((slug)=>{
const disallowedCollection = options.actions.includes(slug) || options.users.includes(slug);
if (disallowedCollection) {
return false;
}
return ![
'payload-preferences',
'payload-migrations',
'payload-locked-documents'
].includes(slug);
});
allCollections.forEach((slug)=>expect(store[slug]).toBeDefined());
});
it('should return a locale element containing all the locale globals', async ()=>{
options.locales.forEach((locale)=>{
const code = typeof locale === 'string' ? locale : locale.code;
expect(store['locale'].find((doc)=>doc.id === code)).toBeDefined();
});
});
it('should return a route element containing all the routes', async ()=>{
// @see route.service.test.ts for specific tests about the route service
options.locales.forEach((locale)=>{
const code = typeof locale === 'string' ? locale : locale.code;
expect(store['route'].find((route)=>route.locale === code && route.page === pageDoc.id)).toBeDefined();
});
});
it('should return saved documents and their fields', async ()=>{
const storeDoc = store[StorePageCollection.slug][0];
expect(storeDoc).toBeDefined();
expect(storeDoc.title).toEqual({
en: pageDoc.title
});
expect(storeDoc.category.id).toEqual(pageDoc.category.id);
expect(storeDoc.customField).toEqual(pageDoc.customField);
});
});
});
});
//# sourceMappingURL=store.service.test.js.map