-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathtally.test.js
More file actions
82 lines (70 loc) · 2.36 KB
/
tally.test.js
File metadata and controls
82 lines (70 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const tally = require("./tally.js");
/**
* tally array
*
* In this task, you'll need to implement a function called tally
* that will take a list of items and count the frequency of each item
* in an array
*
* For example:
*
* tally(['a']), target output: { a: 1 }
* tally(['a', 'a', 'a']), target output: { a: 3 }
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
*/
// Acceptance criteria:
// Given a function called tally
// When passed an array of items
// Then it should return an object containing the count for each unique item
// Given an empty array
// When passed to tally
// Then it should return an empty object
test("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});
// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("tally on an array with duplicates returns correct counts for each unique item", () => {
expect(
tally([
"CYF",
"CYF",
"AWS",
"Capgemini",
"Deloitte",
"Google",
"Slack",
"Capgemini",
])
).toEqual({ CYF: 2, AWS: 1, Capgemini: 2, Deloitte: 1, Google: 1, Slack: 1 });
});
test("tally on an array with duplicates returns correct counts for each unique item", () => {
expect(tally(["toString", "toString"])).toEqual({ toString: 2 });
});
// Given an input that you mentioned in the review.
//test("tally on an array with duplicates returns correct counts for each unique item", () => {
// expect(tally(["toString", "toString"])).toEqual({ toString: 2 });
//});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("given invalid input throws an error", () => {
expect(() => tally("invalid")).toThrow("invalid input");
});
// Given an invalid input like a number
test("given invalid input throws an error", () => {
expect(() => tally(123)).toThrow("invalid input");
});
// Given an invalid input like an object
test("given invalid input throws an error", () => {
expect(() => tally({})).toThrow("invalid input");
});
// Given an invalid input like null
test("given invalid input throws an error", () => {
expect(() => tally(null)).toThrow("invalid input");
});
// Given an invalid input like undefined
test("given invalid input throws an error", () => {
expect(() => tally(undefined)).toThrow("invalid input");
});