@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
71 lines (70 loc) • 2.74 kB
JavaScript
import { config, FieldCollection, getTestConfig } from '@/test';
import { textToSlug } from '@websolutespa/payload-utils';
import { clearContext, endUsers, getContext, users } from '@websolutespa/test/payload';
import { v4 as uuid } from 'uuid';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
describe('withSlug 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 "slug" of type "text"', ()=>{
field = collection.fields.find((field)=>field.type === 'text' && field.name === 'slug');
expect(field).toBeDefined();
});
it('should be localized', ()=>{
expect(field.localized).toEqual(true);
});
it('should have a "beforeValidate" hook defined', ()=>{
expect(field.hooks.beforeValidate).toBeDefined();
});
});
describe('Document creation', ()=>{
it('should have a valid "slug" field', async ()=>{
const postData = {
_id: uuid(),
title: uuid(),
slug: uuid()
};
const { doc: fieldsDoc } = await client.post(`/${FieldCollection.slug}?locale=${testConfig.defaultLocale}`, postData, {
headers: {
Authorization: `JWT ${userToken}`
}
});
expect(fieldsDoc.slug).toEqual(textToSlug(postData.slug));
});
it('should have a valid "slug" field (title field used as value fallback)', async ()=>{
const postData = {
_id: uuid(),
title: uuid()
};
const { doc: fieldsDoc } = await client.post(`/${FieldCollection.slug}?locale=${testConfig.defaultLocale}`, postData, {
headers: {
Authorization: `JWT ${userToken}`
}
});
expect(fieldsDoc.slug).toEqual(textToSlug(postData.title));
});
});
});
//# sourceMappingURL=withSlug.test.js.map