Refactor function testEmailData using an extendable array of tested strings

This commit is contained in:
Wolfgang Huß 2021-11-18 14:29:53 +01:00
parent 7b0209b484
commit 8e1fda5e90

View File

@ -9,7 +9,7 @@ import {
} from './templateBuilder' } from './templateBuilder'
const welcomeImageUrl = new URL(logosWebapp.LOGO_WELCOME_PATH, CONFIG.CLIENT_URI) const welcomeImageUrl = new URL(logosWebapp.LOGO_WELCOME_PATH, CONFIG.CLIENT_URI)
const supportUrl = CONFIG.SUPPORT_URL const supportUrl = CONFIG.SUPPORT_URL.toString()
let actionUrl, name, settingsUrl let actionUrl, name, settingsUrl
const signupTemplateData = () => ({ const signupTemplateData = () => ({
@ -27,31 +27,37 @@ const notificationTemplateData = (locale) => ({
}, },
}, },
}) })
const testEmailData = (emailTemplate, templateBuilder, templateData, individuals) => { const textsStandard = [
{
templPropName: 'from',
isContaining: false,
text: CONFIG.EMAIL_DEFAULT_SENDER,
},
{
templPropName: 'to',
isContaining: false,
text: 'test@example.org',
},
// is containing in html
welcomeImageUrl.toString(),
CONFIG.ORGANIZATION_URL,
CONFIG.APPLICATION_NAME,
]
const testEmailData = (emailTemplate, templateBuilder, templateData, texts) => {
if (!emailTemplate) { if (!emailTemplate) {
emailTemplate = templateBuilder(templateData) emailTemplate = templateBuilder(templateData)
} }
expect(emailTemplate.from).toEqual(CONFIG.EMAIL_DEFAULT_SENDER) texts.forEach((element) => {
expect(emailTemplate.to).toEqual('test@example.org') if (typeof element === 'object') {
if (individuals.subject) { if (element.isContaining) {
expect(emailTemplate.subject).toEqual(individuals.subject) expect(emailTemplate[element.templPropName]).toEqual(expect.stringContaining(element.text))
} } else {
expect(emailTemplate.html).toEqual(expect.stringContaining(welcomeImageUrl.toString())) expect(emailTemplate[element.templPropName]).toEqual(element.text)
if (individuals.name) { }
expect(emailTemplate.html).toEqual(expect.stringContaining(individuals.name)) } else {
} expect(emailTemplate.html).toEqual(expect.stringContaining(element))
if (individuals.actionUrl) { }
expect(emailTemplate.html).toEqual(expect.stringContaining(individuals.actionUrl.toString())) })
}
expect(emailTemplate.html).toEqual(expect.stringContaining(CONFIG.ORGANIZATION_URL))
expect(emailTemplate.html).toEqual(expect.stringContaining(CONFIG.APPLICATION_NAME))
if (individuals.settingsUrl) {
expect(emailTemplate.html).toEqual(expect.stringContaining(individuals.settingsUrl.toString()))
}
expect(emailTemplate.html).toEqual(expect.stringContaining(individuals.content))
if (individuals.supportUrl) {
expect(emailTemplate.html).toEqual(expect.stringContaining(individuals.supportUrl.toString()))
}
return emailTemplate return emailTemplate
} }
@ -67,27 +73,24 @@ describe('templateBuilder', () => {
describe('signupTemplate', () => { describe('signupTemplate', () => {
describe('multi language', () => { describe('multi language', () => {
it('e-mail is build with all data', () => { it('e-mail is build with all data', () => {
const actionUrl = new URL('/registration', CONFIG.CLIENT_URI) const actionUrl = new URL('/registration', CONFIG.CLIENT_URI).toString()
const theSignupTemplateData = signupTemplateData() const theSignupTemplateData = signupTemplateData()
// locale: en // locale: en
let content = "Thank you for joining our cause it's awesome to have you on board." let content = "Thank you for joining our cause it's awesome to have you on board."
const emailTemplate = testEmailData(null, signupTemplate, theSignupTemplateData, { const emailTemplate = testEmailData(null, signupTemplate, theSignupTemplateData, [
...textsStandard,
actionUrl, actionUrl,
content, content,
supportUrl, supportUrl,
}) theSignupTemplateData.variables.nonce,
theSignupTemplateData.variables.inviteCode,
])
// locale: de // locale: de
content = 'Danke, dass Du dich angemeldet hast wir freuen uns, Dich dabei zu haben.' content = 'Danke, dass Du dich angemeldet hast wir freuen uns, Dich dabei zu haben.'
testEmailData(emailTemplate, signupTemplate, theSignupTemplateData, { testEmailData(emailTemplate, signupTemplate, theSignupTemplateData, [
// ...textsStandard, // tested at locale: en
content, content,
}) ])
// test additional
expect(emailTemplate.html).toEqual(
expect.stringContaining(theSignupTemplateData.variables.nonce),
)
expect(emailTemplate.html).toEqual(
expect.stringContaining(theSignupTemplateData.variables.inviteCode),
)
}) })
}) })
}) })
@ -103,7 +106,7 @@ describe('templateBuilder', () => {
describe('notificationTemplate', () => { describe('notificationTemplate', () => {
beforeEach(() => { beforeEach(() => {
actionUrl = new URL('/notifications', CONFIG.CLIENT_URI) actionUrl = new URL('/notifications', CONFIG.CLIENT_URI).toString()
name = 'Mr Example' name = 'Mr Example'
settingsUrl = new URL('/settings/notifications', CONFIG.CLIENT_URI) settingsUrl = new URL('/settings/notifications', CONFIG.CLIENT_URI)
}) })
@ -112,13 +115,18 @@ describe('templateBuilder', () => {
it('e-mail is build with all data', () => { it('e-mail is build with all data', () => {
const subject = `${CONFIG.APPLICATION_NAME} Notification` const subject = `${CONFIG.APPLICATION_NAME} Notification`
const content = 'You received at least one notification. Click on this button to view them:' const content = 'You received at least one notification. Click on this button to view them:'
testEmailData(null, notificationTemplate, notificationTemplateData('en'), { testEmailData(null, notificationTemplate, notificationTemplateData('en'), [
subject, ...textsStandard,
{
templPropName: 'subject',
isContaining: false,
text: subject,
},
actionUrl, actionUrl,
name, name,
settingsUrl,
content, content,
}) settingsUrl,
])
}) })
}) })
@ -126,13 +134,18 @@ describe('templateBuilder', () => {
it('e-mail is build with all data', async () => { it('e-mail is build with all data', async () => {
const subject = `${CONFIG.APPLICATION_NAME} Benachrichtigung` const subject = `${CONFIG.APPLICATION_NAME} Benachrichtigung`
const content = `Du hast mindestens eine Benachrichtigung erhalten. Klick auf diesen Button, um sie anzusehen:` const content = `Du hast mindestens eine Benachrichtigung erhalten. Klick auf diesen Button, um sie anzusehen:`
testEmailData(null, notificationTemplate, notificationTemplateData('de'), { testEmailData(null, notificationTemplate, notificationTemplateData('de'), [
subject, ...textsStandard,
{
templPropName: 'subject',
isContaining: false,
text: subject,
},
actionUrl, actionUrl,
name, name,
settingsUrl,
content, content,
}) settingsUrl,
])
}) })
}) })
}) })