Skip to content

Commit 3735e91

Browse files
committed
In till.test.js test cases written and tested.
1 parent bd20fa1 commit 3735e91

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Sprint-2/stretch/till.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const totalTill = require("./till.js");
2+
3+
describe("totalTill()", () => {
4+
test("correctly totals a simple till", () => {
5+
const till = {
6+
"1p": 10, // 10p
7+
"5p": 6, // 30p
8+
"20p": 10, // 200p
9+
"50p": 4, // 200p
10+
};
11+
12+
expect(totalTill(till)).toBe("£4.40");
13+
});
14+
15+
test("ignores invalid coin types", () => {
16+
const till = {
17+
"1p": 2, // 2p
18+
"abc": 10, // ignore
19+
"£1": 5,
20+
};
21+
22+
expect(totalTill(till)).toEqual("£0.02");
23+
});
24+
25+
test("handles empty till", () => {
26+
expect(totalTill({})).toEqual("£0.00");
27+
});
28+
29+
test("throws an error for non-object input", () => {
30+
expect(() => totalTill(null)).toThrow("Input should be an object");
31+
expect(() => totalTill(5)).toThrow("Input should be an object");
32+
expect(() => totalTill("hello")).toThrow("Input should be an object");
33+
expect(() => totalTill([])).toThrow("Input should be an object");
34+
});
35+
36+
test("correct formatting for values under 10p", () => {
37+
const till = { "1p": 3 }; // 3p
38+
expect(totalTill(till)).toEqual("£0.03");
39+
});
40+
41+
test("correct formatting when total is exactly whole pounds", () => {
42+
const till = { "50p": 4, "20p": 5 };
43+
// 50p*4 = 200p, 20p*5 = 100p → total = 300p = £3.00
44+
45+
expect(totalTill(till)).toEqual("£3.00");
46+
});
47+
});
48+
49+
// In till.test.js test cases written and tested.

0 commit comments

Comments
 (0)