Skip to content

Commit 60864d0

Browse files
committed
Refactor tests in getOrdinalNumber to cover additional cases and improve error handling
1 parent 0e92f62 commit 60864d0

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed
Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,55 @@
11
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber
32

4-
// continue testing and implementing getOrdinalNumber for additional cases
5-
// Write your tests using Jest - remember to run your tests often for continual feedback
3+
// Category 1: Numbers ending in 11, 12, 13 → always "th"
4+
test("appends 'th' to numbers ending in 11, 12, or 13", () => {
5+
const numbers = [11, 12, 13, 111, 1012, 1313];
6+
numbers.forEach(num => {
7+
expect(getOrdinalNumber(num)).toEqual(`${num}th`);
8+
});
9+
});
10+
11+
// Category 2: Numbers ending in 1 → "st" (except those ending in 11)
12+
test("appends 'st' to numbers ending in 1 except those ending in 11", () => {
13+
const numbers = [1, 21, 101, 1001];
14+
numbers.forEach(num => {
15+
expect(getOrdinalNumber(num)).toEqual(`${num}st`);
16+
});
17+
});
618

7-
// Case 1: Identify the ordinal number for 1
8-
// When the number is 1,
9-
// Then the function should return "1st"
19+
// Category 3: Numbers ending in 2 → "nd" (except those ending in 12)
20+
test("appends 'nd' to numbers ending in 2 except those ending in 12", () => {
21+
const numbers = [2, 22, 102, 202];
22+
numbers.forEach(num => {
23+
expect(getOrdinalNumber(num)).toEqual(`${num}nd`);
24+
});
25+
});
26+
27+
// Category 4: Numbers ending in 3 → "rd" (except those ending in 13)
28+
test("appends 'rd' to numbers ending in 3 except those ending in 13", () => {
29+
const numbers = [3, 23, 103, 1003];
30+
numbers.forEach(num => {
31+
expect(getOrdinalNumber(num)).toEqual(`${num}rd`);
32+
});
33+
});
34+
35+
// Category 5: All other numbers → "th"
36+
test("appends 'th' to numbers that don't meet special suffix rules", () => {
37+
const numbers = [4, 5, 6, 9, 20, 100, 1004, 1009];
38+
numbers.forEach(num => {
39+
expect(getOrdinalNumber(num)).toEqual(`${num}th`);
40+
});
41+
});
42+
43+
// Error handling - Non-integers
44+
test("throws an error when number is not an integer", () => {
45+
const invalidInputs = [1.5, "3", null, undefined, {}, [], NaN];
46+
47+
invalidInputs.forEach(input => {
48+
expect(() => getOrdinalNumber(input)).toThrow("Input must be an integer");
49+
});
50+
});
1051

11-
test("should return '1st' for 1", () => {
12-
expect(getOrdinalNumber(1)).toEqual("1st");
52+
// Error handling - Negative values
53+
test("throws an error when number is negative", () => {
54+
expect(() => getOrdinalNumber(-1)).toThrow("Input must be a non-negative integer");
1355
});

0 commit comments

Comments
 (0)