eletrotupi / tcc/ api/tests/mood.test.jsba9ebeebe13950cdc2b168d63fe2ec5b3be67d11
2.0 KB | 90 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
    title: "Bad day",
37
    description: "Stressado com a faculdade",
38
  });
39

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

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

50
test.serial("Falha ao registrar humor sem stress_level", async (t) => {
51
  const res = await request(app).post("/mood").send({
52
    user_id: userId,
53
    anxiety_level: 4,
54
    title: "Test",
55
  });
56

57
  t.is(res.status, 400);
58
  t.is(res.body.error, "Campos faltantes");
59
});
60

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

65
  // Create first mood entry
66
  const res1 = await request(app).post("/mood").send({
67
    user_id: userId,
68
    stress_level: 3,
69
    anxiety_level: 4,
70
    title: "Morning",
71
    description: "Morning mood",
72
  });
73
  t.is(res1.status, 201);
74

75
  // Create second mood entry
76
  const res2 = await request(app).post("/mood").send({
77
    user_id: userId,
78
    stress_level: 2,
79
    anxiety_level: 2,
80
    title: "Evening",
81
    description: "Evening mood",
82
  });
83
  t.is(res2.status, 201);
84

85
  const res = await request(app).get(`/mood/${userId}`);
86
  t.is(res.status, 200);
87
  t.true(Array.isArray(res.body));
88
  t.is(res.body.length, 2);
89
});
90