@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
93 lines (92 loc) • 3.71 kB
JavaScript
import { config, FieldCollection, 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('withTemplate field', ()=>{
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('Field config', ()=>{
let collection;
let field;
beforeAll(async ()=>{
collection = payload.config.collections.find((x)=>x.slug === FieldCollection.slug);
});
it('should have a field named "template" of type "relationship"', ()=>{
field = collection.fields.find((field)=>field.type === 'relationship' && field.name === 'template');
expect(field).toBeDefined();
});
it('should allow only one relation', ()=>{
expect(field.hasMany).toEqual(false);
});
it('should allow relationship with the "template" collection', ()=>{
expect(field.relationTo).toEqual(options.slug.template);
});
it('should filter the relationship options (filterOptions property)', ()=>{
expect(field.filterOptions).toBeDefined();
});
});
describe('Document creation', ()=>{
let fieldsDoc;
let postData;
beforeAll(async ()=>{
postData = {
_id: uuid(),
title: uuid(),
template: testConfig.documents.template.id
};
({ doc: fieldsDoc } = await client.post(`/${FieldCollection.slug}?locale=${testConfig.defaultLocale}`, postData, {
headers: {
Authorization: `JWT ${userToken}`
}
}));
});
it('should have a valid "template" field', async ()=>{
expect(fieldsDoc.template.id).toEqual(postData.template);
});
it('should not allow incompatible templates', async ()=>{
try {
const { doc: incompatibleTemplateDoc } = await client.post(`/${options.slug.template}?locale=${testConfig.defaultLocale}`, {
id: uuid(),
name: uuid(),
hasCompatibleCollections: true,
compatibleCollections: [
PageCollection.slug
]
}, {
headers: {
Authorization: `JWT ${userToken}`
}
});
postData._id = uuid();
postData.template = incompatibleTemplateDoc.id;
({ doc: fieldsDoc } = await client.post(`/${FieldCollection.slug}?locale=${testConfig.defaultLocale}`, postData, {
headers: {
Authorization: `JWT ${userToken}`
}
}));
expect(fieldsDoc).toBeUndefined();
} catch (error) {
expect(error).toBeDefined();
expect(error.status).toBe(400);
}
});
});
});
//# sourceMappingURL=withTemplate.test.js.map