-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathpromise.test.js
More file actions
80 lines (68 loc) · 2.19 KB
/
Copy pathpromise.test.js
File metadata and controls
80 lines (68 loc) · 2.19 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
describe('DAY 9: Promises', () => {
it(`validString function should return a Promise
the promise should:
resolve with the input value if it's a string
reject with an instance of MySampleTypeError if it's not
the error message should be a "Template literal" (see the catch block for a hint)`, () => {
/**
*
* @param {*} input
* @returns {Promise}
*/
function validateString (input) {
return new Promise((resolve, reject) => {
});
}
let input1 = `I'm a a string`;
let input2 = 5;
/**
* this test is not quite good
* we might have false positives
* ? can you tell why?
*/
return validateString(input1).then(resolution => {
expect(resolution).toBe(`I'm a a string`);
}).then(() => {
return validateString(input2);
}).catch((error) => {
expect(error).toBeInstanceOf(TypeError);
expect(error.message).toBe(`${input2} is not a string`);
});
});
it(`validString function should exactly as the previous test
stringToUpper function should return a Promise
and will try to resolve the string to uppercase
or catch the error and reject it
`, () => {
/**
*
* @param {*} input
* @returns {Promise}
*/
function validateString (input) {
return new Promise((resolve, reject) => {
resolve(input);
});
}
/**
*
* @param {*} input
* @returns {Promise}
*/
function stringToUpper (input) {
return new Promise((resolve, reject) => {
resolve('change me');
});
}
let input = 'oo';
/**
* this tests has a redundant validation
* ? can you tell why is that?
*/
return validateString(input).then(resolution => {
return stringToUpper(resolution);
}).then(resolution => {
expect(resolution).toBe(input.toUpperCase());
});
});
});