test: add test email adapter, use for all tests by default (#6120)

This commit is contained in:
Elliot DeNolf
2024-04-29 14:38:35 -04:00
committed by GitHub
parent cce75f11ca
commit 2e77bdf11e
3 changed files with 41 additions and 18 deletions

View File

@@ -101,7 +101,7 @@ export default joi.object({
validate: joi.func().required(),
})
.unknown(),
email: joi.object(),
email: joi.alternatives().try(joi.object(), joi.func()),
endpoints: endpointsSchema,
globals: joi.array(),
graphQL: joi.object().keys({

View File

@@ -34,6 +34,7 @@ import sharp from 'sharp'
import { reInitEndpoint } from './helpers/reInit.js'
import { localAPIEndpoint } from './helpers/sdk/endpoint.js'
import { testEmailAdapter } from './testEmailAdapter.js'
// process.env.PAYLOAD_DATABASE = 'postgres'
export async function buildConfigWithDefaults(
@@ -74,23 +75,7 @@ export async function buildConfigWithDefaults(
const config: Config = {
db: databaseAdapters[process.env.PAYLOAD_DATABASE || 'mongodb'],
secret: 'TEST_SECRET',
//editor: slateEditor({}),
// editor: slateEditor({
// admin: {
// upload: {
// collections: {
// media: {
// fields: [
// {
// name: 'alt',
// type: 'text',
// },
// ],
// },
// },
// },
// },
// }),
email: testEmailAdapter,
endpoints: [localAPIEndpoint, reInitEndpoint],
editor: lexicalEditor({
features: [

38
test/testEmailAdapter.ts Normal file
View File

@@ -0,0 +1,38 @@
import type { EmailAdapter, SendEmailOptions } from 'payload/config'
/**
* Logs all emails to stdout
*/
export const testEmailAdapter: EmailAdapter<void> = ({ payload }) => ({
name: 'test-email-adapter',
defaultFromAddress: 'dev@payloadcms.com',
defaultFromName: 'Payload Test',
sendEmail: async (message) => {
const stringifiedTo = getStringifiedToAddress(message)
const res = `Test email to: '${stringifiedTo}', Subject: '${message.subject}'`
payload.logger.info({ msg: res, content: message })
return Promise.resolve()
},
})
export function getStringifiedToAddress(message: SendEmailOptions): string | undefined {
let stringifiedTo: string | undefined
if (typeof message.to === 'string') {
stringifiedTo = message.to
} else if (Array.isArray(message.to)) {
stringifiedTo = message.to
.map((to) => {
if (typeof to === 'string') {
return to
} else if (to.address) {
return to.address
}
return ''
})
.join(', ')
} else if (message.to.address) {
stringifiedTo = message.to.address
}
return stringifiedTo
}