eletrotupi / tcc/ commit / d5fb879

api: add some high-level push testing

Pedro Lucas Porcellis porcellis@eletrotupi.com · 4 months ago d5fb879a9f1b81387adef6c43c0babf0a913474c Browse files
Parents 63ebf27
Changed files
1
Additions
+121
Deletions
0
/dev/null → api/tests/jobs/push-notifications.test.ts
1 +
import { describe, it, expect, afterAll, beforeEach, vi } from 'vitest'
2 +
import { medicineReminderProcessor } from '@app/lib/queue/processors/medicineReminders'
3 +
import { dailyReminderProcessor } from '@app/lib/queue/processors/dailyReminders'
4 +
import { buildPrisma, cleanupTestDb, createTestUser } from '../test-helper'
5 +

6 +
// Prevent any Expo push API calls
7 +
vi.mock('@app/lib/sendPushNotification', () => ({
8 +
  sendPushNotification: vi.fn().mockResolvedValue(undefined),
9 +
}))
10 +

11 +
import { sendPushNotification } from '@app/lib/sendPushNotification'
12 +
const mockedSend = vi.mocked(sendPushNotification)
13 +

14 +
const db = buildPrisma()
15 +

16 +
afterAll(() => db.$disconnect())
17 +
beforeEach(async () => {
18 +
  await cleanupTestDb(db)
19 +
  mockedSend.mockClear()
20 +
})
21 +

22 +
describe('medicineReminderProcessor', () => {
23 +
  it('sends a push notification to a user with a push token', async () => {
24 +
    const user = await createTestUser(db, { pushToken: 'ExponentPushToken[abc123]' })
25 +

26 +
    await medicineReminderProcessor({
27 +
      data: {
28 +
        userId: user.id,
29 +
        regimenId: 1,
30 +
        medicineName: 'Sertralina',
31 +
        dosage: '50mg',
32 +
        scheduledTime: '08:00',
33 +
      },
34 +
    } as any)
35 +

36 +
    expect(mockedSend).toHaveBeenCalledOnce()
37 +
    expect(mockedSend).toHaveBeenCalledWith(
38 +
      expect.objectContaining({
39 +
        token: 'ExponentPushToken[abc123]',
40 +
        title: 'Hora de tomar Sertralina',
41 +
        body: '50mg',
42 +
        data: expect.objectContaining({ screen: 'medicine-regimens' }),
43 +
      })
44 +
    )
45 +
  })
46 +

47 +
  it('skips when the user has no push token', async () => {
48 +
    const user = await createTestUser(db) // no pushToken
49 +

50 +
    await medicineReminderProcessor({
51 +
      data: {
52 +
        userId: user.id,
53 +
        regimenId: 1,
54 +
        medicineName: 'Ritalina',
55 +
        dosage: '10mg',
56 +
        scheduledTime: '12:00'
57 +
      },
58 +
    } as any)
59 +

60 +
    expect(mockedSend).not.toHaveBeenCalled()
61 +
  })
62 +

63 +
  it('skips when the user does not exist', async () => {
64 +
    await medicineReminderProcessor({
65 +
      data: {
66 +
        userId: 999999,
67 +
        regimenId: 1,
68 +
        medicineName: 'X',
69 +
        dosage: 'Y',
70 +
        scheduledTime: '08:00'
71 +
      },
72 +
    } as any)
73 +

74 +
    expect(mockedSend).not.toHaveBeenCalled()
75 +
  })
76 +
})
77 +

78 +
describe('dailyReminderProcessor', () => {
79 +
  it('sends a notification when user allows it and token is present', async () => {
80 +
    const user = await createTestUser(db, {
81 +
      pushToken: 'ExponentPushToken[daily123]',
82 +
      notificationsEnabled: true,
83 +
    })
84 +

85 +
    await dailyReminderProcessor({ data: { userId: user.id } } as any)
86 +

87 +
    expect(mockedSend).toHaveBeenCalledOnce()
88 +
    expect(mockedSend).toHaveBeenCalledWith(
89 +
      expect.objectContaining({
90 +
        token: 'ExponentPushToken[daily123]',
91 +
        title: 'Como você está?',
92 +
        data: expect.objectContaining({ screen: 'moods' }),
93 +
      })
94 +
    )
95 +
  })
96 +

97 +
  it('skips when notifications are disabled', async () => {
98 +
    const user = await createTestUser(db, {
99 +
      pushToken: 'ExponentPushToken[disabled]',
100 +
      notificationsEnabled: false,
101 +
    })
102 +

103 +
    await dailyReminderProcessor({ data: { userId: user.id } } as any)
104 +

105 +
    expect(mockedSend).not.toHaveBeenCalled()
106 +
  })
107 +

108 +
  it('skips when the user has no push token', async () => {
109 +
    const user = await createTestUser(db, { notificationsEnabled: true })
110 +

111 +
    await dailyReminderProcessor({ data: { userId: user.id } } as any)
112 +

113 +
    expect(mockedSend).not.toHaveBeenCalled()
114 +
  })
115 +

116 +
  it('skips when the user does not exist', async () => {
117 +
    await dailyReminderProcessor({ data: { userId: 999999 } } as any)
118 +

119 +
    expect(mockedSend).not.toHaveBeenCalled()
120 +
  })
121 +
})