chore(plugin-form-builder): scaffolds tests (#4500)

This commit is contained in:
Jacob Fletcher
2023-12-13 15:13:38 -05:00
committed by GitHub
parent eb6572e9e5
commit 9e7a8c7206
14 changed files with 484 additions and 371 deletions

View File

@@ -0,0 +1,36 @@
// const payload = require('payload');
import type { CollectionConfig } from '../../../packages/payload/src/collections/config/types'
export const Pages: CollectionConfig = {
slug: 'pages',
labels: {
singular: 'Page',
plural: 'Pages',
},
admin: {
useAsTitle: 'title',
},
access: {
read: () => true,
},
fields: [
{
name: 'title',
label: 'Title',
type: 'text',
required: true,
},
{
name: 'slug',
label: 'Slug',
type: 'text',
required: true,
},
{
name: 'form',
label: 'Form',
type: 'relationship',
relationTo: 'forms',
},
],
}

View 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
],
}

View File

@@ -0,0 +1,83 @@
import type { Block } from '../../packages/payload/src/fields/config/types'
import formBuilder, { fields as formFields } from '../../packages/plugin-form-builder/src'
import { buildConfigWithDefaults } from '../buildConfigWithDefaults'
import { devUser } from '../credentials'
import { Pages } from './collections/Pages'
import { Users } from './collections/Users'
import { seed } from './seed'
const colorField: Block = {
slug: 'color',
labels: {
singular: 'Color',
plural: 'Colors',
},
fields: [
{
name: 'value',
type: 'text',
},
],
}
export default buildConfigWithDefaults({
collections: [Pages, Users],
localization: {
defaultLocale: 'en',
fallback: true,
locales: ['en', 'es', 'de'],
},
onInit: async (payload) => {
await payload.create({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
},
})
await seed(payload)
},
plugins: [
formBuilder({
// handlePayment: handleFormPayments,
// beforeEmail: prepareFormEmails,
redirectRelationships: ['pages'],
formOverrides: {
// labels: {
// singular: 'Contact Form',
// plural: 'Contact Forms'
// },
fields: [
{
name: 'custom',
type: 'text',
},
],
},
fields: {
payment: true,
colorField,
text: {
...formFields.text,
labels: {
singular: 'Custom Text Field',
plural: 'Custom Text Fields',
},
},
// payment: {
// paymentProcessor: {
// options: [
// {
// label: 'Stripe',
// value: 'stripe'
// },
// ],
// defaultValue: 'stripe',
// },
// },
},
}),
],
})

View File

@@ -0,0 +1,119 @@
import type { Form } from './payload-types'
import payload from '../../packages/payload/src'
import { initPayloadTest } from '../helpers/configHelpers'
import { formSubmissionsSlug, formsSlug } from './shared'
describe('Form Builder Plugin', () => {
let form: Form
beforeAll(async () => {
await initPayloadTest({ __dirname, init: { local: true } })
const formConfig: Omit<Form, 'createdAt' | 'id' | 'updatedAt'> = {
title: 'Test Form',
fields: [
{
name: 'name',
blockType: 'text',
},
],
confirmationMessage: [
{
type: 'text',
text: 'Confirmed.',
},
],
}
form = (await payload.create({
collection: formsSlug,
data: formConfig,
})) as unknown as Form
})
describe('plugin collections', () => {
it('adds forms collection', async () => {
const { docs: forms } = await payload.find({ collection: formsSlug })
expect(forms.length).toBeGreaterThan(0)
})
it('adds form submissions collection', async () => {
const { docs: formSubmissions } = await payload.find({ collection: formSubmissionsSlug })
expect(formSubmissions).toHaveLength(0)
})
})
describe('form building', () => {
it('can create a simple form', async () => {
const formConfig: Omit<Form, 'createdAt' | 'id' | 'updatedAt'> = {
title: 'Test Form',
fields: [
{
name: 'name',
blockType: 'text',
},
],
confirmationMessage: [
{
type: 'text',
text: 'Confirmed.',
},
],
}
const testForm = await payload.create({
collection: formsSlug,
data: formConfig,
})
expect(testForm).toHaveProperty('fields')
expect(testForm.fields).toHaveLength(1)
expect(testForm.fields[0]).toHaveProperty('name', 'name')
})
it('can use form overrides', async () => {
const formConfig: Omit<Form, 'createdAt' | 'id' | 'updatedAt'> = {
custom: 'custom',
title: 'Test Form',
confirmationMessage: [
{
type: 'text',
text: 'Confirmed.',
},
],
}
const testForm = await payload.create({
collection: formsSlug,
data: formConfig,
})
expect(testForm).toHaveProperty('custom', 'custom')
})
})
describe('form submissions and validations', () => {
it('can create a form submission', async () => {
const formSubmission = await payload.create({
collection: formSubmissionsSlug,
data: {
form: form.id,
submissionData: [
{
field: 'name',
value: 'Test Submission',
},
],
},
depth: 0,
})
expect(formSubmission).toHaveProperty('form', form.id)
expect(formSubmission).toHaveProperty('submissionData')
expect(formSubmission.submissionData).toHaveLength(1)
expect(formSubmission.submissionData[0]).toHaveProperty('field', 'name')
expect(formSubmission.submissionData[0]).toHaveProperty('value', 'Test Submission')
})
})
})

View File

