forked from bloominstituteoftechnology/node-testing1-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
151 lines (136 loc) · 4.64 KB
/
index.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const utils = require('./index')
describe('[Exercise 1] trimProperties', () => {
test('[1] returns an object with the properties trimmed', () => {
// EXAMPLE
const input = { foo: ' foo ', bar: 'bar ', baz: ' baz' }
const expected = { foo: 'foo', bar: 'bar', baz: 'baz' }
const actual = utils.trimProperties(input)
expect(actual).toEqual(expected)
})
test("[2] returns a copy, leaving the original object intact", () => {
const input = { foo: " foo ", bar: "bar ", baz: " baz" };
utils.trimProperties(input);
expect(input).toEqual({ foo: " foo ", bar: "bar ", baz: " baz" });
});
});
describe('[Exercise 2] trimPropertiesMutation', () => {
test('[3] returns an object with the properties trimmed', () => {
const input = { foo: ' foo ', bar: 'bar ', baz: ' baz' }
const expected = { foo: 'foo', bar: 'bar', baz: 'baz' }
const actual = utils.trimPropertiesMutation(input)
expect(actual).toEqual(expected)
})
test('[4] the object returned is the exact same one we passed in', () => {
const input = { foo:' foo ', bar: 'bar ', baz:'baz' }
const actual = utils.trimPropertiesMutation(input)
expect(actual).toBe(input)
})
})
describe("[Exercise 3] findLargestInteger", () => {
test("[5] returns the largest number in an array of objects { integer: 2 }", () => {
const arr = [{ integer: 11 }, { integer: 5 },{ integer: 10 },{ integer: 9 },];
const largestInt = utils.findLargestInteger(arr);
expect(largestInt).toEqual(11);
});
});
describe('[Exercise 4] Counter', () => {
let counter
beforeEach(() => {
counter = new utils.Counter(3) // each test must start with a fresh couter
})
test('[6] the FIRST CALL of counter.countDown returns the initial count', () => {
expect(counter.countDown()).toBe(3)
})
test('[7] the SECOND CALL of counter.countDown returns the initial count minus one', () => {
counter.countDown()
expect(counter.countDown()).toBe(2)
})
test('[8] the count eventually reaches zero but does not go below zero', () => {
counter.countDown()
counter.countDown()
counter.countDown()
counter.countDown()
expect(counter.countDown()).toBe(0)
})
})
describe('[Exercise 5] Seasons', () => {
let seasons
beforeEach(() => {
seasons = new utils.Seasons() // each test must start with fresh seasons
})
test('[9] the FIRST call of seasons.next returns "summer"', () => {
expect(seasons.next()).toBe('summer')
})
test('[10] the SECOND call of seasons.next returns "fall"', () => {
seasons.next()
expect(seasons.next()).toBe('fall')
})
test('[11] the THIRD call of seasons.next returns "winter"', () => {
seasons.next()
seasons.next()
expect(seasons.next()).toBe('winter')
})
test('[12] the FOURTH call of seasons.next returns "spring"', () => {
seasons.next()
seasons.next()
seasons.next()
expect(seasons.next()).toBe('spring')
})
test('[13] the FIFTH call of seasons.next returns again "summer"', () => {
seasons.next()
seasons.next()
seasons.next()
seasons.next()
expect(seasons.next()).toBe('summer')
})
test('[14] the 40th call of seasons.next returns "spring"', () => {
for (let i = 0; i < 39; i++) {
seasons.next()
}
expect(seasons.next()).toBe('spring')
})
})
describe('[Exercise 6] Car', () => {
let focus
beforeEach(() => {
focus = new utils.Car('focus', 20, 30) // each test must start with a fresh car
})
test('[15] driving the car returns the updated odometer', () => {
expect(focus.drive(100)).toBe(100)
expect(focus.drive(100)).toBe(200)
expect(focus.drive(100)).toBe(300)
expect(focus.drive(200)).toBe(500)
})
test('[16] driving the car uses gas', () => {
focus.drive(600)
expect(focus.drive(1)).toBe(600)
expect(focus.drive(1)).toBe(600)
expect(focus.drive(1)).toBe(600)
expect(focus.tank).toBe(0)
})
test("[17] refueling allows to keep driving", () => {
focus.drive(600);
focus.refuel(10);
focus.drive(600);
expect(focus.odometer).toBe(900);
focus.refuel(20);
focus.drive(600);
expect(focus.odometer).toBe(1500);
});
test("[18] adding fuel to a full tank has no effect", () => {
focus.refuel(2000000);
focus.drive(600);
expect(focus.odometer).toBe(600);
expect(focus.tank).toBe(0);
});
});
describe("[Exercise 7] isEvenNumberAsync", () => {
test("[19] resolves true if passed an even number", async () => {
const even = await utils.isEvenNumberAsync(2);
expect(even).toBe(true);
});
test("[20] resolves false if passed an odd number", async () => {
const odd = await utils.isEvenNumberAsync(5);
expect(odd).toBe(false);
});
});