File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 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.
You can’t perform that action at this time.
0 commit comments