@@ -0,0 +1,257 @@
/* tslint:disable */
/* eslint-disable */
/**
* This file was automatically generated by Payload.
* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,
* and re-run `payload generate:types` to regenerate this file.
*/
export interface Config {
collections: {
pages: Page
users: User
forms: Form
'form-submissions': FormSubmission
'payload-preferences': PayloadPreference
'payload-migrations': PayloadMigration
}
globals: {}
}
export interface Page {
id: string
title: string
slug: string
form?: (string | null) | Form
updatedAt: string
createdAt: string
}
export interface Form {
id: string
title: string
fields?:
| (
| {
name: string
label?: string | null
width?: number | null
required?: boolean | null
defaultValue?: boolean | null
id?: string | null
blockName?: string | null
blockType: 'checkbox'
}
| {
name: string
label?: string | null
width?: number | null
required?: boolean | null
id?: string | null
blockName?: string | null
blockType: 'country'
}
| {
name: string
label?: string | null
width?: number | null
required?: boolean | null
id?: string | null
blockName?: string | null
blockType: 'email'
}
| {
message?:
| {
[k: string]: unknown
}[]
| null
id?: string | null
blockName?: string | null
blockType: 'message'
}
| {
name: string
label?: string | null
width?: number | null
defaultValue?: number | null
required?: boolean | null
id?: string | null
blockName?: string | null
blockType: 'number'
}
| {
name: string
label?: string | null
width?: number | null
basePrice?: number | null
priceConditions?:
| {
fieldToUse?: string | null
condition?: ('hasValue' | 'equals' | 'notEquals') | null
valueForCondition?: string | null
operator?: ('add' | 'subtract' | 'multiply' | 'divide') | null
valueType?: ('static' | 'valueOfField') | null
valueForOperator?: string | null
id?: string | null
}[]
| null
required?: boolean | null
id?: string | null
blockName?: string | null
blockType: 'payment'
}
| {
name: string
label?: string | null
width?: number | null
defaultValue?: string | null
options?:
| {
label: string
value: string
id?: string | null
}[]
| null
required?: boolean | null
id?: string | null
blockName?: string | null
blockType: 'select'
}
| {
name: string
label?: string | null
width?: number | null
required?: boolean | null
id?: string | null
blockName?: string | null
blockType: 'state'
}
| {
name: string
label?: string | null
width?: number | null
defaultValue?: string | null
required?: boolean | null
id?: string | null
blockName?: string | null
blockType: 'text'
}
| {
name: string
label?: string | null
width?: number | null
defaultValue?: string | null
required?: boolean | null
id?: string | null
blockName?: string | null
blockType: 'textarea'
}
| {
value?: string | null
id?: string | null
blockName?: string | null
blockType: 'color'
}
)[]
| null
submitButtonLabel?: string | null
confirmationType?: ('message' | 'redirect') | null
confirmationMessage?:
| {
[k: string]: unknown
}[]
| null
redirect?: {
type?: ('reference' | 'custom') | null
reference?: {
relationTo: 'pages'
value: string | Page
} | null
url?: string | null
}
emails?:
| {
emailTo?: string | null
cc?: string | null
bcc?: string | null
replyTo?: string | null
emailFrom?: string | null
subject: string
message?:
| {
[k: string]: unknown
}[]
| null
id?: string | null
}[]
| null
custom?: string | null
updatedAt: string
createdAt: string
}
export interface User {
id: string
updatedAt: string
createdAt: string
email: string
resetPasswordToken?: string | null
resetPasswordExpiration?: string | null
salt?: string | null
hash?: string | null
loginAttempts?: number | null
lockUntil?: string | null
password: string | null
}
export interface FormSubmission {
id: string
form: string | Form
submissionData?:
| {
field: string
value: string
id?: string | null
}[]
| null
payment?: {
field?: string | null
status?: string | null
amount?: number | null
paymentProcessor?: string | null
creditCard?: {
token?: string | null
brand?: string | null
number?: string | null
}
}
updatedAt: string
createdAt: string
}
export interface PayloadPreference {
id: string
user: {
relationTo: 'users'
value: string | User
}
key?: string | null
value?:
| {
[k: string]: unknown
}
| unknown[]
| string
| number
| boolean
| null
updatedAt: string
createdAt: string
}
export interface PayloadMigration {
id: string
name?: string | null
batch?: number | null
updatedAt: string
createdAt: string
}
declare module 'payload' {
export interface GeneratedTypes extends Config {}
}

View File

@@ -0,0 +1,70 @@
import type { Payload } from '../../../packages/payload/src'
import type { PayloadRequest } from '../../../packages/payload/src/express/types'
import { formsSlug, pagesSlug } from '../shared'
export const seed = async (payload: Payload): Promise<boolean> => {
payload.logger.info('Seeding data...')
const req = {} as PayloadRequest
try {
await payload.create({
collection: 'users',
data: {
email: 'demo@payloadcms.com',
password: 'demo',
},
req,
})
await payload.create({
collection: pagesSlug,
data: {
slug: 'home',
title: 'Home page',
},
req,
})
const { id: formID } = await payload.create({
collection: formsSlug,
data: {
title: 'Contact Form',
confirmationMessage: [
{
type: 'paragraph',
text: 'Confirmed',
},
],
fields: [
{
blockType: 'text',
label: 'Name',
name: 'name',
required: true,
},
{
blockType: 'email',
label: 'Email',
name: 'email',
required: true,
},
],
},
})
await payload.create({
collection: pagesSlug,
data: {
title: 'Contact',
slug: 'contact',
form: formID,
},
})
return true
} catch (err) {
console.error(err)
return false
}
}

View File

@@ -0,0 +1,5 @@
export const pagesSlug = 'pages'
export const formsSlug = 'forms'
export const formSubmissionsSlug = 'form-submissions'