Skip to content

Commit e4ac9a3

Browse files
committed
Update mock in api DSAR email test
1 parent a563a99 commit e4ac9a3

1 file changed

Lines changed: 21 additions & 34 deletions

File tree

‎src/pages/api/gdpr/__tests__/dsarVerificationEmails.spec.ts‎

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
22
import { sendDSARVerificationEmail } from '@pages/api/gdpr/_dsarVerificationEmails'
33

4-
// Mock environment utilities BEFORE importing the module under test
5-
vi.mock('@lib/config/environmentServer', () => ({
6-
isDev: vi.fn(() => false),
7-
isTest: vi.fn(() => false),
4+
const { envMocks, siteUrlMock } = vi.hoisted(() => ({
5+
envMocks: {
6+
isDev: vi.fn(() => false),
7+
isTest: vi.fn(() => false),
8+
getResendApiKey: vi.fn(() => 'test-resend-key'),
9+
},
10+
siteUrlMock: vi.fn(() => 'https://webstackbuilders.com'),
811
}))
912

10-
// Mock getSiteUrl
11-
vi.mock('@lib/config', () => ({
12-
getSiteUrl: vi.fn(() => 'https://webstackbuilders.com'),
13-
}))
13+
vi.mock('@pages/api/_environment/environmentApi', () => envMocks)
1414

15-
// Mock astro:env/server
16-
vi.mock('astro:env/server', () => ({
17-
RESEND_API_KEY: 'test-resend-key',
15+
vi.mock('@pages/api/_environment/siteUrlApi', () => ({
16+
getSiteUrl: siteUrlMock,
1817
}))
1918

2019
// Create mock send function at module level
@@ -36,30 +35,18 @@ vi.mock('resend', () => {
3635
describe('GDPR Email Utils', () => {
3736
let consoleLogSpy: ReturnType<typeof vi.fn>
3837
let consoleErrorSpy: ReturnType<typeof vi.fn>
39-
let isDev: ReturnType<typeof vi.fn>
40-
let isTest: ReturnType<typeof vi.fn>
41-
let getSiteUrl: ReturnType<typeof vi.fn>
4238

43-
beforeEach(async () => {
44-
// Get the mocked functions
45-
const envUtils = await import('@lib/config/environmentServer')
46-
isDev = envUtils.isDev as ReturnType<typeof vi.fn>
47-
isTest = envUtils.isTest as ReturnType<typeof vi.fn>
39+
beforeEach(() => {
40+
vi.resetModules()
41+
vi.clearAllMocks()
4842

49-
const config = await import('@lib/config')
50-
getSiteUrl = config.getSiteUrl as ReturnType<typeof vi.fn>
43+
envMocks.isDev.mockReturnValue(false)
44+
envMocks.isTest.mockReturnValue(false)
45+
envMocks.getResendApiKey.mockReturnValue('test-resend-key')
46+
siteUrlMock.mockReturnValue('https://webstackbuilders.com')
5147

52-
// Mock console methods
5348
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
5449
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
55-
56-
// Reset to production mode by default
57-
isDev.mockReturnValue(false)
58-
isTest.mockReturnValue(false)
59-
getSiteUrl.mockReturnValue('https://webstackbuilders.com')
60-
61-
// Reset all mocks
62-
vi.clearAllMocks()
6350
})
6451

6552
afterEach(() => {
@@ -70,7 +57,7 @@ describe('GDPR Email Utils', () => {
7057
describe('sendDSARVerificationEmail', () => {
7158
describe('in development/test environment', () => {
7259
it('should log email details instead of sending when isDev() returns true', async () => {
73-
isDev.mockReturnValue(true)
60+
envMocks.isDev.mockReturnValue(true)
7461

7562
await sendDSARVerificationEmail('test@example.com', 'test-token', 'ACCESS')
7663

@@ -86,7 +73,7 @@ describe('GDPR Email Utils', () => {
8673
})
8774

8875
it('should log email details instead of sending when isTest() returns true', async () => {
89-
isTest.mockReturnValue(true)
76+
envMocks.isTest.mockReturnValue(true)
9077

9178
await sendDSARVerificationEmail('test@example.com', 'test-token', 'DELETE')
9279

@@ -163,7 +150,7 @@ describe('GDPR Email Utils', () => {
163150
})
164151

165152
it('should use getSiteUrl() return value for verification URL', async () => {
166-
getSiteUrl.mockReturnValue('http://localhost:4321')
153+
siteUrlMock.mockReturnValue('http://localhost:4321')
167154
mockSend.mockResolvedValue({
168155
data: { id: 'message-id-789' },
169156
error: null,
@@ -229,7 +216,7 @@ describe('GDPR Email Utils', () => {
229216
})
230217

231218
it('should generate proper verification URLs with tokens', async () => {
232-
getSiteUrl.mockReturnValue('https://example.com')
219+
siteUrlMock.mockReturnValue('https://example.com')
233220
mockSend.mockResolvedValue({
234221
data: { id: 'message-id-url' },
235222
error: null,

0 commit comments

Comments
 (0)