Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

Commit 6f97181

Browse files
authored
Jest-ify tests (#85)
There are also several modifications to tests here: * mandatory/1-fix-functions.js no longer has `sortArray` or `first5` exercises - we moved array methods to week 3, so the tests should move there too. * mandatory/2-function-creation.js has some array-based tests changed to instead test the same function creation but for single values. We moved teaching `map` to week 3, I'll re-add the array versions of these tests to week 3. * extra/1-radio-stations.js changes how we populate the expected stations list, because the tests were previously failing if duplicate stations were generated by the random generator.
1 parent 24339e3 commit 6f97181

File tree

5 files changed

+157
-228
lines changed

5 files changed

+157
-228
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/package-lock.json

extra/1-radio-stations.js

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,26 @@
2626
*/
2727
// `getStations` goes here
2828

29-
/* ======= TESTS - DO NOT MODIFY ======= */
29+
/*
30+
* ======= TESTS - DO NOT MODIFY =======
31+
* Note: You are not expected to understand everything below this comment!
32+
*/
3033

3134
function getAvailableStations() {
3235
// Using `stations` as a property as defining it as a global variable wouldn't
3336
// always make it initialized before the function is called
3437
if (!getAvailableStations.stations) {
3538
const stationCount = 4;
36-
getAvailableStations.stations = new Array(stationCount)
37-
.fill(undefined)
38-
.map(function () {
39-
return Math.floor(Math.random() * (108 - 87 + 1) + 87);
40-
})
41-
.sort(function (frequencyA, frequencyB) {
42-
return frequencyA - frequencyB;
43-
});
39+
getAvailableStations.stations = [];
40+
while (getAvailableStations.stations.length < stationCount) {
41+
let randomFrequency = Math.floor(Math.random() * (108 - 87 + 1) + 87);
42+
if (!getAvailableStations.stations.includes(randomFrequency)) {
43+
getAvailableStations.stations.push(randomFrequency);
44+
}
45+
}
46+
getAvailableStations.stations.sort(function (frequencyA, frequencyB) {
47+
return frequencyA - frequencyB;
48+
});
4449
}
4550

4651
return getAvailableStations.stations;
@@ -50,48 +55,13 @@ function isRadioStation(frequency) {
5055
return getAvailableStations().includes(frequency);
5156
}
5257

53-
const assert = require("assert");
54-
55-
function test(testName, fn) {
56-
try {
57-
fn();
58-
console.log(`\n✅ ${testName}: PASS`);
59-
} catch (error) {
60-
console.log(
61-
`\n❌ ${testName}: FAIL (see details below)\n\n${error.message}`
62-
);
63-
}
64-
}
65-
66-
test("getAllFrequencies() returns all frequencies between 87 and 108", function () {
67-
const frequencies = getAllFrequencies();
68-
assert.deepStrictEqual(frequencies, [
69-
87,
70-
88,
71-
89,
72-
90,
73-
91,
74-
92,
75-
93,
76-
94,
77-
95,
78-
96,
79-
97,
80-
98,
81-
99,
82-
100,
83-
101,
84-
102,
85-
103,
86-
104,
87-
105,
88-
106,
89-
107,
90-
108,
58+
test("getAllFrequencies() returns all frequencies between 87 and 108", () => {
59+
expect(getAllFrequencies()).toEqual([
60+
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
61+
105, 106, 107, 108,
9162
]);
9263
});
9364

9465
test("getStations() returns all the available stations", () => {
95-
const stations = getStations();
96-
assert.deepStrictEqual(stations, getAvailableStations());
66+
expect(getStations()).toEqual(getAvailableStations());
9767
});

mandatory/1-fix-functions.js

Lines changed: 44 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ function greaterThan10(num) {
3030
}
3131
}
3232

33-
function sortArray(letters) {
34-
let sortedLetters = letters;
35-
36-
return sortedLetters;
37-
}
38-
39-
function first5(numbers) {
40-
let sliced;
41-
42-
return sliced;
43-
}
44-
4533
function get3rdIndex(arr) {
4634
let index = 3;
4735
let element;
@@ -51,80 +39,47 @@ function get3rdIndex(arr) {
5139

5240
/* ======= TESTS - DO NOT MODIFY ===== */
5341

54-
const util = require("util");
55-
56-
function test(test_name, actual, expected) {
57-
let status;
58-
59-
let isEqual;
60-
if (Array.isArray(expected)) {
61-
isEqual = arraysEqual(actual, expected);
62-
} else {
63-
isEqual = actual === expected;
64-
}
65-
66-
if (isEqual) {
67-
status = "PASSED";
68-
} else {
69-
status = `FAILED: expected: ${util.inspect(
70-
expected
71-
)} but your function returned: ${util.inspect(actual)}`;
72-
}
73-
74-
console.log(`${test_name}: ${status}`);
75-
}
76-
77-
function arraysEqual(a, b) {
78-
if (a === b) return true;
79-
if (a == null || b == null) return false;
80-
if (a.length != b.length) return false;
81-
82-
for (let i = 0; i < a.length; ++i) {
83-
if (a[i] !== b[i]) return false;
84-
}
85-
86-
return true;
87-
}
88-
89-
test("mood function works for true", mood(true), "I am happy");
90-
test("mood function works for false", mood(false), "I am not happy");
91-
test(
92-
"greaterThanTen function works for 11",
93-
greaterThan10(11),
94-
"num is greater than 10"
95-
);
96-
test(
97-
"greaterThanTen function works for 10",
98-
greaterThan10(10),
99-
"num is not big enough"
100-
);
101-
test(
102-
"greaterThanTen function works for 9",
103-
greaterThan10(9),
104-
"num is not big enough"
105-
);
106-
test("sortArray function works", sortArray(["a", "n", "c", "e", "z", "f"]), [
107-
"a",
108-
"c",
109-
"e",
110-
"f",
111-
"n",
112-
"z",
113-
]);
114-
115-
let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
116-
test("first5 function works", first5(numbers), [1, 2, 3, 4, 5]);
117-
if (!arraysEqual(numbers, [1, 2, 3, 4, 5, 6, 7, 8])) {
118-
console.log("PROBLEM: first5 changed its input array - it shouldn't!");
119-
}
120-
121-
test(
122-
"get3rdIndex function works - case 1",
123-
get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]),
124-
"strawberry"
125-
);
126-
test(
127-
"get3rdIndex function works - case 2",
128-
get3rdIndex([11, 37, 62, 18, 19, 3, 30]),
129-
18
130-
);
42+
test("mood function works for true", () => {
43+
expect(mood(true)).toEqual("I am happy");
44+
});
45+
46+
test("mood function works for false", () => {
47+
expect(mood(false)).toEqual("I am not happy");
48+
});
49+
50+
test("greaterThanTen function works for value greater than 10", () => {
51+
expect(greaterThan10(11)).toEqual("num is greater than 10");
52+
});
53+
54+
test("greaterThanTen function works for value much greater than 10", () => {
55+
expect(greaterThan10(96)).toEqual("num is greater than 10");
56+
});
57+
58+
test("greaterThanTen function works for value less than 10", () => {
59+
expect(greaterThan10(9)).toEqual("num is not big enough");
60+
});
61+
62+
test("greaterThanTen function works for value equal to 10", () => {
63+
expect(greaterThan10(10)).toEqual("num is not big enough");
64+
});
65+
66+
test("get3rdIndex function works with strings", () => {
67+
const strings = ["fruit", "banana", "apple", "strawberry", "raspberry"];
68+
const copyOfOriginal = strings.slice();
69+
expect(get3rdIndex(strings)).toEqual("strawberry");
70+
// Make sure get3rdIndex didn't change its input array.
71+
expect(strings).toEqual(copyOfOriginal);
72+
});
73+
74+
test("get3rdIndex function works with numbers", () => {
75+
const numbers = [11, 37, 62, 18, 19, 3, 30];
76+
const copyOfOriginal = numbers.slice();
77+
expect(get3rdIndex(numbers)).toEqual(18);
78+
// Make sure get3rdIndex didn't change its input array.
79+
expect(numbers).toEqual(copyOfOriginal);
80+
});
81+
82+
test("get3rdIndex returns undefined if not enough elements", () => {
83+
const numbers = [5, 10];
84+
expect(get3rdIndex(numbers)).toBeUndefined();
85+
});

0 commit comments

Comments
 (0)