Skip to content

Commit b07b417

Browse files
committed
test: fulfill email sendEmail TODO — assert emailProvider.sendEmail is called
The existing test had a TODO comment noting that it should assert emailProvider.sendEmail was called but only verified no exceptions were thrown. Now uses vi.spyOn to verify: - emailProvider.sendEmail is called exactly once - called with the correct recipient address - called with an html body that includes the magic link URL email-provider.server.ts already short-circuits in test mode (returns Promise.resolve without hitting Mailgun), so no mocking of HTTP is needed.
1 parent f2d2e4e commit b07b417

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
import { vi, describe, it, expect, beforeEach } from 'vitest'
12
import { User } from '@prisma/client'
23
import { sendEmail } from '../email.server'
4+
import * as emailProvider from '~/services/email-provider.server'
35
import { userBuilder } from '~/models/__mocks__/user'
46

57
describe('sendEmail', () => {
68
const user = userBuilder() as User
79

10+
beforeEach(() => {
11+
vi.restoreAllMocks()
12+
})
13+
814
it('call emailProvider.sendEmail method', async () => {
15+
const spy = vi.spyOn(emailProvider, 'sendEmail')
16+
917
await sendEmail({
1018
emailAddress: user.email,
1119
magicLink: 'http://localhost:3000/magic',
@@ -14,6 +22,14 @@ describe('sendEmail', () => {
1422
form: new FormData(),
1523
})
1624

17-
// TODO: assert emailProvider.sendEmail was called
25+
expect(spy).toHaveBeenCalledOnce()
26+
expect(spy).toHaveBeenCalledWith(
27+
expect.objectContaining({
28+
to: user.email,
29+
from: expect.any(String),
30+
subject: expect.any(String),
31+
html: expect.stringContaining('localhost:3000/magic'),
32+
})
33+
)
1834
})
1935
})

0 commit comments

Comments
 (0)