eletrotupi / tcc/ api/tests/care-actions/list.test.ts85435e34bb5cc0625f3ec5d629a5f44a728632a7
3.9 KB | 136 lines Raw
1
import { describe, it, expect, afterAll, beforeEach } from 'vitest'
2
import request from 'supertest'
3
import { createApp } from '@app/createApp'
4
import { buildPrisma, cleanupTestDb, createTestUser, generateTestToken } from '../test-helper'
5

6
const app = createApp()
7
const db = buildPrisma()
8

9
afterAll(() => db.$disconnect())
10
beforeEach(() => cleanupTestDb(db))
11

12
async function seedActivity(userId: number, overrides: { moment?: Date } = {}) {
13
  return db.careAction.create({
14
    data: {
15
      userId,
16
      type: 'ACTIVITY',
17
      moment: overrides.moment ?? new Date(),
18
      activity: { create: { type: 'WALK' } },
19
    },
20
  })
21
}
22

23
describe('GET /care-actions', () => {
24
  it('returns an empty list when no actions exist', async () => {
25
    const user = await createTestUser(db)
26
    const token = generateTestToken(user.id, user.email)
27

28
    const res = await request(app)
29
      .get('/care-actions')
30
      .set('Authorization', `Bearer ${token}`)
31

32
    expect(res.status).toBe(200)
33
    expect(res.body.entries).toEqual([])
34
    expect(res.body.total).toBe(0)
35
  })
36

37
  it('returns only the authenticated user\'s actions', async () => {
38
    const user = await createTestUser(db)
39
    const other = await createTestUser(db, { email: 'other@example.com' })
40
    const token = generateTestToken(user.id, user.email)
41

42
    await seedActivity(user.id)
43
    await seedActivity(other.id)
44

45
    const res = await request(app)
46
      .get('/care-actions')
47
      .set('Authorization', `Bearer ${token}`)
48

49
    expect(res.status).toBe(200)
50
    expect(res.body.entries).toHaveLength(1)
51
    expect(res.body.entries[0].userId).toBe(user.id)
52
  })
53

54
  it('filters by type', async () => {
55
    const user = await createTestUser(db)
56
    const token = generateTestToken(user.id, user.email)
57

58
    await seedActivity(user.id)
59
    await db.careAction.create({
60
      data: {
61
        userId: user.id,
62
        type: 'APPOINTMENT',
63
        moment: new Date(),
64
        appointment: { create: { type: 'GP', duration: 30 } },
65
      },
66
    })
67

68
    const res = await request(app)
69
      .get('/care-actions?type=ACTIVITY')
70
      .set('Authorization', `Bearer ${token}`)
71

72
    expect(res.status).toBe(200)
73
    expect(res.body.entries).toHaveLength(1)
74
    expect(res.body.entries[0].type).toBe('ACTIVITY')
75
  })
76
})
77

78
describe('GET /care-actions/:id', () => {
79
  it('returns a single care action', async () => {
80
    const user = await createTestUser(db)
81
    const token = generateTestToken(user.id, user.email)
82
    const action = await seedActivity(user.id)
83

84
    const res = await request(app)
85
      .get(`/care-actions/${action.id}`)
86
      .set('Authorization', `Bearer ${token}`)
87

88
    expect(res.status).toBe(200)
89
    expect(res.body.id).toBe(action.id)
90
  })
91
})
92

93
describe('PATCH /care-actions/:id', () => {
94
  it('links a care action to a mood', async () => {
95
    const user = await createTestUser(db)
96
    const token = generateTestToken(user.id, user.email)
97
    const action = await seedActivity(user.id)
98

99
    const mood = await db.mood.create({
100
      data: {
101
        userId: user.id,
102
        selectedMood: 'GOOD',
103
        anxietyLevel: 1,
104
        stressLevel: 1,
105
        energyLevel: 8,
106
        moment: new Date(),
107
        moodComponents: { create: [{ component: 'JOY', intensity: 'LIGHT' }] },
108
      },
109
    })
110

111
    const res = await request(app)
112
      .patch(`/care-actions/${action.id}`)
113
      .set('Authorization', `Bearer ${token}`)
114
      .send({ careAction: { moodId: mood.id } })
115

116
    expect(res.status).toBe(200)
117
    expect(res.body.moodId).toBe(mood.id)
118
  })
119
})
120

121
describe('DELETE /care-actions/:id', () => {
122
  it('deletes a care action', async () => {
123
    const user = await createTestUser(db)
124
    const token = generateTestToken(user.id, user.email)
125
    const action = await seedActivity(user.id)
126

127
    const res = await request(app)
128
      .delete(`/care-actions/${action.id}`)
129
      .set('Authorization', `Bearer ${token}`)
130

131
    expect(res.status).toBe(200)
132

133
    const dbRecord = await db.careAction.findUnique({ where: { id: action.id } })
134
    expect(dbRecord).toBeNull()
135
  })
136
})