Skip to content

Commit c52700a

Browse files
committed
Implement tests for sum function to cover various scenarios including empty array, single number, negative numbers, decimal numbers, and non-number values
1 parent 1c07c61 commit c52700a

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

‎Sprint-1/implement/sum.test.js‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,39 @@ const sum = require("./sum.js");
1313
// Given an empty array
1414
// When passed to the sum function
1515
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
16+
test("given an empty array, returns 0", () => {
17+
expect(sum([])).toBe();
18+
});
1719

1820
// Given an array with just one number
1921
// When passed to the sum function
2022
// Then it should return that number
23+
test("given an array with one number, return the number", () => {
24+
expect(sum([7])).toBe(7);
25+
});
2126

2227
// Given an array containing negative numbers
2328
// When passed to the sum function
2429
// Then it should still return the correct total sum
30+
test("given an array with negative numbers, return the correct sum total", () => {
31+
expect(sum([-1, -2, -3])).toBe(-6);
32+
});
2533

2634
// Given an array with decimal/float numbers
2735
// When passed to the sum function
2836
// Then it should return the correct total sum
29-
37+
test("given an array with decimal/float numbers, return the correct sum total", () => {
38+
expect(sum([1.1, 1.2, 2.1])).toBeCloseTo(4.4);
39+
});
3040
// Given an array containing non-number values
3141
// When passed to the sum function
3242
// Then it should ignore the non-numerical values and return the sum of the numerical elements
33-
43+
test("given an array containing non-number values, return correct sum total and ignore the non-numbers", () => {
44+
expect(sum(["we", 2, "are", 18])).toBe(20);
45+
});
3446
// Given an array with only non-number values
3547
// When passed to the sum function
3648
// Then it should return the least surprising value given how it behaves for all other inputs
49+
test("given an array with only non-number values, return the least surprising value", () => {
50+
expect(sum(["the", "world"])).toBe(0);
51+
});

0 commit comments

Comments
 (0)