-
Notifications
You must be signed in to change notification settings - Fork 0
/
addTask.test.offline.ts
94 lines (82 loc) · 2.4 KB
/
addTask.test.offline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { expect, test } from 'vitest';
import * as taskRepository from '@app/database/taskRepository';
import axios from 'axios';
const url = 'http://localhost:3000/task';
const principalId = 'test-access';
test.only('should return one created item', async () => {
expect(await taskRepository.getAllTasks(principalId)).toHaveLength(0);
const data = { description: 'new task' };
const response = await axios({
method: 'post',
url,
headers: { Authorization: `token` },
data: data,
validateStatus: () => true,
});
expect(response.status).toEqual(201);
expect(response.data).toHaveProperty('result', 'success');
expect(response.data).toHaveProperty('message', 'Task created successfully');
expect(response.data).toHaveProperty('data.type', 'Task');
expect(response.data).toHaveProperty(
'data.attributes.description',
'new task',
);
expect(response.data).toHaveProperty('data.id');
expect(response.data).toHaveProperty('data.created');
expect(await taskRepository.getAllTasks(principalId)).toHaveLength(1);
});
test('should fail on bad token', async () => {
const data = { description: 'val' };
const response = await axios({
method: 'post',
url,
headers: { Authorization: `bad-token` },
data: data,
validateStatus: () => true,
});
expect(response.data).toEqual({
error: 'Forbidden',
message: 'User is not authorized to access this resource',
statusCode: 403,
});
});
test('should fail with missing description', async () => {
const data = {};
const response = await axios({
method: 'post',
url,
headers: { Authorization: `token` },
data: data,
validateStatus: () => true,
});
expect(response.status).toEqual(422);
expect(response.data).toEqual([
{
code: 'invalid_type',
expected: 'string',
message: 'Required',
path: ['description'],
received: 'undefined',
},
]);
});
test('should fail with description as number', async () => {
const data = { description: 10 };
const response = await axios({
method: 'post',
url,
headers: { Authorization: `token` },
data: data,
validateStatus: () => true,
});
expect(response.status).toEqual(422);
expect(response.data).toEqual([
{
code: 'invalid_type',
expected: 'string',
message: 'Expected string, received number',
path: ['description'],
received: 'number',
},
]);
});