eletrotupi / tcc/ commit / 63ebf27

api: mockQueue and allow to customize/override other user's preference/attrs

Pedro Lucas Porcellis porcellis@eletrotupi.com 29 days ago 63ebf2747779b8e00c4b8154074d000d98ffa751
Parents: fd61f8e
2 file(s) changed
  • api/tests/setup.ts +15 -1
  • api/tests/test-helper.ts +15 -5
api/tests/setup.ts
1 1
import { vi } from 'vitest'
2 2

3 +
const mockQueue = () => ({
4 +
  add: vi.fn().mockResolvedValue({}),
5 +
  addBulk: vi.fn().mockResolvedValue([]),
6 +
  getRepeatableJobs: vi.fn().mockResolvedValue([]),
7 +
  removeRepeatableByKey: vi.fn().mockResolvedValue(undefined),
8 +
})
9 +

10 +
// Mock the barrel export used by controllers and most services
3 11
vi.mock('@app/lib/queue', () => ({
4 12
  bootWorkers: vi.fn(),
5 13
  closeAllWorkers: vi.fn(),
6 14
  closeAllQueues: vi.fn(),
7 15
  scheduleInsights: vi.fn(),
8 -
  getQueue: vi.fn(() => ({ add: vi.fn(), addBulk: vi.fn() })),
16 +
  getQueue: vi.fn(() => mockQueue()),
9 17
  MailJobName: {
10 18
    WelcomeEmail: 'mail:welcome',
11 19
    PasswordReset: 'mail:password-reset',
12 20
    ActivateAccountEmail: 'mail:activate-account',
13 21
  },
14 22
}))
23 +

24 +
// syncMedicineReminderJobs imports getQueue directly from QueueRegistry
25 +
vi.mock('@app/lib/queue/QueueRegistry', () => ({
26 +
  getQueue: vi.fn(() => mockQueue()),
27 +
  closeAllQueues: vi.fn(),
28 +
}))
api/tests/test-helper.ts
1 1
import { PrismaClient } from '@prisma/client'
2 2
import { PrismaPg } from '@prisma/adapter-pg'
3 3
import jwt from 'jsonwebtoken'
4 4
import bcrypt from 'bcryptjs'
5 5

▸ 10 unchanged lines
16 16
  return jwt.sign({ userId, email }, process.env.JWT_SECRET!, { expiresIn: '1h' })
17 17
}
18 18

19 19
export async function createTestUser(
20 20
  prisma: PrismaClient,
21 -
  overrides: { email?: string; firstName?: string; lastName?: string; password?: string } = {}
21 +
  overrides: {
22 +
    email?: string
23 +
    firstName?: string
24 +
    lastName?: string
25 +
    password?: string
26 +
    pushToken?: string
27 +
    notificationsEnabled?: boolean
28 +
  } = {}
22 29
) {
30 +
  const { password, pushToken, notificationsEnabled, ...rest } = overrides
23 31
  return prisma.user.create({
24 32
    data: {
25 -
      email: overrides.email ?? 'test@example.com',
26 -
      firstName: overrides.firstName ?? 'Test',
27 -
      lastName: overrides.lastName ?? 'User',
28 -
      encryptedPassword: await bcrypt.hash(overrides.password ?? 'password123', 10),
33 +
      email: rest.email ?? 'test@example.com',
34 +
      firstName: rest.firstName ?? 'Test',
35 +
      lastName: rest.lastName ?? 'User',
36 +
      encryptedPassword: await bcrypt.hash(password ?? 'password123', 10),
37 +
      ...(pushToken !== undefined && { pushToken }),
38 +
      ...(notificationsEnabled !== undefined && { notificationsEnabled }),
29 39
    },
30 40
  })
31 41
}