-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitTesting.test.js
61 lines (52 loc) · 2.17 KB
/
UnitTesting.test.js
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
const userNameSum = require('./utils/GetUserNameSum');
const reverseUserName = require('./utils/GetReversedUserName');
const paramsValidator = require('./utils/paramsValidator')
const CustomError = require('./utils/CustomErrorClass')
const pickARandomOption = require('./utils/GetRandomOption');
const successfulCalls = require('./utils/GetSuccessfulCallsDistribution');
const express = require('express');
const app = express();
const userNameObj =new userNameSum();
const reverseNameObj = new reverseUserName();
test("Returns 121 for Sun Tzu", () => {
(userNameObj.surprise('Sun Tzu'.toUpperCase())).then((result) => {
expect(result).toBe(121);
})
});
test("Returns nuyaH_lavuY for Yuval_Hayun", () => {
(reverseNameObj.surprise('Yuval_Hayun')).then((result) => {
expect(result).toBe('nuyaH_lavuY');
})
});
test("Throw an error for missing name", () => {
expect(() => {
paramsValidator('' , 2021);
}).toThrow(CustomError);
});
test("Throw an error for wrong birth_year format", () => {
expectedMessage = 'Please enter a valid Integer birth year';
expect(() => {
paramsValidator('user_name' , 21.1);
}).toThrow(expectedMessage);
});
test("Throw an error for wrong name format", () => {
expect(() => { paramsValidator('12_name' , 2011); }).toThrow(CustomError);
});
test("Returns name-sum as the random option for the giveen request", () => {
let url = '/api/surprise?name=Ali_Baba&birth_year=2005&upsidedown=false';
app.get(url, (req, res) =>
expect(pickARandomOption(req)).toBe('name-sum'));
});
test("Returns kanye-quote as the random option for the giveen request", () => {
let url = '/api/surprise?name=Qu_Qu&birth_year=2003&upsidedown=false';
app.get(url, (req, res) =>
expect(pickARandomOption(req)).toBe('kanye-quote'));
});
test("Returns an array without the zero occurrences requests", () => {
requestsHistory = [{type: 'chuck-norris-joke', count: 0},
{type: 'kanye-quote', count: 5},
{type: 'name-sum', count: 0},
{type: 'reversed-name', count: 0}];
expectedResult = [{type: 'kanye-quote', count: 5}];
expect(successfulCalls(requestsHistory)).toStrictEqual(expectedResult);
});