-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.test.js
More file actions
101 lines (81 loc) · 3.05 KB
/
functions.test.js
File metadata and controls
101 lines (81 loc) · 3.05 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
92
93
94
95
96
97
98
99
100
101
const functions = require('./functions');
// beforeEach(() => initDatabase());
// afterEach(() => closedDatabase());
beforeAll(() => initDatabase());
afterAll(() => closedDatabase());
// // Dommy function. Initialize a database before each function in it and clear after each function
const initDatabase = () => console.log('Database Initialized...');
const closedDatabase = () => console.log('Database Closed...');
// it will be run before the code block
const nameCheck = () => console.log('Checking Name...functions....');
describe('Checking Names', () => {
beforeEach(() => nameCheck());
test('User is Olamphzy', () => {
const user = 'Olamphzy'
expect(user).toBe('Olamphzy');
});
test('User is Pelumi', () => {
const user = 'Pelumi'
expect(user).toBe('Pelumi');
});
});
// toBe
it('Adds 2 + 2 to equal 4', () => { //expect(function testing for.matcher)
expect(functions.add(2, 2)).toBe(4) //If its fails it gives reason for such failure in the terminal
})
// not.toBe
test('Add 2 + 2 not equal 5', () => {
expect(functions.add(2, 2)).not.toBe(5);
})
// Falsy values in Js are 0, null, undefined
// CHECK TRINITY & FALSY VALUES
// toBeNull matches only null
// toBeUndefined matches only undefined
// toBeDefined is the opposite of toBeUndefined
// toBetruthy matches anything that an if statement treats as true
// toBeFalsy matches anything that an if statement treats as false
// toBeNull
test('Should be null', () => {
expect(functions.isNull()).toBeNull();
});
// toBeFalsy
test('Should be falsy', () => {
expect(functions.checkValue(null)).toBeFalsy(); //it passes all falsy values 0, null & undefined
});
// toEqual
test('User should be Olanrewaju Rasheed object', () => {
expect(functions.createUser()).toEqual({ //object in toEqual will compare objecct in arrays
firstName: 'Olanrewaju',
lastName: 'Rasheed'
});
});
// less than and greater than
test('Shouldbe under 1700', () => {
const load1 = 900;
const load2 = 800;
expect(load1 + load2).toBeLessThanOrEqual(1700);
})
// Regular expression
test('There is no k in visual', () => {
expect('visual').not.toMatch(/K/); //it doesn't matter is its lower case
})
// Arrays
test('Iffe should be in usernames', () => {
usernames = ['pelumi', 'iffe', 'aisha', 'rasaq'];
expect(usernames).toContain('iffe')
})
// //Working with async data
// Promise
// test('User fetched name should be Leanne Graham', () => {
// expect.assertions(1); //dealing with async we assertion with number of assertions; this case 1.
// return functions.fetchUser()
// .then(data => {
// expect(data.name).toEqual('Leanne Graham')
// })
// })
// Async Await
test('User fetched name should be Leanne Graham', async () => {
expect.assertions(1); //dealing with async we assertion with number of assertions; this case 1.
const data = await functions.fetchUser();
expect(data.name).toEqual('Leanne Graham')
})