chore(plugin-form-builder): scaffolds tests (#4500)
This commit is contained in:
119
test/plugin-form-builder/int.spec.ts
Normal file
119
test/plugin-form-builder/int.spec.ts
Normal 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')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user