chore: renames nested-docs test suite
This commit is contained in:
45
test/plugin-nested-docs/collections/Pages.ts
Normal file
45
test/plugin-nested-docs/collections/Pages.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
// const payload = require('payload');
|
||||
import type { CollectionConfig } from '../../../packages/payload/src/collections/config/types'
|
||||
|
||||
import populateFullTitle from './populateFullTitle'
|
||||
|
||||
export const Pages: CollectionConfig = {
|
||||
slug: 'pages',
|
||||
labels: {
|
||||
singular: 'Page',
|
||||
plural: 'Pages',
|
||||
},
|
||||
admin: {
|
||||
useAsTitle: 'fullTitle',
|
||||
},
|
||||
access: {
|
||||
read: () => true,
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
label: 'Title',
|
||||
type: 'text',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'slug',
|
||||
label: 'Slug',
|
||||
type: 'text',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'fullTitle',
|
||||
type: 'text',
|
||||
localized: true,
|
||||
hooks: {
|
||||
beforeChange: [populateFullTitle],
|
||||
},
|
||||
admin: {
|
||||
components: {
|
||||
Field: () => null,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
16
test/plugin-nested-docs/collections/Users.ts
Normal file
16
test/plugin-nested-docs/collections/Users.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import type { CollectionConfig } from '../../../packages/payload/src/collections/config/types'
|
||||
|
||||
export const Users: CollectionConfig = {
|
||||
slug: 'users',
|
||||
auth: true,
|
||||
admin: {
|
||||
useAsTitle: 'email',
|
||||
},
|
||||
access: {
|
||||
read: () => true,
|
||||
},
|
||||
fields: [
|
||||
// Email added by default
|
||||
// Add more fields as needed
|
||||
],
|
||||
}
|
||||
17
test/plugin-nested-docs/collections/populateFullTitle.ts
Normal file
17
test/plugin-nested-docs/collections/populateFullTitle.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type { FieldHook } from '../../../packages/payload/src/fields/config/types'
|
||||
|
||||
export const generateFullTitle = (breadcrumbs: Array<{ label: string }>): string | undefined => {
|
||||
if (Array.isArray(breadcrumbs)) {
|
||||
return breadcrumbs.reduce((title, breadcrumb, i) => {
|
||||
if (i === 0) return `${breadcrumb.label}`
|
||||
return `${title} > ${breadcrumb.label}`
|
||||
}, '')
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
const populateFullTitle: FieldHook = async ({ data, originalDoc }) =>
|
||||
generateFullTitle(data?.breadcrumbs || originalDoc?.breadcrumbs)
|
||||
|
||||
export default populateFullTitle
|
||||
33
test/plugin-nested-docs/config.ts
Normal file
33
test/plugin-nested-docs/config.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import nestedDocs from '../../packages/plugin-nested-docs/src'
|
||||
import { buildConfigWithDefaults } from '../buildConfigWithDefaults'
|
||||
import { devUser } from '../credentials'
|
||||
import { Pages } from './collections/Pages'
|
||||
import { Users } from './collections/Users'
|
||||
import { seed } from './seed'
|
||||
|
||||
export default buildConfigWithDefaults({
|
||||
collections: [Pages, Users],
|
||||
localization: {
|
||||
locales: ['en', 'es', 'de'],
|
||||
defaultLocale: 'en',
|
||||
fallback: true,
|
||||
},
|
||||
plugins: [
|
||||
nestedDocs({
|
||||
collections: ['pages'],
|
||||
generateLabel: (_, doc) => doc.title as string,
|
||||
generateURL: (docs) => docs.reduce((url, doc) => `${url}/${doc.slug}`, ''),
|
||||
}),
|
||||
],
|
||||
onInit: async (payload) => {
|
||||
await payload.create({
|
||||
collection: 'users',
|
||||
data: {
|
||||
email: devUser.email,
|
||||
password: devUser.password,
|
||||
},
|
||||
})
|
||||
|
||||
await seed(payload)
|
||||
},
|
||||
})
|
||||
41
test/plugin-nested-docs/int.spec.ts
Normal file
41
test/plugin-nested-docs/int.spec.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import payload from '../../packages/payload/src'
|
||||
import { initPayloadTest } from '../helpers/configHelpers'
|
||||
|
||||
describe('Nested Docs', () => {
|
||||
beforeAll(async () => {
|
||||
await initPayloadTest({ __dirname, init: { local: true } })
|
||||
})
|
||||
|
||||
describe('seed', () => {
|
||||
it('should populate two levels of breadcrumbs', async () => {
|
||||
const query = await payload.find({
|
||||
collection: 'pages',
|
||||
where: {
|
||||
slug: {
|
||||
equals: 'child-page',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(query.docs[0].breadcrumbs).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('should populate three levels of breadcrumbs', async () => {
|
||||
const query = await payload.find({
|
||||
collection: 'pages',
|
||||
where: {
|
||||
slug: {
|
||||
equals: 'grandchild-page',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(query.docs[0].breadcrumbs).toHaveLength(3)
|
||||
expect(query.docs[0].breadcrumbs[0].url).toStrictEqual('/parent-page')
|
||||
expect(query.docs[0].breadcrumbs[1].url).toStrictEqual('/parent-page/child-page')
|
||||
expect(query.docs[0].breadcrumbs[2].url).toStrictEqual(
|
||||
'/parent-page/child-page/grandchild-page',
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
53
test/plugin-nested-docs/seed/index.ts
Normal file
53
test/plugin-nested-docs/seed/index.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { Payload } from '../../../packages/payload/src'
|
||||
|
||||
export const seed = async (payload: Payload): Promise<boolean> => {
|
||||
payload.logger.info('Seeding data...')
|
||||
|
||||
try {
|
||||
await payload.create({
|
||||
collection: 'users',
|
||||
data: {
|
||||
email: 'demo@payloadcms.com',
|
||||
password: 'demo',
|
||||
},
|
||||
})
|
||||
|
||||
const { id: parentID } = await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: 'Parent page',
|
||||
slug: 'parent-page',
|
||||
},
|
||||
})
|
||||
|
||||
const { id: childID } = await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: 'Child page',
|
||||
slug: 'child-page',
|
||||
parent: parentID,
|
||||
},
|
||||
})
|
||||
|
||||
await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: 'Grandchild page',
|
||||
slug: 'grandchild-page',
|
||||
parent: childID,
|
||||
},
|
||||
})
|
||||
|
||||
await payload.create({
|
||||
collection: 'pages',
|
||||
data: {
|
||||
title: 'Sister page',
|
||||
slug: 'sister-page',
|
||||
},
|
||||
})
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user