@@ -15,20 +15,41 @@ const tally = require("./tally.js");
1515 */
1616
1717// Acceptance criteria:
18+ describe ( "tally" , ( ) => {
19+ // Given a function called tally
20+ // When passed an array of items
21+ // Then it should return an object containing the count for each unique item
22+ test ( "returns correct counts for mixed data types" , ( ) => {
23+ const input = [ 1 , "a" , 2 , true , 1 , "a" , 2 , true ] ;
24+ const expectedOutput = { 1 : 2 , a : 2 , 2 : 2 , true : 2 } ;
25+ expect ( tally ( input ) ) . toEqual ( expectedOutput ) ;
26+ } ) ;
27+ // Given an empty array
28+ // When passed to tally
29+ // Then it should return an empty object
30+ test ( "tally on an empty array returns an empty object" , ( ) => {
31+ expect ( tally ( [ ] ) ) . toEqual ( { } ) ;
32+ } ) ;
33+ // Given an array with duplicate items
34+ // When passed to tally
35+ // Then it should return counts for each unique item
36+ test ( "returns correct counts for an array with duplicates" , ( ) => {
37+ const input = [ "banana" , "apple" , "banana" , "cherry" , "apple" , "banana" ] ;
38+ const expectedOutput = {
39+ banana : 3 ,
40+ apple : 2 ,
41+ cherry : 1 ,
42+ } ;
43+ expect ( tally ( input ) ) . toEqual ( expectedOutput ) ;
44+ } ) ;
45+ // Given an invalid input like a string
46+ // When passed to tally
47+ // Then it should throw an error
1848
19- // Given a function called tally
20- // When passed an array of items
21- // Then it should return an object containing the count for each unique item
22-
23- // Given an empty array
24- // When passed to tally
25- // Then it should return an empty object
26- test . todo ( "tally on an empty array returns an empty object" ) ;
27-
28- // Given an array with duplicate items
29- // When passed to tally
30- // Then it should return counts for each unique item
31-
32- // Given an invalid input like a string
33- // When passed to tally
34- // Then it should throw an error
49+ test . each ( [ "not an array" , 123 , null , undefined , { key : "value" } ] ) (
50+ "throws an error for invalid input type: %p" ,
51+ ( invalidInput ) => {
52+ expect ( ( ) => tally ( invalidInput ) ) . toThrow ( "Input must be an array." ) ;
53+ }
54+ ) ;
55+ } ) ;
0 commit comments