eletrotupi / tcc/ api/tests/mood.test.js86b807288e7e3138f09761dcc61fdf84ba213b13
2.3 KB | 98 lines Raw
1
import test from "ava";
2
import request from "supertest";
3
import { createApp } from "../src/create-app.js";
4
import { setupTestDb, cleanupTestDb } from "./test-helper.js";
5

6
let app;
7
let db;
8
let userId;
9

10
test.before(() => {
11
  db = setupTestDb();
12
  app = createApp(db);
13
});
14

15
test.beforeEach(async () => {
16
  cleanupTestDb();
17

18
  // Create a user for the mood tests
19
  const res = await request(app)
20
    .post("/users")
21
    .send({
22
      email: "maria@example.com",
23
      password: "123456",
24
      first_name: "Maria",
25
      last_name: "Silva",
26
    });
27

28
  userId = res.body.id;
29
});
30

31
test.serial("Registrar humor", async (t) => {
32
  const res = await request(app).post("/mood").send({
33
    user_id: userId,
34
    stress_level: 3,
35
    anxiety_level: 4,
36
    energy_level: 5,
37
    title: "Bad day",
38
    description: "Stressado com a faculdade",
39
    recorded_at: new Date().toISOString()
40
  });
41

42
  t.is(res.status, 201);
43
  t.truthy(res.body.id);
44
});
45

46
test.serial("Listar humores do usuário", async (t) => {
47
  const res = await request(app).get(`/mood/user/${userId}`);
48
  t.is(res.status, 200);
49
  t.true(Array.isArray(res.body.moods));
50
});
51

52
test.serial("Falha ao registrar humor sem stress_level", async (t) => {
53
  const res = await request(app).post("/mood").send({
54
    user_id: userId,
55
    anxiety_level: 4,
56
    energy_level: 5,
57
    title: "Test",
58
    recorded_at: new Date().toISOString()
59
  });
60

61
  t.is(res.status, 400);
62
  t.is(res.body.error, "Campos faltantes");
63
});
64

65
test.serial("Listar múltiplos humores do usuário", async (t) => {
66
  // Ensure we have a valid userId
67
  t.truthy(userId, "userId should be defined");
68

69
  // Create first mood entry
70
  const res1 = await request(app).post("/mood").send({
71
    user_id: userId,
72
    stress_level: 3,
73
    anxiety_level: 4,
74
    energy_level: 6,
75
    title: "Morning",
76
    description: "Morning mood",
77
    recorded_at: new Date().toISOString()
78
  });
79
  t.is(res1.status, 201);
80

81
  // Create second mood entry
82
  const res2 = await request(app).post("/mood").send({
83
    user_id: userId,
84
    stress_level: 2,
85
    anxiety_level: 2,
86
    energy_level: 8,
87
    title: "Evening",
88
    description: "Evening mood",
89
    recorded_at: new Date().toISOString()
90
  });
91
  t.is(res2.status, 201);
92

93
  const res = await request(app).get(`/mood/user/${userId}`);
94
  t.is(res.status, 200);
95
  t.true(Array.isArray(res.body.moods));
96
  t.is(res.body.moods.length, 2);
97
});
98