|
1 | 1 | import { UtcDateFromStringSchema, UtcDateTimeFromStringSchema } from "@src/utils/date"; |
| 2 | +import { calculateAge } from "@src/utils/date"; |
2 | 3 |
|
3 | 4 | describe("utils-date", () => { |
4 | 5 | describe("UtcDateFromStringSchema", () => { |
@@ -63,4 +64,49 @@ describe("utils-date", () => { |
63 | 64 | }).toThrow(); |
64 | 65 | }); |
65 | 66 | }); |
| 67 | + describe("calculateAge", () => { |
| 68 | + beforeEach(() => { |
| 69 | + jest.useFakeTimers().setSystemTime(new Date("2025-01-15T00:00:00.000Z").getTime()); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should return age based on birthdate", () => { |
| 73 | + const birthdate = "2011-11-01"; |
| 74 | + |
| 75 | + const result = calculateAge(birthdate); |
| 76 | + |
| 77 | + expect(result).toEqual(13); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should return age of previous year if tomorrow is their birthday", () => { |
| 81 | + const birthdate = "1987-01-16"; |
| 82 | + |
| 83 | + const result = calculateAge(birthdate); |
| 84 | + |
| 85 | + expect(result).toEqual(37); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should return updated age if today is their birthday", () => { |
| 89 | + const birthdate = "1987-01-15"; |
| 90 | + |
| 91 | + const result = calculateAge(birthdate); |
| 92 | + |
| 93 | + expect(result).toEqual(38); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should return age of this year if birthday was yesterday", () => { |
| 97 | + const birthdate = "1987-01-14"; |
| 98 | + |
| 99 | + const result = calculateAge(birthdate); |
| 100 | + |
| 101 | + expect(result).toEqual(38); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should throw if birthdate is invalid", () => { |
| 105 | + const birthdate = "1987-13-17"; |
| 106 | + |
| 107 | + expect(() => { |
| 108 | + calculateAge(birthdate); |
| 109 | + }).toThrow(); |
| 110 | + }); |
| 111 | + }); |
66 | 112 | }); |
0 commit comments