-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathsum.test.js
More file actions
45 lines (39 loc) · 1.4 KB
/
sum.test.js
File metadata and controls
45 lines (39 loc) · 1.4 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
const sum = require("./sum.js");
describe("sum", () => {
// Given an empty array
// When passed to the sum function
// Then it should return 0
test("given an empty array, returns 0", () => {
expect(sum([])).toBe(0);
});
// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("given an array with one number, returns that number", () => {
expect(sum([5])).toBe(5);
});
// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("sums negative numbers correctly", () => {
expect(sum([-5, -10, 15])).toBe(0);
});
// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("sums decimal numbers correctly", () => {
expect(sum([1.5, 2.5, 3.5])).toBe(7.5);
});
// Given an array containing non-number values
// When passed to the sum function
// Then it should ignore the non-numerical values
test("ignores non-number values", () => {
expect(sum(["hey", 10, "hi", 60, 10])).toBe(80);
});
// Given an array with only non-number values
// When passed to the sum function
// Then it should return the least surprising value
test("returns 0 when array contains only non-number values", () => {
expect(sum(["a", "b", "c"])).toBe(0);
});
});