import { sqliteAdapter } from '@payloadcms/db-sqlite'; import { BlocksFeature, EXPERIMENTAL_TableFeature, lexicalEditor, } from '@payloadcms/richtext-lexical'; import { buildConfig, getPayload, slugField } from 'payload'; import type { Block, CollectionConfig, Field, Payload, Plugin } from 'payload'; // A link as Payload's website template defines it: a URL and a label. A new // object per use, since Payload's config setup changes nested fields. const link = (): Field => ({ name: 'link', type: 'group', fields: [ { name: 'url', type: 'text' }, { name: 'label', type: 'text', localized: true }, ], }); // A block that can hold itself, and a collection with text only inside it. const NESTED_BLOCK: Block = { slug: 'nested', fields: [ { name: 'children', type: 'blocks', blocks: [], blockReferences: ['nested'], }, { name: 'title', type: 'text', localized: true }, ], }; const TREES: CollectionConfig = { slug: 'trees', fields: [ { name: 'root', type: 'blocks', blocks: [], blockReferences: ['nested'] }, ], }; // Emails of users who may read notices but not update them. export const LOCKED_OUT = new Set(); // Emails of users who may not read notices. export const CANNOT_READ = new Set(); // The SEO plugin's meta title and description fields render with these. const SEO_TITLE = '@payloadcms/plugin-seo/client#MetaTitleComponent'; const SEO_DESCRIPTION = '@payloadcms/plugin-seo/client#MetaDescriptionComponent'; // A content model shaped like Payload's website template, on an in-memory // SQLite database. Pages and posts have drafts; the footer and media do not. export function testConfig( plugins: Plugin[] = [], { defaultLocale = 'en', localization = true, recursiveBlocks = false, }: { defaultLocale?: string; localization?: boolean; // Adds a block that can hold itself, which Payload's SQL adapters cannot // store, so only for configs that never open a database. recursiveBlocks?: boolean; } = {} ) { return buildConfig({ secret: 'test-secret', // Payload otherwise starts a type generation process per instance that // outlives the test run. typescript: { autoGenerate: false }, db: sqliteAdapter({ client: { url: ':memory:' } }), editor: lexicalEditor(), localization: localization ? { locales: ['en', 'es', 'fr', 'de'], defaultLocale, fallback: true } : false, collections: [ { slug: 'pages', admin: { useAsTitle: 'title' }, versions: { drafts: true }, fields: [ { name: 'title', type: 'text', localized: true, required: true }, { name: 'hero', type: 'group', fields: [ { name: 'richText', type: 'richText', localized: true }, { name: 'links', type: 'array', fields: [link()] }, ], }, { name: 'layout', type: 'blocks', blocks: [ { slug: 'cta', fields: [ { name: 'richText', type: 'richText', localized: true }, { name: 'links', type: 'array', fields: [link()] }, ], }, { slug: 'content', fields: [ { name: 'columns', type: 'array', fields: [ { name: 'size', type: 'select', options: ['full', 'half'], }, { name: 'richText', type: 'richText', localized: true }, ], }, ], }, ], }, { type: 'tabs', tabs: [ { name: 'meta', label: 'SEO', fields: [ { name: 'title', type: 'text', localized: true }, { name: 'description', type: 'textarea', localized: true }, ], }, ], }, { name: 'body', type: 'richText', localized: true, editor: lexicalEditor({ features: ({ defaultFeatures }) => [ ...defaultFeatures, BlocksFeature({ blocks: [ { slug: 'banner', fields: [{ name: 'content', type: 'richText' }], }, { slug: 'callout', fields: [{ name: 'content', type: 'richText' }], }, ], }), ], }), }, { name: 'keywords', type: 'text', hasMany: true, localized: true }, { name: 'code', type: 'code', localized: true, }, { name: 'internalNote', type: 'text', localized: true, custom: { gt: { translate: false } }, }, slugField({ localized: true }), ], }, { // Field settings that change what gets translated, without drafts. slug: 'articles', admin: { useAsTitle: 'name' }, fields: [ { name: 'name', type: 'text', localized: true, required: true, maxLength: 24, }, { name: 'summary', type: 'textarea', localized: true }, { name: 'metaTitle', type: 'text', localized: true, admin: { components: { Field: { path: SEO_TITLE } } }, }, { name: 'metaDescription', type: 'textarea', localized: true, admin: { components: { Field: { path: SEO_DESCRIPTION } } }, }, { name: 'generated', type: 'text', localized: true, admin: { readOnly: true }, }, { name: 'hiddenText', type: 'text', localized: true, admin: { hidden: true }, }, { name: 'legalNote', type: 'text', localized: true, required: true, custom: { gt: { translate: false } }, }, { name: 'tags', type: 'array', localized: true, fields: [ { name: 'label', type: 'text' }, { name: 'code', type: 'text', custom: { gt: { translate: false } }, }, ], }, { name: 'promo', type: 'group', localized: true, fields: [ { name: 'headline', type: 'text' }, { name: 'terms', type: 'text', custom: { gt: { translate: false } }, }, ], }, { name: 'keywords', type: 'text', hasMany: true, localized: true }, { name: 'body', type: 'richText', localized: true, editor: lexicalEditor({ features: ({ defaultFeatures }) => [ ...defaultFeatures, EXPERIMENTAL_TableFeature(), ], }), }, ], }, { // Localized text only inside a named localized tab. slug: 'faqs', fields: [ { type: 'tabs', tabs: [ { name: 'content', localized: true, fields: [{ name: 'question', type: 'text' }], }, ], }, ], }, { // Localized text only inside a block defined once and referenced. slug: 'stories', fields: [ { name: 'layout', type: 'blocks', blocks: [], blockReferences: ['quote'], }, ], }, ...(recursiveBlocks ? [TREES] : []), { // Editors may save drafts but not change a published document. slug: 'gated', versions: { drafts: true }, access: { read: () => true, update: ({ data }) => data?._status !== 'published', }, fields: [{ name: 'note', type: 'text', localized: true }], }, { // Readable and editable while the latest draft is marked editable. slug: 'memos', versions: { drafts: true }, access: { read: () => ({ editable: { equals: true } }), update: () => ({ editable: { equals: true } }), }, fields: [ { name: 'editable', type: 'checkbox' }, { name: 'note', type: 'text', localized: true }, ], }, { // Editors in LOCKED_OUT can read these but not change them. slug: 'notices', access: { read: ({ req }) => !CANNOT_READ.has(String(req.user?.email)), update: ({ req }) => !LOCKED_OUT.has(String(req.user?.email)), }, fields: [{ name: 'message', type: 'text', localized: true }], }, { // Like the search plugin's collection: copies kept by a plugin. slug: 'search', fields: [ { name: 'title', type: 'text', localized: true, admin: { readOnly: true }, }, ], }, { slug: 'media', fields: [{ name: 'alt', type: 'text', localized: true }], }, ], blocks: [ { slug: 'quote', fields: [{ name: 'text', type: 'text', localized: true }], }, ...(recursiveBlocks ? [NESTED_BLOCK] : []), ], globals: [ { slug: 'announcement', access: { read: () => true, update: ({ req }) => !LOCKED_OUT.has(String(req.user?.email)), }, fields: [{ name: 'message', type: 'text', localized: true }], }, { slug: 'header', versions: { drafts: true }, fields: [ { name: 'navItems', type: 'array', localized: true, fields: [link()], }, ], }, { slug: 'footer', fields: [{ name: 'navItems', type: 'array', fields: [link()] }], }, ], plugins, }); } // One instance per test file: Payload pushes the schema to a new database // only once per process, so a second in-memory database stays empty. export async function createTestPayload( plugins: Plugin[] = [], options: { defaultLocale?: string; localization?: boolean } = {} ): Promise { return getPayload({ config: await testConfig(plugins, options) }); } let shared: Promise | undefined; // The test file's instance without plugins, created on first use, for suites // that share a file. The file destroys it when its suites are done. export function sharedTestPayload(): Promise { return (shared ??= createTestPayload()); }