Skip to content

Commit 6993bb1

Browse files
committed
Modified the test into groups
1 parent 4b42c9f commit 6993bb1

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,57 @@ const getOrdinalNumber = require("./get-ordinal-number");
88
// When the number is 1,
99
// Then the function should return "1st"
1010

11-
// Case 1: 1 → Standard ordinals
12-
13-
test("should return correct ordinals for 1, 2, 3, 4, 21, 22, 23, 24", () => {
14-
expect(getOrdinalNumber(1)).toEqual("1st");
15-
expect(getOrdinalNumber(2)).toEqual("2nd");
16-
expect(getOrdinalNumber(3)).toEqual("3rd");
17-
expect(getOrdinalNumber(4)).toEqual("4th");
18-
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(22)).toEqual("22nd");
20-
expect(getOrdinalNumber(23)).toEqual("23rd");
21-
expect(getOrdinalNumber(24)).toEqual("24th");
11+
// Case 1: Numbers ending in 1 except 11
12+
13+
test("should return 'st' for numbers ending in 1 except 11", () => {
14+
const values = [1, 21, 31, 101, 1001];
15+
values.forEach(num => {
16+
expect(getOrdinalNumber(num)).toEqual(`${num}st`);
17+
});
2218
});
2319

24-
// Case 2: Exceptions 11, 12, 13
25-
test("should handle exceptions 11, 12, 13 correctly", () => {
26-
expect(getOrdinalNumber(11)).toEqual("11th");
27-
expect(getOrdinalNumber(12)).toEqual("12th");
28-
expect(getOrdinalNumber(13)).toEqual("13th");
29-
expect(getOrdinalNumber(111)).toEqual("111th");
20+
// Case 2: Numbers ending in 2 except 12
21+
22+
test("should return 'nd' for numbers ending in 2 except 12", () => {
23+
const values = [2, 22, 32, 102, 1002];
24+
values.forEach(num => {
25+
expect(getOrdinalNumber(num)).toEqual(`${num}nd`);
26+
});
27+
});
28+
29+
// Case 3: Numbers ending in 3 except 13
30+
31+
test("should return 'rd' for numbers ending in 3 except 13", () => {
32+
const values = [3, 23, 33, 103, 1003];
33+
values.forEach(num => {
34+
expect(getOrdinalNumber(num)).toEqual(`${num}rd`);
35+
});
3036
});
3137

32-
// Case 3: Large numbers
33-
test("should handle larger numbers correctly", () => {
34-
expect(getOrdinalNumber(101)).toEqual("101st");
35-
expect(getOrdinalNumber(112)).toEqual("112th");
36-
expect(getOrdinalNumber(123)).toEqual("123rd");
38+
// Case 4: Exceptions 11, 12 & 13
39+
40+
test("should return 'th' for 11, 12, 13 regardless of other digits", () => {
41+
const values = [11, 12, 13, 111, 212, 513];
42+
values.forEach(num => {
43+
expect(getOrdinalNumber(num)).toEqual(`${num}th`);
44+
});
45+
});
46+
47+
// Case 5: Numbers ending in 0 and (4-9)
48+
49+
test("should return 'th' for numbers ending in 0 or 4–9", () => {
50+
const values = [4, 5, 6, 7, 8, 9, 10, 20, 25, 100, 104, 1009];
51+
values.forEach(num => {
52+
expect(getOrdinalNumber(num)).toEqual(`${num}th`);
53+
});
3754
});
3855

39-
// Case 4: Invalid cases / inputs
56+
// Case 6: Invalid cases / inputs
4057
test("should throw error for zero, negative or non-integer inputs", () => {
4158
expect(() => getOrdinalNumber(0)).toThrow("Only positive integers are allowed");
4259
expect(() => getOrdinalNumber(-5)).toThrow("Only positive integers are allowed");
4360
expect(() => getOrdinalNumber(2.5)).toThrow("Only positive integers are allowed");
4461
});
4562

46-
// Added possible and invalid cases and tested using npx jest
63+
// Added possible and invalid cases and tested using npx jest
64+
// Modified the test into groups

0 commit comments

Comments
 (0)