|
| 1 | +import type { ContextGenerator } from 'vintasend/dist/services/notification-context-registry'; |
| 2 | +import type { DatabaseNotification } from 'vintasend/dist/types/notification'; |
| 3 | +import type { BaseLogger } from 'vintasend/dist/services/loggers/base-logger'; |
| 4 | +import { PugInlineEmailTemplateRendererFactory, PugInlineEmailTemplateRenderer } from '../pug-inline-email-template-renderer'; |
| 5 | + |
| 6 | +type MockConfig = { |
| 7 | + ContextMap: { testContext: ContextGenerator }; |
| 8 | + NotificationIdType: string; |
| 9 | + UserIdType: string; |
| 10 | +}; |
| 11 | + |
| 12 | +describe('PugInlineEmailTemplateRenderer', () => { |
| 13 | + let renderer: PugInlineEmailTemplateRenderer<MockConfig>; |
| 14 | + let mockNotification: DatabaseNotification<MockConfig>; |
| 15 | + |
| 16 | + // Define template strings inline (plain text mode with interpolation) |
| 17 | + const templates = { |
| 18 | + 'test-notification': `html |
| 19 | + head |
| 20 | + title Email |
| 21 | + body |
| 22 | + h1 Hello #{name}! |
| 23 | + p Your message: #{message}`, |
| 24 | + 'test-subject': `| Welcome #{name}`, |
| 25 | + 'invalid-template': `p= undefinedVariable.nonExistentProperty`, |
| 26 | + 'invalid-subject': `| = undefinedVariable.nonExistentProperty`, |
| 27 | + }; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + renderer = new PugInlineEmailTemplateRendererFactory<MockConfig>().create(templates); |
| 31 | + mockNotification = { |
| 32 | + id: '123', |
| 33 | + notificationType: 'EMAIL' as const, |
| 34 | + contextName: 'testContext', |
| 35 | + contextParameters: {}, |
| 36 | + userId: '456', |
| 37 | + title: 'Test Notification', |
| 38 | + bodyTemplate: 'test-notification', |
| 39 | + subjectTemplate: 'test-subject', |
| 40 | + extraParams: {}, |
| 41 | + contextUsed: null, |
| 42 | + adapterUsed: null, |
| 43 | + status: 'PENDING_SEND' as const, |
| 44 | + sentAt: null, |
| 45 | + readAt: null, |
| 46 | + sendAfter: new Date(), |
| 47 | + gitCommitSha: null, |
| 48 | + }; |
| 49 | + }); |
| 50 | + |
| 51 | + it('should render email template with context', async () => { |
| 52 | + const context = { |
| 53 | + name: 'John', |
| 54 | + message: 'Hello World', |
| 55 | + }; |
| 56 | + |
| 57 | + const result = await renderer.render(mockNotification, context); |
| 58 | + |
| 59 | + expect(result.subject).toBe('Welcome John'); |
| 60 | + expect(result.body).toContain('Hello John!'); |
| 61 | + expect(result.body).toContain('Your message: Hello World'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should render email template from template content', async () => { |
| 65 | + const result = await renderer.renderFromTemplateContent( |
| 66 | + mockNotification, |
| 67 | + { |
| 68 | + subject: '| Welcome #{name}', |
| 69 | + body: 'p Hello #{name}!\np Message: #{message}', |
| 70 | + }, |
| 71 | + { |
| 72 | + name: 'John', |
| 73 | + message: 'Hello World', |
| 74 | + }, |
| 75 | + ); |
| 76 | + |
| 77 | + expect(result.subject).toBe('Welcome John'); |
| 78 | + expect(result.body).toContain('Hello John!'); |
| 79 | + expect(result.body).toContain('Message: Hello World'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should throw when renderFromTemplateContent receives empty subject', async () => { |
| 83 | + await expect( |
| 84 | + renderer.renderFromTemplateContent( |
| 85 | + mockNotification, |
| 86 | + { |
| 87 | + subject: null, |
| 88 | + body: 'p Body', |
| 89 | + }, |
| 90 | + {}, |
| 91 | + ), |
| 92 | + ).rejects.toThrow('Subject template is required'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should throw error when subject template is missing', async () => { |
| 96 | + const notification = { |
| 97 | + ...mockNotification, |
| 98 | + id: 'test-notification', |
| 99 | + bodyTemplate: 'test-notification', |
| 100 | + subjectTemplate: null, |
| 101 | + userId: 'user123', |
| 102 | + }; |
| 103 | + |
| 104 | + await expect(renderer.render(notification, {})).rejects.toThrow('Subject template is required'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle empty context', async () => { |
| 108 | + const notification = { |
| 109 | + ...mockNotification, |
| 110 | + id: 'test-notification', |
| 111 | + bodyTemplate: 'test-notification', |
| 112 | + subjectTemplate: 'test-subject', |
| 113 | + userId: 'user123', |
| 114 | + }; |
| 115 | + |
| 116 | + const result = await renderer.render(notification, {}); |
| 117 | + |
| 118 | + expect(result.subject).toBe('Welcome '); |
| 119 | + expect(result.body).toContain('Hello !'); |
| 120 | + expect(result.body).toContain('Your message: '); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should throw error when template key not found', async () => { |
| 124 | + const notification = { |
| 125 | + ...mockNotification, |
| 126 | + subjectTemplate: 'non-existent-subject', |
| 127 | + }; |
| 128 | + |
| 129 | + await expect(renderer.render(notification, {})).rejects.toThrow('Subject template "non-existent-subject" not found in templates'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should throw error when body template not found', async () => { |
| 133 | + const notification = { |
| 134 | + ...mockNotification, |
| 135 | + bodyTemplate: 'non-existent-body', |
| 136 | + }; |
| 137 | + |
| 138 | + await expect(renderer.render(notification, {})).rejects.toThrow('Body template "non-existent-body" not found in templates'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should handle template runtime errors', async () => { |
| 142 | + const notification = { |
| 143 | + ...mockNotification, |
| 144 | + bodyTemplate: 'invalid-template', |
| 145 | + subjectTemplate: 'invalid-subject', |
| 146 | + }; |
| 147 | + |
| 148 | + await expect(renderer.render(notification, { undefinedVariable: undefined })).rejects.toThrow(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should throw error when bodyTemplate is empty', async () => { |
| 152 | + const notification = { |
| 153 | + ...mockNotification, |
| 154 | + bodyTemplate: '', |
| 155 | + }; |
| 156 | + |
| 157 | + await expect(renderer.render(notification, { name: 'Test' })).rejects.toThrow('Body template is required'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should throw error when subjectTemplate is empty', async () => { |
| 161 | + const notification = { |
| 162 | + ...mockNotification, |
| 163 | + subjectTemplate: '', |
| 164 | + }; |
| 165 | + |
| 166 | + await expect(renderer.render(notification, { name: 'Test' })).rejects.toThrow('Subject template is required'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should create renderer with empty templates object', () => { |
| 170 | + const emptyRenderer = new PugInlineEmailTemplateRendererFactory<MockConfig>().create({}); |
| 171 | + expect(emptyRenderer).toBeDefined(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should support logger injection', () => { |
| 175 | + const mockLogger: BaseLogger = { |
| 176 | + info: vi.fn(), |
| 177 | + error: vi.fn(), |
| 178 | + warn: vi.fn(), |
| 179 | + }; |
| 180 | + |
| 181 | + renderer.injectLogger(mockLogger); |
| 182 | + |
| 183 | + // biome-ignore lint/complexity/useLiteralKeys: accessing private attribute |
| 184 | + expect((renderer as any).logger).toBe(mockLogger); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should use logger when rendering fails', async () => { |
| 188 | + const mockLogger: BaseLogger = { |
| 189 | + info: vi.fn(), |
| 190 | + error: vi.fn(), |
| 191 | + warn: vi.fn(), |
| 192 | + }; |
| 193 | + |
| 194 | + renderer.injectLogger(mockLogger); |
| 195 | + |
| 196 | + const notification = { |
| 197 | + ...mockNotification, |
| 198 | + bodyTemplate: 'invalid-template', |
| 199 | + subjectTemplate: 'invalid-subject', |
| 200 | + }; |
| 201 | + |
| 202 | + await expect(renderer.render(notification, { undefinedVariable: undefined })).rejects.toThrow(); |
| 203 | + expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining('[PugInlineEmailTemplateRenderer] Error rendering body template')); |
| 204 | + }); |
| 205 | +}); |
0 commit comments