-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(email-plugin): Support custom EmailGenerators and EmailSenders
Co-authored-by: Will Milne <[email protected]>
- Loading branch information
1 parent
5d9ecaa
commit 3e20624
Showing
4 changed files
with
72 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ import { orderConfirmationHandler } from './default-email-handlers'; | |
import { EmailEventHandler } from './event-handler'; | ||
import { EmailEventListener } from './event-listener'; | ||
import { EmailPlugin } from './plugin'; | ||
import { EmailPluginOptions } from './types'; | ||
import { EmailDetails, EmailPluginOptions, EmailSender, EmailTransportOptions } from './types'; | ||
|
||
describe('EmailPlugin', () => { | ||
let eventBus: EventBus; | ||
|
@@ -624,8 +624,42 @@ describe('EmailPlugin', () => { | |
expect(testingLogger.errorSpy.mock.calls[0][0]).toContain(`something went horribly wrong!`); | ||
}); | ||
}); | ||
|
||
describe('custom sender', () => { | ||
it('should allow a custom sender to be utilized', async () => { | ||
const ctx = RequestContext.deserialize({ | ||
_channel: { code: DEFAULT_CHANNEL_CODE }, | ||
_languageCode: LanguageCode.en, | ||
} as any); | ||
const handler = new EmailEventListener('test') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello') | ||
.setTemplateVars(event => ({ subjectVar: 'foo' })); | ||
|
||
const fakeSender = new FakeCustomSender(); | ||
const send = jest.fn(); | ||
fakeSender.send = send; | ||
|
||
await initPluginWithHandlers([handler], { | ||
emailSender: fakeSender, | ||
}); | ||
|
||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
expect(send.mock.calls[0][0].subject).toBe('Hello'); | ||
expect(send.mock.calls[0][0].recipient).toBe('[email protected]'); | ||
expect(send.mock.calls[0][0].from).toBe('"test from" <[email protected]>'); | ||
expect(onSend).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
}); | ||
|
||
class FakeCustomSender implements EmailSender { | ||
send: (email: EmailDetails<'unserialized'>, options: EmailTransportOptions) => void; | ||
} | ||
|
||
const pause = () => new Promise(resolve => setTimeout(resolve, 100)); | ||
|
||
class MockEvent extends VendureEvent { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters