|
|
@@ -1,4 +1,5 @@
|
|
|
/* tslint:disable:no-non-null-assertion */
|
|
|
+import { Test, TestingModule } from '@nestjs/testing';
|
|
|
import path from 'path';
|
|
|
|
|
|
import { LanguageCode } from '../../common/lib/generated-types';
|
|
|
@@ -18,32 +19,32 @@ describe('EmailPlugin', () => {
|
|
|
let eventBus: EventBus;
|
|
|
let onSend: jest.Mock;
|
|
|
|
|
|
- async function initPluginWithHandlers(handlers: Array<EmailEventHandler<string, any>>, options?: Partial<EmailPluginOptions>) {
|
|
|
- eventBus = new EventBus();
|
|
|
+ async function initPluginWithHandlers(
|
|
|
+ handlers: Array<EmailEventHandler<string, any>>,
|
|
|
+ options?: Partial<EmailPluginOptions>,
|
|
|
+ ) {
|
|
|
onSend = jest.fn();
|
|
|
- plugin = new EmailPlugin({
|
|
|
- templatePath: path.join(__dirname, '../test-templates'),
|
|
|
- transport: {
|
|
|
- type: 'testing',
|
|
|
- onSend,
|
|
|
- },
|
|
|
- handlers,
|
|
|
- ...options,
|
|
|
- });
|
|
|
-
|
|
|
- const inject = (token: any): any => {
|
|
|
- if (token === EventBus) {
|
|
|
- return eventBus;
|
|
|
- } else {
|
|
|
- throw new Error(`Was not expecting to inject the token ${token}`);
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- await plugin.onBootstrap(inject);
|
|
|
+ const module = await Test.createTestingModule({
|
|
|
+ imports: [
|
|
|
+ EmailPlugin.init({
|
|
|
+ templatePath: path.join(__dirname, '../test-templates'),
|
|
|
+ transport: {
|
|
|
+ type: 'testing',
|
|
|
+ onSend,
|
|
|
+ },
|
|
|
+ handlers,
|
|
|
+ ...options,
|
|
|
+ }),
|
|
|
+ ],
|
|
|
+ }).compile();
|
|
|
+
|
|
|
+ plugin = module.get(EmailPlugin);
|
|
|
+ eventBus = module.get(EventBus);
|
|
|
+ await plugin.onVendureBootstrap();
|
|
|
+ return module;
|
|
|
}
|
|
|
|
|
|
describe('event filtering', () => {
|
|
|
-
|
|
|
const ctx = {
|
|
|
channel: { code: DEFAULT_CHANNEL_CODE },
|
|
|
languageCode: LanguageCode.en,
|
|
|
@@ -56,7 +57,7 @@ describe('EmailPlugin', () => {
|
|
|
.setRecipient(() => 'test@test.com')
|
|
|
.setSubject('test subject');
|
|
|
|
|
|
- await initPluginWithHandlers([handler]);
|
|
|
+ const module = await initPluginWithHandlers([handler]);
|
|
|
|
|
|
eventBus.publish(new MockEvent(ctx, false));
|
|
|
await pause();
|
|
|
@@ -65,6 +66,7 @@ describe('EmailPlugin', () => {
|
|
|
eventBus.publish(new MockEvent(ctx, true));
|
|
|
await pause();
|
|
|
expect(onSend).toHaveBeenCalledTimes(1);
|
|
|
+ await module.close();
|
|
|
});
|
|
|
|
|
|
it('multiple filters', async () => {
|
|
|
@@ -75,7 +77,7 @@ describe('EmailPlugin', () => {
|
|
|
.setRecipient(() => 'test@test.com')
|
|
|
.setSubject('test subject');
|
|
|
|
|
|
- await initPluginWithHandlers([handler]);
|
|
|
+ const module = await initPluginWithHandlers([handler]);
|
|
|
|
|
|
eventBus.publish(new MockEvent(ctx, true));
|
|
|
await pause();
|
|
|
@@ -84,6 +86,7 @@ describe('EmailPlugin', () => {
|
|
|
eventBus.publish(new MockEvent({ ...ctx, user: 'joe' }, true));
|
|
|
await pause();
|
|
|
expect(onSend).toHaveBeenCalledTimes(1);
|
|
|
+ await module.close();
|
|
|
});
|
|
|
});
|
|
|
|
|
|
@@ -100,11 +103,12 @@ describe('EmailPlugin', () => {
|
|
|
.setSubject('Hello {{ subjectVar }}')
|
|
|
.setTemplateVars(event => ({ subjectVar: 'foo' }));
|
|
|
|
|
|
- await initPluginWithHandlers([handler]);
|
|
|
+ const module = await initPluginWithHandlers([handler]);
|
|
|
|
|
|
eventBus.publish(new MockEvent(ctx, true));
|
|
|
await pause();
|
|
|
expect(onSend.mock.calls[0][0].subject).toBe('Hello foo');
|
|
|
+ await module.close();
|
|
|
});
|
|
|
|
|
|
it('interpolates body', async () => {
|
|
|
@@ -114,11 +118,12 @@ describe('EmailPlugin', () => {
|
|
|
.setSubject('Hello')
|
|
|
.setTemplateVars(event => ({ testVar: 'this is the test var' }));
|
|
|
|
|
|
- await initPluginWithHandlers([handler]);
|
|
|
+ const module = await initPluginWithHandlers([handler]);
|
|
|
|
|
|
eventBus.publish(new MockEvent(ctx, true));
|
|
|
await pause();
|
|
|
expect(onSend.mock.calls[0][0].body).toContain('this is the test var');
|
|
|
+ await module.close();
|
|
|
});
|
|
|
|
|
|
it('interpolates globalTemplateVars', async () => {
|
|
|
@@ -127,11 +132,14 @@ describe('EmailPlugin', () => {
|
|
|
.setRecipient(() => 'test@test.com')
|
|
|
.setSubject('Hello {{ globalVar }}');
|
|
|
|
|
|
- await initPluginWithHandlers([handler], { globalTemplateVars: { globalVar: 'baz' } });
|
|
|
+ const module = await initPluginWithHandlers([handler], {
|
|
|
+ globalTemplateVars: { globalVar: 'baz' },
|
|
|
+ });
|
|
|
|
|
|
eventBus.publish(new MockEvent(ctx, true));
|
|
|
await pause();
|
|
|
expect(onSend.mock.calls[0][0].subject).toBe('Hello baz');
|
|
|
+ await module.close();
|
|
|
});
|
|
|
|
|
|
it('globalTemplateVars available in setTemplateVars method', async () => {
|
|
|
@@ -141,11 +149,14 @@ describe('EmailPlugin', () => {
|
|
|
.setSubject('Hello {{ testVar }}')
|
|
|
.setTemplateVars((event, globals) => ({ testVar: globals.globalVar + ' quux' }));
|
|
|
|
|
|
- await initPluginWithHandlers([handler], { globalTemplateVars: { globalVar: 'baz' } });
|
|
|
+ const module = await initPluginWithHandlers([handler], {
|
|
|
+ globalTemplateVars: { globalVar: 'baz' },
|
|
|
+ });
|
|
|
|
|
|
eventBus.publish(new MockEvent(ctx, true));
|
|
|
await pause();
|
|
|
expect(onSend.mock.calls[0][0].subject).toBe('Hello baz quux');
|
|
|
+ await module.close();
|
|
|
});
|
|
|
|
|
|
it('setTemplateVars overrides globals', async () => {
|
|
|
@@ -155,11 +166,12 @@ describe('EmailPlugin', () => {
|
|
|
.setSubject('Hello {{ name }}')
|
|
|
.setTemplateVars((event, globals) => ({ name: 'quux' }));
|
|
|
|
|
|
- await initPluginWithHandlers([handler], { globalTemplateVars: { name: 'baz' } });
|
|
|
+ const module = await initPluginWithHandlers([handler], { globalTemplateVars: { name: 'baz' } });
|
|
|
|
|
|
eventBus.publish(new MockEvent(ctx, true));
|
|
|
await pause();
|
|
|
expect(onSend.mock.calls[0][0].subject).toBe('Hello quux');
|
|
|
+ await module.close();
|
|
|
});
|
|
|
});
|
|
|
|
|
|
@@ -182,7 +194,7 @@ describe('EmailPlugin', () => {
|
|
|
subject: 'Servus, {{ name }}!',
|
|
|
});
|
|
|
|
|
|
- await initPluginWithHandlers([handler]);
|
|
|
+ const module = await initPluginWithHandlers([handler]);
|
|
|
|
|
|
eventBus.publish(new MockEvent({ ...ctx, languageCode: LanguageCode.ta }, true));
|
|
|
await pause();
|
|
|
@@ -193,13 +205,20 @@ describe('EmailPlugin', () => {
|
|
|
await pause();
|
|
|
expect(onSend.mock.calls[1][0].subject).toBe('Servus, Test!');
|
|
|
expect(onSend.mock.calls[1][0].body).toContain('German body.');
|
|
|
+ await module.close();
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('orderConfirmationHandler', () => {
|
|
|
-
|
|
|
+ let module: TestingModule;
|
|
|
beforeEach(async () => {
|
|
|
- await initPluginWithHandlers([orderConfirmationHandler], { templatePath: path.join(__dirname, '../templates') });
|
|
|
+ module = await initPluginWithHandlers([orderConfirmationHandler], {
|
|
|
+ templatePath: path.join(__dirname, '../templates'),
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ afterEach(async () => {
|
|
|
+ await module.close();
|
|
|
});
|
|
|
|
|
|
const ctx = {
|
|
|
@@ -207,12 +226,12 @@ describe('EmailPlugin', () => {
|
|
|
languageCode: LanguageCode.en,
|
|
|
} as any;
|
|
|
|
|
|
- const order = {
|
|
|
+ const order = ({
|
|
|
code: 'ABCDE',
|
|
|
customer: {
|
|
|
emailAddress: 'test@test.com',
|
|
|
},
|
|
|
- } as Partial<Order> as any;
|
|
|
+ } as Partial<Order>) as any;
|
|
|
|
|
|
it('filters events with wrong order state', async () => {
|
|
|
eventBus.publish(new OrderStateTransitionEvent('AddingItems', 'ArrangingPayment', ctx, order));
|