Skip to content

Commit

Permalink
fix: mail.assert to allow mail classes that accepts constructor argum…
Browse files Browse the repository at this point in the history
…ents

Fixes: #95
  • Loading branch information
thetutlage committed Mar 27, 2024
1 parent 667c796 commit 7f142f7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/fake_mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
MailerContract,
MailerMessenger,
MessageSearchOptions,
NormalizeConstructor,
MessageComposeCallback,
} from './types.js'

Expand Down Expand Up @@ -63,7 +64,7 @@ class MailsCollection {
* Assert the mentioned mail was sent during the fake
* mode
*/
assertSent<T extends typeof BaseMail>(
assertSent<T extends NormalizeConstructor<typeof BaseMail>>(
mailConstructor: T,
findFn?: (mail: InstanceType<T>) => boolean
) {
Expand All @@ -85,7 +86,7 @@ class MailsCollection {
* Assert the mentioned mail was NOT sent during the fake
* mode
*/
assertNotSent<T extends typeof BaseMail>(
assertNotSent<T extends NormalizeConstructor<typeof BaseMail>>(
mailConstructor: T,
findFn?: (mail: InstanceType<T>) => boolean
) {
Expand All @@ -112,8 +113,11 @@ class MailsCollection {
* Assert the mentioned mail was sent for expected number
* of times
*/
assertSentCount(mailConstructor: typeof BaseMail, count: number): void
assertSentCount(mailConstructor: typeof BaseMail | number, count?: number): void {
assertSentCount(mailConstructor: NormalizeConstructor<typeof BaseMail>, count: number): void
assertSentCount(
mailConstructor: NormalizeConstructor<typeof BaseMail> | number,
count?: number
): void {
if (typeof mailConstructor === 'number') {
const actual = this.#sent.length
const expected = mailConstructor
Expand Down Expand Up @@ -161,7 +165,7 @@ class MailsCollection {
* Assert the mentioned mail was queued during the fake
* mode
*/
assertQueued<T extends typeof BaseMail>(
assertQueued<T extends NormalizeConstructor<typeof BaseMail>>(
mailConstructor: T,
findFn?: (mail: InstanceType<T>) => boolean
) {
Expand All @@ -183,7 +187,7 @@ class MailsCollection {
* Assert the mentioned mail was NOT queued during the fake
* mode
*/
assertNotQueued<T extends typeof BaseMail>(
assertNotQueued<T extends NormalizeConstructor<typeof BaseMail>>(
mailConstructor: T,
findFn?: (mail: InstanceType<T>) => boolean
) {
Expand All @@ -210,8 +214,11 @@ class MailsCollection {
* Assert the mentioned mail was sequeuednt for expected number
* of times
*/
assertQueuedCount(mailConstructor: typeof BaseMail, count: number): void
assertQueuedCount(mailConstructor: typeof BaseMail | number, count?: number): void {
assertQueuedCount(mailConstructor: NormalizeConstructor<typeof BaseMail>, count: number): void
assertQueuedCount(
mailConstructor: NormalizeConstructor<typeof BaseMail> | number,
count?: number
): void {
if (typeof mailConstructor === 'number') {
const actual = this.#queued.length
const expected = mailConstructor
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,8 @@ export interface MailService
extends MailManager<
MailersList extends Record<string, MailManagerTransportFactory> ? MailersList : never
> {}

export type Constructor = abstract new (...args: any[]) => any
export type NormalizeConstructor<T extends Constructor> = {
new (...args: any[]): InstanceType<T>
} & Omit<T, 'constructor'>
35 changes: 35 additions & 0 deletions tests/unit/fake_mailer/mails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,41 @@ test.group('Fake mailer | mails | send', (group) => {
return true
})
})

test('assert using mail class that accepts constructor arguments', async ({ assert, expectTypeOf }) => {

Check failure on line 195 in tests/unit/fake_mailer/mails.spec.ts

View workflow job for this annotation

GitHub Actions / linux (20.10.0)

Replace `·assert,·expectTypeOf` with `⏎····assert,⏎····expectTypeOf,⏎·`

Check failure on line 195 in tests/unit/fake_mailer/mails.spec.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Replace `·assert,·expectTypeOf` with `⏎····assert,⏎····expectTypeOf,⏎·`
const emitter = new Emitter<MailEvents>(app)
const mailer = new FakeMailer('mailgun', emitter, {})
const { mails } = mailer

class VerifyEmail<UserId extends number> extends BaseMail {
from: string = '[email protected]'
subject: string = 'Verify your email address'

constructor(public userId: UserId) {
super()
}

prepare() {
this.message.to('[email protected]')
}
}

const response = await mailer.send(new VerifyEmail(1))
assert.exists(response.messageId)
assert.deepEqual(response.envelope, { from: '[email protected]', to: ['[email protected]'] })

mails.assertSent(VerifyEmail)
mails.assertSent(VerifyEmail, (mail) => {
expectTypeOf(mail).toMatchTypeOf<VerifyEmail<number>>
return mail.message.hasTo('[email protected]') && mail.message.hasFrom('[email protected]')
})

assert.throws(() => {
return mails.assertSent(VerifyEmail, (mail) => {
return mail.message.hasTo('[email protected]')
})
}, 'Expected mail "VerifyEmail" was not sent')
})
})

test.group('Fake mailer | mails | sendLater', (group) => {
Expand Down

0 comments on commit 7f142f7

Please sign in to comment.