This repository was archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathusers.test.ts
More file actions
91 lines (82 loc) · 3.49 KB
/
users.test.ts
File metadata and controls
91 lines (82 loc) · 3.49 KB
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
import { useTestDatabase } from '../config/index';
const {
listUsers,
getUser,
addUser,
modifyUser,
deleteUser,
} = require('../../controllers/users');
useTestDatabase();
const newUser = {
username: 'anewuser',
email: 'anewuser@domain.com',
first_name: 'Jane',
last_name: 'Doe',
password: 'theplainpasswordgoeshere',
author_id: 2,
};
describe('Test if Users CRUD operations are working correctly', () => {
test('Listing all Users should return rows', async () => {
expect.assertions(1);
const users = await listUsers();
expect(users.length).toBeGreaterThan(0);
});
test('Fetching a single User should return an User', async () => {
expect.assertions(7);
const user = await getUser(1);
expect(user.id).toBe(1);
expect(typeof user.username).toBe('string');
expect(typeof user.email).toBe('string');
expect(typeof user.first_name).toBe('string');
expect(typeof user.last_name).toBe('string');
expect(typeof user.password).toBe('string');
expect(typeof user.author_id).toBe('number');
});
test('Adding a new User should add a single row', async () => {
expect.assertions(1);
const usersBefore = await listUsers();
const userLengthBefore = usersBefore.length;
return addUser(newUser).then(async () => {
const usersAfter = await listUsers();
const userLengthAfter = usersAfter.length;
expect(userLengthAfter).toBe(userLengthBefore + 1);
});
});
test('Adding a new User should return the new User', async () => {
expect.assertions(7);
const addedUser = await addUser(newUser);
expect(typeof addedUser.id).toBe('number');
expect(addedUser.username).toBe(newUser.username);
expect(addedUser.email).toBe(newUser.email);
expect(addedUser.first_name).toBe(newUser.first_name);
expect(addedUser.last_name).toBe(newUser.last_name);
expect(addedUser.password).not.toBe(newUser.password); // Hashed pass
expect(addedUser.author_id).toBe(newUser.author_id);
});
test('Updating an User should return the modified data', async () => {
expect.assertions(15);
const originalUser = await getUser(1);
expect(originalUser.id).toBe(1);
expect(originalUser.username).not.toBe(newUser.username);
expect(originalUser.email).not.toBe(newUser.email);
expect(originalUser.first_name).not.toBe(newUser.first_name);
expect(originalUser.last_name).not.toBe(newUser.last_name);
expect(originalUser.password).not.toBe(newUser.password);
expect(originalUser.author_id).not.toBe(newUser.author_id);
const modifiedUser = await modifyUser(1, newUser);
expect(modifiedUser.id).toBeDefined();
expect(typeof modifiedUser.id).toBe('number');
expect(modifiedUser.username).toBe(newUser.username);
expect(modifiedUser.email).toBe(newUser.email);
expect(modifiedUser.first_name).toBe(newUser.first_name);
expect(modifiedUser.last_name).toBe(newUser.last_name);
expect(modifiedUser.password).toBe(newUser.password);
expect(modifiedUser.author_id).toBe(newUser.author_id);
});
test('Deleting an User should return undefined', async () => {
expect.assertions(2);
return deleteUser(2)
.then(data => expect(data.id).toBe(2))
.then(async () => expect(await getUser(2)).toBeUndefined());
});
});