Skip to content

Commit 483e8fd

Browse files
committed
Refactor test for readbility
1 parent e8a096f commit 483e8fd

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed
Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,64 @@
1-
// implement a function countChar that counts the number of times a character occurs in a string
21
const countChar = require("./count");
32

43
// Scenario 1: Multiple Occurrences
5-
test("should count multiple occurrences of a character", () => {
6-
expect(countChar("aaaaa", "a")).toEqual(5);
7-
expect(countChar("sea saw", "s")).toEqual(2);
8-
expect(countChar("Arrested Development", "d")).toEqual(2);
9-
expect(countChar("* Star * TV *", "*")).toEqual(3);
4+
test("Should count multiple occurrences of a character", () => {
5+
const multipleOccurrences = [
6+
{ a: "aaaaa", b: "a", c: 5 },
7+
{ a: "sea saw", b: "s", c: 2 },
8+
{ a: "Arrested Development", b: "d", c: 2 },
9+
{ a: "* Star * TV *", b: "*", c: 3 },
10+
];
11+
12+
for (const { a, b, c } of multipleOccurrences) {
13+
expect(countChar(a, b)).toEqual(c);
14+
}
1015
});
1116

1217
// Scenario 2: Single Occurrence
13-
test("should count single occurrence of a character", () => {
14-
expect(countChar("a", "a")).toEqual(1);
15-
expect(countChar("Star Light", "i")).toEqual(1);
16-
expect(countChar("!@#$%^&*()", "#")).toEqual(1);
17-
expect(countChar("onomatopoeia", "t")).toEqual(1);
18+
test("Should count single occurrence of a character", () => {
19+
const singleOccurrences = [
20+
{ a: "a", b: "a" },
21+
{ a: "Star Light", b: "i" },
22+
{ a: "!@#$%^&*()", b: "#" },
23+
{ a: "Why fly?", b: " " },
24+
];
25+
26+
for (const { a, b } of singleOccurrences) {
27+
expect(countChar(a, b)).toEqual(1);
28+
}
1829
});
1930

2031
// Scenario 3: No Occurrence
21-
test("should count single occurrence of a character", () => {
22-
expect(countChar("The Rookie", "S")).toEqual(0);
23-
expect(countChar("Angela Bassett", "r")).toEqual(0);
24-
expect(countChar("Angela Bassett", "r")).toEqual(0);
25-
expect(countChar("Better Off Ted", "9")).toEqual(0);
32+
test("Should return 0 for no occurrence of a character", () => {
33+
const noOccurrences = [
34+
{ a: "The Rookie", b: "S" },
35+
{ a: "Angela Bassett", b: "r" },
36+
{ a: "Santa Clarita Diet", b: "*" },
37+
{ a: "Better Off Ted", b: "9" },
38+
];
39+
40+
for (const { a, b } of noOccurrences) {
41+
expect(countChar(a, b)).toEqual(0);
42+
}
2643
});
2744

2845
// Scenario 4: Invalid Entries
29-
const invalidEntries = [
30-
{ a: "Courage, the Cowardly Dog", b: "dog" },
31-
{ a: "Sheep in the Big City", b: 1 },
32-
{ a: -205, b: "a" },
33-
{ a: -205, b: 55.5 },
34-
{ a: true, b: "f" },
35-
{ a: undefined, b: "f" },
36-
{ a: null, b: true },
37-
{ a: false, b: true },
38-
{ a: [false], b: {} },
39-
];
40-
41-
describe.each(invalidEntries)("countChar($a, $b)", ({ a, b }) => {
42-
test(`Should throw an error when given an invalid input (${a}, ${b})`, () => {
46+
test("Should throw an error when given an invalid input", () => {
47+
const invalidEntries = [
48+
{ a: "Courage, the Cowardly Dog", b: "dog" },
49+
{ a: "Sheep in the Big City", b: 1 },
50+
{ a: -205, b: "a" },
51+
{ a: -205, b: 55.5 },
52+
{ a: true, b: "f" },
53+
{ a: undefined, b: "f" },
54+
{ a: null, b: true },
55+
{ a: false, b: true },
56+
{ a: [false], b: {} },
57+
];
58+
59+
for (const { a, b } of invalidEntries) {
4360
expect(() => {
4461
countChar(a, b);
4562
}).toThrow();
46-
});
63+
}
4764
});

0 commit comments

Comments
 (0)