148 lines
5.5 KiB
TypeScript
148 lines
5.5 KiB
TypeScript
import { expect, tap } from '@push.rocks/tapbundle';
|
|
import * as smartmail from '../ts/index.js';
|
|
import * as plugins from '../ts/smartmail.plugins.js';
|
|
|
|
let emailAddressValidatorInstance: smartmail.EmailAddressValidator;
|
|
|
|
// EmailAddressValidator Tests
|
|
tap.test('should create an instance of EmailAddressValidator', async () => {
|
|
emailAddressValidatorInstance = new smartmail.EmailAddressValidator();
|
|
expect(emailAddressValidatorInstance).toBeInstanceOf(smartmail.EmailAddressValidator);
|
|
});
|
|
|
|
tap.test('should validate an email with detailed information', async () => {
|
|
const result = await emailAddressValidatorInstance.validate('sandbox@bleu.de');
|
|
expect(result.freemail).toBeFalse();
|
|
expect(result.disposable).toBeFalse();
|
|
expect(result.formatValid).toBeTrue();
|
|
expect(result.localPartValid).toBeTrue();
|
|
expect(result.domainPartValid).toBeTrue();
|
|
expect(result.reason).toBeDefined();
|
|
});
|
|
|
|
tap.test('should recognize an email as freemail', async () => {
|
|
const result = await emailAddressValidatorInstance.validate('sandbox@gmail.com');
|
|
expect(result.freemail).toBeTrue();
|
|
expect(result.disposable).toBeFalse();
|
|
expect(result.formatValid).toBeTrue();
|
|
expect(result.valid).toBeTrue();
|
|
});
|
|
|
|
tap.test('should recognize an email as disposable', async () => {
|
|
const result = await emailAddressValidatorInstance.validate('sandbox@gmx.de');
|
|
expect(result.freemail).toBeFalse();
|
|
expect(result.disposable).toBeTrue();
|
|
expect(result.formatValid).toBeTrue();
|
|
});
|
|
|
|
tap.test('should detect invalid email format', async () => {
|
|
const result = await emailAddressValidatorInstance.validate('invalid-email');
|
|
expect(result.formatValid).toBeFalse();
|
|
expect(result.valid).toBeFalse();
|
|
expect(result.reason).toEqual('Invalid email format');
|
|
});
|
|
|
|
tap.test('should validate email format parts separately', async () => {
|
|
expect(emailAddressValidatorInstance.isValidEmailFormat('valid.email@example.com')).toBeTrue();
|
|
expect(emailAddressValidatorInstance.isValidEmailFormat('invalid-email')).toBeFalse();
|
|
expect(emailAddressValidatorInstance.isValidLocalPart('valid.local.part')).toBeTrue();
|
|
expect(emailAddressValidatorInstance.isValidLocalPart('invalid..part')).toBeFalse();
|
|
expect(emailAddressValidatorInstance.isValidDomainPart('example.com')).toBeTrue();
|
|
expect(emailAddressValidatorInstance.isValidDomainPart('invalid')).toBeFalse();
|
|
});
|
|
|
|
// Smartmail Tests
|
|
let testSmartmail: smartmail.Smartmail<any>;
|
|
|
|
tap.test('should create a SmartMail instance', async () => {
|
|
testSmartmail = new smartmail.Smartmail({
|
|
body: 'hi there',
|
|
from: 'noreply@mail.lossless.com',
|
|
subject: 'hi from here',
|
|
});
|
|
expect(testSmartmail).toBeInstanceOf(smartmail.Smartmail);
|
|
});
|
|
|
|
tap.test('should handle email recipients', async () => {
|
|
testSmartmail.addRecipient('user1@example.com');
|
|
testSmartmail.addRecipients(['user2@example.com', 'user3@example.com'], 'cc');
|
|
testSmartmail.addRecipient('user4@example.com', 'bcc');
|
|
|
|
expect(testSmartmail.options.to!.length).toEqual(1);
|
|
expect(testSmartmail.options.cc!.length).toEqual(2);
|
|
expect(testSmartmail.options.bcc!.length).toEqual(1);
|
|
});
|
|
|
|
tap.test('should set reply-to and priority', async () => {
|
|
testSmartmail.setReplyTo('replies@example.com');
|
|
testSmartmail.setPriority('high');
|
|
|
|
expect(testSmartmail.options.replyTo).toEqual('replies@example.com');
|
|
expect(testSmartmail.options.priority).toEqual('high');
|
|
});
|
|
|
|
tap.test('should apply template data to subject and body', async () => {
|
|
const templateMailer = new smartmail.Smartmail({
|
|
subject: 'Hello {{name}}',
|
|
body: 'Welcome, {{name}}! Your ID is {{userId}}.',
|
|
from: 'noreply@example.com'
|
|
});
|
|
|
|
const data = { name: 'John Doe', userId: '12345' };
|
|
|
|
expect(templateMailer.getSubject(data)).toEqual('Hello John Doe');
|
|
expect(templateMailer.getBody(data)).toEqual('Welcome, John Doe! Your ID is 12345.');
|
|
});
|
|
|
|
tap.test('should handle HTML email body', async () => {
|
|
const htmlMailer = new smartmail.Smartmail({
|
|
subject: 'HTML Test',
|
|
body: 'Plain text version',
|
|
htmlBody: '<h1>{{title}}</h1><p>This is an HTML email with {{variable}}.</p>',
|
|
from: 'noreply@example.com'
|
|
});
|
|
|
|
const data = { title: 'Welcome', variable: 'dynamic content' };
|
|
const htmlContent = htmlMailer.getHtmlBody(data);
|
|
|
|
expect(htmlContent).toEqual('<h1>Welcome</h1><p>This is an HTML email with dynamic content.</p>');
|
|
});
|
|
|
|
tap.test('should convert to MIME format', async () => {
|
|
const mimeMailer = new smartmail.Smartmail({
|
|
subject: 'MIME Test',
|
|
body: 'Plain text content',
|
|
htmlBody: '<p>HTML content</p>',
|
|
from: 'sender@example.com',
|
|
to: ['recipient@example.com'],
|
|
headers: { 'X-Custom': 'Value' }
|
|
});
|
|
|
|
const mimeObj = await mimeMailer.toMimeFormat();
|
|
|
|
expect(mimeObj.from).toEqual('sender@example.com');
|
|
expect(mimeObj.to).toInclude('recipient@example.com');
|
|
expect(mimeObj.subject).toEqual('MIME Test');
|
|
expect(mimeObj.text).toEqual('Plain text content');
|
|
expect(mimeObj.html).toEqual('<p>HTML content</p>');
|
|
expect(mimeObj.headers['X-Custom']).toEqual('Value');
|
|
});
|
|
|
|
tap.test('should add email headers', async () => {
|
|
const headerMailer = new smartmail.Smartmail({
|
|
subject: 'Header Test',
|
|
body: 'Test body',
|
|
from: 'noreply@example.com'
|
|
});
|
|
|
|
headerMailer.addHeader('X-Test-Header', 'TestValue');
|
|
headerMailer.addHeader('X-Tracking-ID', '12345');
|
|
|
|
const mimeObj = await headerMailer.toMimeFormat();
|
|
|
|
expect(mimeObj.headers['X-Test-Header']).toEqual('TestValue');
|
|
expect(mimeObj.headers['X-Tracking-ID']).toEqual('12345');
|
|
});
|
|
|
|
tap.start();
|