import { TEMPLATES, TemplateKey, missingPlaceholders, renderTemplate } from '../../src/emailTemplates'; const KEYS: TemplateKey[] = [ 'verification', 'passwordReset', 'favoriteSold', 'favoriteWithdrawn', 'cartReminder', 'emailChanged' ]; describe('the built-in templates', () => { it.each(KEYS)('%s has a default subject and body', (key) => { expect(TEMPLATES[key].defaultSubject.trim()).not.toBe(''); expect(TEMPLATES[key].defaultBody.trim()).not.toBe(''); }); // A default that would be refused on save is a default nobody can edit and // put back. it.each(KEYS)('%s default body satisfies its own required placeholders', (key) => { expect(missingPlaceholders(key, TEMPLATES[key].defaultBody)).toEqual([]); }); }); describe('missingPlaceholders', () => { it('names the placeholder a body has dropped', () => { expect(missingPlaceholders('passwordReset', 'Hello, no link here.')).toEqual(['resetUrl']); }); it('is satisfied once the placeholder is present', () => { expect(missingPlaceholders('passwordReset', 'Reset it [here]({{resetUrl}}).')).toEqual([]); }); it('reports every missing placeholder rather than the first', () => { const missing = missingPlaceholders('cartReminder', 'You have things.'); expect(missing).toContain('itemList'); expect(missing).toContain('cartUrl'); }); it('tolerates whitespace inside the braces', () => { expect(missingPlaceholders('passwordReset', 'Go [here]({{ resetUrl }}).')).toEqual([]); }); }); describe('renderTemplate', () => { const resetValues = { resetUrl: 'https://shop.test/reset-password?token=abc' }; it('substitutes a placeholder into the rendered body', () => { const { html } = renderTemplate('passwordReset', {}, resetValues); expect(html).toContain('https://shop.test/reset-password?token=abc'); expect(html).not.toContain('{{resetUrl}}'); }); it('renders markdown as HTML', () => { const { html } = renderTemplate( 'passwordReset', { body: 'Use **this** [link]({{resetUrl}}).' }, resetValues ); expect(html).toContain('this'); expect(html).toContain(' { const { html } = renderTemplate( 'passwordReset', { body: ' [link]({{resetUrl}})' }, resetValues ); expect(html).not.toContain('