UNPKG

@websolutespa/payload-plugin-bowl

Version:

Bowl PayloadCms plugin of the BOM Repository

144 lines (143 loc) 7.01 kB
import { config, LinkCollection } 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'; import { appearanceOptions } from './withLink'; describe('withLink field', ()=>{ 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('Field config', ()=>{ let collection; let field; beforeAll(async ()=>{ collection = payload.config.collections.find((collection)=>collection.slug === LinkCollection.slug); }); it('should have a field named "link"', ()=>{ field = collection.fields.find((field)=>field.type === 'group' && 'name' in field && field.name === 'link'); expect(field).toBeDefined(); }); describe('Subfields', ()=>{ describe('"type" subfield', ()=>{ let typeField; it('should have a subfield "type" of type "radio"', ()=>{ typeField = field.fields.find((field)=>field.type === 'radio' && field.name === 'type'); expect(typeField).toBeDefined(); }); it('should have options "reference", "custom"', ()=>{ expect(typeField.options.find((option)=>typeof option === 'object' && option.value === 'reference')).toBeDefined(); expect(typeField.options.find((option)=>typeof option === 'object' && option.value === 'custom')).toBeDefined(); }); }); describe('"newTab" subfield', ()=>{ let newTabField; it('should have a subfield "newTab" of type "checkbox"', ()=>{ newTabField = field.fields.find((field)=>field.type === 'checkbox' && field.name === 'newTab'); expect(newTabField).toBeDefined(); }); }); describe('"appearance" subfield', ()=>{ let appearanceField; it('should have a subfield "appearance" of type "select"', ()=>{ appearanceField = field.fields.find((field)=>field.type === 'select' && field.name === 'appearance'); expect(appearanceField).toBeDefined(); }); it('should have default options "text", "primaryButton", "secondaryButton"', ()=>{ expect(appearanceField.options.find((option)=>typeof option === 'object' && option.value === appearanceOptions.text.value)).toBeDefined(); expect(appearanceField.options.find((option)=>typeof option === 'object' && option.value === appearanceOptions.primaryButton.value)).toBeDefined(); expect(appearanceField.options.find((option)=>typeof option === 'object' && option.value === appearanceOptions.secondaryButton.value)).toBeDefined(); }); }); describe('"row" subfield', ()=>{ let rowField; it('should have a subfield of type "row"', ()=>{ rowField = field.fields.find((field)=>field.type === 'row'); expect(rowField).toBeDefined(); }); describe('"reference" field', ()=>{ let referenceField; it('should have a subfield "reference" of type "relationship"', ()=>{ referenceField = rowField.fields.find((field)=>field.type === 'relationship' && field.name === 'reference'); expect(referenceField).toBeDefined(); }); it('should allow relationship with pages collections', ()=>{ expect(JSON.stringify(referenceField.relationTo)).toEqual(JSON.stringify(options.pages)); }); it('should be required', ()=>{ expect(referenceField.required).toEqual(true); }); it('should have a maxDepth of 2', ()=>{ expect(referenceField.maxDepth).toEqual(2); }); it('should have a admin visibility condition ', ()=>{ expect(referenceField.admin.condition).toBeDefined(); }); }); describe('"url" field', ()=>{ let urlField; it('should have a subfield "url" of type "text"', ()=>{ urlField = rowField.fields.find((field)=>field.type === 'text' && field.name === 'url'); expect(urlField).toBeDefined(); }); it('should be required', ()=>{ expect(urlField.required).toEqual(true); }); it('should have a admin visibility condition ', ()=>{ expect(urlField.admin.condition).toBeDefined(); }); }); describe('"label" field', ()=>{ let labelField; it('should have a subfield "label" of type "text"', ()=>{ labelField = rowField.fields.find((field)=>field.type === 'text' && field.name === 'label'); expect(labelField).toBeDefined(); }); it('should be required', ()=>{ expect(labelField.required).toEqual(true); }); }); }); }); }); describe('Document creation', ()=>{ let fieldsDoc; let postData; beforeAll(async ()=>{ postData = { title: uuid(), id: uuid(), link: { type: 'custom', url: uuid(), label: uuid() } }; ({ doc: fieldsDoc } = await client.post(LinkCollection.slug, postData, { headers: { Authorization: `JWT ${userToken}` } })); }); it('should have a valid "link" field', async ()=>{ expect(fieldsDoc.link).toBeDefined(); expect(fieldsDoc.link.type).toEqual(postData.link.type); expect(fieldsDoc.link.url).toEqual(postData.link.url); expect(fieldsDoc.link.label).toEqual(postData.link.label); }); }); }); //# sourceMappingURL=withLink.test.js.map