diff --git a/app/src/__tests__/build.test.ts b/app/src/__tests__/build.test.ts new file mode 100644 index 0000000..6815969 --- /dev/null +++ b/app/src/__tests__/build.test.ts @@ -0,0 +1,83 @@ +import path from 'path' +import { beforeAll, describe, expect, it } from 'vitest' + +import { buildHtml } from '@/lib/email/build.js' +import { directory } from '@/utils/helpers.js' +import { EmailSchemaMessages } from '@/types/email.schema.js' + +// buildHtml resolves the EJS template relative to __dirname (set to the build.ts location) +beforeAll(() => { + globalThis.__dirname = path.resolve(directory(import.meta.url), '..', 'lib', 'email') +}) + +const TEST_RECIPIENTS = ['tester@gmail.com'] +const TEST_SENDER = 'sender@gmail.com' + +describe('buildHtml test', () => { + it('should return an HTML string from paragraph content', async () => { + const html = await buildHtml({ + content: ['Hello, World!', 'Second paragraph'], + recipients: TEST_RECIPIENTS, + sender: TEST_SENDER, + }) + + expect(typeof html).toBe('string') + expect(html.length).toBeGreaterThan(0) + }) + + it('should return an HTML string from wysiwyg content', async () => { + const html = await buildHtml({ + content: [], + recipients: TEST_RECIPIENTS, + sender: TEST_SENDER, + wysiwyg: '
Hello, wysiwyg!
', + }) + + expect(typeof html).toBe('string') + expect(html).toContain('Hello, wysiwyg!') + }) + + it('should strip script tags from wysiwyg content', async () => { + const html = await buildHtml({ + content: [], + recipients: TEST_RECIPIENTS, + sender: TEST_SENDER, + wysiwyg: 'Safe content
', + }) + + expect(html).not.toContain('