{{*:table}} renders a readable submission summary (questions, option labels,
grouped by step, empty fields skipped) inside an email template with logo,
accent colour and footer from Site Settings → Emails. submitForm stores the
submission in the page language, so emails follow the visitor's locale.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
90 lines
4.6 KiB
TypeScript
90 lines
4.6 KiB
TypeScript
import type { PayloadRequest } from 'payload'
|
|
|
|
import { describe, expect, test, vi } from 'vitest'
|
|
|
|
import type { BuilderField } from '../src/modules/forms/builderTypes.js'
|
|
|
|
import { buildEmailLayoutHook, buildSubmissionSummary, renderSummaryHtml, replaceSubmissionTable, wrapEmailHtml } from '../src/modules/forms/emailLayout.js'
|
|
import { submitForm } from '../src/modules/forms/submitForm.js'
|
|
import { fakePayload } from './helpers.js'
|
|
|
|
vi.mock('server-only', () => ({}))
|
|
|
|
const fields: BuilderField[] = [
|
|
{ blockType: 'step', title: 'O firmie' },
|
|
{ blockType: 'message' },
|
|
{ name: 'company', blockType: 'text', label: 'Nazwa firmy' },
|
|
{ name: 'website', blockType: 'text', label: 'Strona' },
|
|
{ name: 'areas', blockType: 'checkboxGroup', label: 'Obszary', options: [{ label: 'Strony internetowe', value: 'web' }, { label: 'IT', value: 'it' }] },
|
|
{ name: 'size', blockType: 'radio', label: 'Wielkość', options: [{ label: 'Do 10', value: '1-9' }] },
|
|
{ name: 'consent', blockType: 'checkbox', label: 'Zgoda RODO' },
|
|
{ blockType: 'step', title: 'IT' },
|
|
{ name: 'it_scope', blockType: 'textarea', label: 'Zakres' },
|
|
]
|
|
const stored = [
|
|
{ field: 'company', value: 'Acme <script>' },
|
|
{ field: 'website', value: '' },
|
|
{ field: 'areas', value: 'web, it' },
|
|
{ field: 'size', value: '1-9' },
|
|
{ field: 'consent', value: 'true' },
|
|
{ field: 'formSubmissionID', value: '42' },
|
|
]
|
|
|
|
describe('e-mail layout', () => {
|
|
test('summary: questions + option labels, grouped by step, blanks and empty steps left out', () => {
|
|
expect(buildSubmissionSummary(fields, stored)).toEqual([
|
|
{
|
|
rows: [
|
|
{ label: 'Nazwa firmy', value: 'Acme <script>' },
|
|
{ label: 'Obszary', value: 'Strony internetowe, IT' },
|
|
{ label: 'Wielkość', value: 'Do 10' },
|
|
{ label: 'Zgoda RODO', value: '✓' },
|
|
],
|
|
title: 'O firmie',
|
|
},
|
|
])
|
|
})
|
|
|
|
test('summary HTML is escaped; the form-builder table (also inside <p>) is replaced', () => {
|
|
const html = renderSummaryHtml(buildSubmissionSummary(fields, stored), { accent: '#017BD0' })
|
|
expect(html).toContain('Acme <script>')
|
|
expect(html).not.toContain('<script>')
|
|
expect(replaceSubmissionTable('<p>Nowe:</p><p><table><tr><td>company</td><td>x</td></tr></table></p>', '[S]')).toBe('<p>Nowe:</p>[S]')
|
|
})
|
|
|
|
test('layout: logo or site name, accent colour, footer', () => {
|
|
const withLogo = wrapEmailHtml({ accent: '#017BD0', content: '<p>Hi</p>', footer: 'Acme sp. z o.o.\nWrocław', logoUrl: 'https://cdn.acme.pl/logo.png', siteName: 'Acme', siteUrl: 'https://acme.pl' })
|
|
expect(withLogo).toContain('src="https://cdn.acme.pl/logo.png"')
|
|
expect(withLogo).toContain('border-top:4px solid #017BD0')
|
|
expect(withLogo).toContain('Acme sp. z o.o.<br>Wrocław')
|
|
expect(wrapEmailHtml({ accent: '#111827', content: '', siteName: 'Acme' })).toContain('>Acme</span>')
|
|
})
|
|
|
|
test('hook: summary + layout from Site Settings → Emails; on an error the original e-mails go out', async () => {
|
|
const payload = {
|
|
config: { serverURL: 'https://acme.pl/' },
|
|
findByID: vi.fn(async () => ({ fields })),
|
|
findGlobal: vi.fn(async () => ({ email: { accentColor: '#017BD0', footer: 'Acme', logo: { url: '/api/media/file/logo.png' } }, siteName: 'Acme' })),
|
|
logger: { error: vi.fn() },
|
|
}
|
|
const hook = buildEmailLayoutHook()
|
|
const email = { from: '[email protected]', html: '<div><p><table><tr><td>company</td><td>Acme</td></tr></table></p></div>', replyTo: '[email protected]', subject: 'Nowy brief', to: '[email protected]' }
|
|
const req = { locale: 'en', payload } as unknown as PayloadRequest
|
|
const [out] = await hook([email], { data: { form: 5, submissionData: stored }, req })
|
|
expect(payload.findByID).toHaveBeenCalledWith(expect.objectContaining({ id: 5, locale: 'en' }))
|
|
expect(out.html).toContain('Strony internetowe, IT')
|
|
expect(out.html).toContain('src="https://acme.pl/api/media/file/logo.png"')
|
|
expect(out.html).not.toContain('<td>company</td>')
|
|
|
|
payload.findByID.mockRejectedValueOnce(new Error('db down'))
|
|
expect(await hook([email], { data: { form: 5, submissionData: stored }, req })).toEqual([email])
|
|
expect(payload.logger.error).toHaveBeenCalled()
|
|
})
|
|
|
|
test('submitForm stores the submission in the page language (e-mail texts per language)', async () => {
|
|
const { create, payload } = fakePayload({ forms: { f: { fields: [{ name: 'company', blockType: 'text' }] } } })
|
|
await submitForm({ data: { company: 'Acme' }, formId: 'f', locale: 'en', maxPerMinute: 0, payload })
|
|
expect(create).toHaveBeenCalledWith(expect.objectContaining({ locale: 'en' }))
|
|
})
|
|
})
|