@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
300 lines (299 loc) • 12.6 kB
JavaScript
import { config, getRouteId, getTestConfig, PageCollection } 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('menu.service', ()=>{
let payload;
let client;
let userToken;
let endUserToken;
let apiKey;
let testConfig;
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);
});
afterAll(async ()=>{
await clearContext();
});
describe('Menu collection config', ()=>{
let menuCollection;
beforeAll(async ()=>{
menuCollection = payload.config.collections.find((collection)=>collection.slug === options.slug.menu);
});
it('should have a endpoint with "/" path', ()=>{
expect(menuCollection.endpoints && menuCollection.endpoints.find((endpoint)=>endpoint.path === '/')).toBeDefined();
});
it('should have a endpoint with "/:id" path', ()=>{
expect(menuCollection.endpoints && menuCollection.endpoints.find((endpoint)=>endpoint.path === '/:id')).toBeDefined();
});
});
describe('Menu items', ()=>{
let pageDoc;
beforeAll(async ()=>{
({ doc: pageDoc } = await client.post(PageCollection.slug, {
title: uuid(),
category: testConfig.documents.category.id,
market: [
testConfig.documents.market.id
],
_status: 'published'
}, {
headers: {
Authorization: `JWT ${userToken}`
}
}));
});
describe('Menu link item', ()=>{
let postData;
let menu;
let menuItem;
beforeAll(async ()=>{
postData = {
id: uuid(),
items: [
{
id: uuid(),
type: 'link',
href: 'https://www.example.com',
title: uuid(),
items: [],
target: '_blank',
blockType: 'Level 0'
}
]
};
const { doc: menuDoc } = await client.post(options.slug.menu, postData, {
headers: {
Authorization: `JWT ${userToken}`
}
});
menu = await client.get(`/${options.slug.menu}/${menuDoc.id}?market=${testConfig.documents.market.id}&locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${users.api.apiKey}`
}
});
menuItem = menu.items[0];
});
it('should be defined', async ()=>{
expect(menuItem).toBeDefined();
});
it('should have a valid type property', async ()=>{
expect(menuItem.type).toEqual(postData.items[0].type);
});
it('should have a valid href property', async ()=>{
expect(menuItem.href).toEqual(postData.items[0].href);
});
it('should have a valid id property', async ()=>{
expect(menuItem.id).toEqual(postData.items[0].id);
});
it('should have a valid items property', async ()=>{
expect(menuItem.items).toEqual(postData.items[0].items);
});
it('should have a valid target property', async ()=>{
expect(menuItem.target).toEqual(postData.items[0].target);
});
it('should have a valid title property', async ()=>{
expect(menuItem.title).toEqual(postData.items[0].title);
});
});
describe('Menu group item', ()=>{
let menuDoc;
let postData;
let menu;
let menuItem;
beforeAll(async ()=>{
postData = {
id: uuid(),
items: [
{
id: uuid(),
type: 'group',
title: uuid(),
target: '_blank',
blockType: 'Level 0',
items: [
{
id: uuid(),
type: 'link',
href: 'https://www.example.com',
title: uuid(),
items: [],
target: '_blank',
blockType: 'Level 1'
}
]
}
]
};
({ doc: menuDoc } = await client.post(options.slug.menu, postData, {
headers: {
Authorization: `JWT ${userToken}`
}
}));
menu = await client.get(`/${options.slug.menu}/${menuDoc.id}?market=${testConfig.documents.market.id}&locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${users.api.apiKey}`
}
});
menuItem = menu.items[0];
});
it('should be defined', async ()=>{
expect(menuItem).toBeDefined();
});
it('should have a valid type property', async ()=>{
expect(menuItem.type).toEqual(postData.items[0].type);
});
it('should have a valid title property', async ()=>{
expect(menuItem.title).toEqual(postData.items[0].title);
});
it('should have a valid items property', async ()=>{
expect(menuItem.items.length).toEqual(postData.items[0].items.length);
menuItem.items.forEach((item, index)=>expect(item.type).toEqual(postData.items[0].items[index].type));
});
});
describe('Menu category item', ()=>{
let menuDoc;
let postData;
let menu;
let menuItem;
beforeAll(async ()=>{
postData = {
id: uuid(),
items: [
{
id: uuid(),
type: 'category',
items: [],
target: '_blank',
category: testConfig.documents.category.id,
blockType: 'Level 0'
}
]
};
({ doc: menuDoc } = await client.post(options.slug.menu, postData, {
headers: {
Authorization: `JWT ${userToken}`
}
}));
menu = await client.get(`/${options.slug.menu}/${menuDoc.id}?market=${testConfig.documents.market.id}&locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${users.api.apiKey}`
}
});
menuItem = menu.items[0];
});
it('should be defined', async ()=>{
expect(menuItem).toBeDefined();
});
it('should have a valid type property', async ()=>{
expect(menuItem.type).toEqual(postData.items[0].type);
});
/*
it('should have a valid id property', async () => {
expect(menuItem.id).toEqual(postData.items[0].id);
});
*/ it('should have a valid category property', async ()=>{
expect(menuItem.category).toEqual(testConfig.documents.category.id);
});
it('should have a valid title property', async ()=>{
expect(menuItem.title).toEqual(testConfig.documents.category.title);
});
it('should have a valid items property', async ()=>{
expect(menuItem.items).toBeDefined();
});
describe('Route item', ()=>{
let routeItem;
beforeAll(async ()=>{
routeItem = menuItem.items.find((item)=>item.id === pageDoc.id);
});
it('should have a valid href property', async ()=>{
expect(routeItem.href).toEqual(getRouteId(pageDoc, options.defaultLocale));
});
it('should have a valid id property', async ()=>{
expect(routeItem.id).toEqual(pageDoc.id);
});
it('should have a valid items property', async ()=>{
expect(routeItem.items).toBeDefined();
});
it('should have a valid items property', async ()=>{
expect(routeItem.page).toEqual(pageDoc.id);
});
it('should have a valid schema property', async ()=>{
expect(routeItem.schema).toEqual(PageCollection.slug);
});
it('should have a valid title property', async ()=>{
expect(routeItem.title).toEqual(pageDoc.title);
});
it('should have a valid type property', async ()=>{
expect(routeItem.type).toEqual('route');
});
});
});
describe('Menu page item', ()=>{
let menuDoc;
let postData;
let menu;
let menuItem;
beforeAll(async ()=>{
postData = {
id: uuid(),
items: [
{
id: uuid(),
type: 'page',
page: {
relationTo: PageCollection.slug,
value: pageDoc.id
},
items: [],
target: '_blank',
blockType: 'Level 0'
}
]
};
({ doc: menuDoc } = await client.post(options.slug.menu, postData, {
headers: {
Authorization: `JWT ${userToken}`
}
}));
menu = await client.get(`/${options.slug.menu}/${menuDoc.id}?market=${testConfig.documents.market.id}&locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${users.api.apiKey}`
}
});
menuItem = menu.items[0];
});
it('should be defined', async ()=>{
expect(menuItem).toBeDefined();
});
it('should have a valid type property', async ()=>{
expect(menuItem.type).toEqual(postData.items[0].type);
});
/*
it('should have a valid id property', async () => {
expect(menuItem.id).toEqual(postData.items[0].id);
});
*/ it('should have a valid items property', async ()=>{
expect(menuItem.items).toEqual(postData.items[0].items);
});
/*
it('should have a valid schema property', async () => {
expect(menuItem.schema).toEqual(PageCollection.slug);
});
*/ it('should have a valid title property', async ()=>{
expect(menuItem.title).toEqual(pageDoc.title);
});
it('should have a valid href property', async ()=>{
expect(menuItem.href).toEqual(getRouteId(pageDoc, options.defaultLocale));
});
});
});
});
//# sourceMappingURL=menu.service.test.js.map