Skip to content

Commit 1874dec

Browse files
committed
Refactor tally tests to implement missing test case for empty array and organize test structure for clarity
1 parent 63973cd commit 1874dec

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

‎Sprint-2/implement/tally.test.js‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,29 @@ const tally = require("./tally.js");
2323
// Given an empty array
2424
// When passed to tally
2525
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
2726

28-
// Given an array with duplicate items
29-
// When passed to tally
30-
// Then it should return counts for each unique item
27+
describe("tally()", () => {
28+
test("tally on an empty array returns an empty object", () => {
29+
expect(tally([])).toEqual({});
30+
});
3131

32-
// Given an invalid input like a string
33-
// When passed to tally
34-
// Then it should throw an error
32+
// Given an array with duplicate items
33+
// When passed to tally
34+
// Then it should return counts for each unique item
35+
36+
test("returns counts for single and duplicate items", () => {
37+
expect(tally(["a"])).toEqual({ a: 1 });
38+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
39+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
40+
});
41+
42+
// Given an invalid input like a string
43+
// When passed to tally
44+
// Then it should throw an error
45+
46+
test("throws an error when passed invalid input like a string or null", () => {
47+
expect(() => tally("a string")).toThrow(TypeError);
48+
expect(() => tally(123)).toThrow(TypeError);
49+
expect(() => tally(null)).toThrow(TypeError);
50+
});
51+
});

0 commit comments

Comments
 (0)