|
| 1 | +import { getNextDigestAt, getNotificationStartDate } from "./helpers" |
| 2 | +import { Timestamp } from "../firebase" |
| 3 | +import { Frequency } from "../auth/types" |
| 4 | + |
| 5 | +describe("getNextDigestAt", () => { |
| 6 | + beforeEach(() => { |
| 7 | + jest.useFakeTimers() |
| 8 | + }) |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + jest.useRealTimers() |
| 12 | + }) |
| 13 | + |
| 14 | + it("should return the next Tuesday for weekly frequency", () => { |
| 15 | + // Set a fixed date: Monday, March 3, 2025 |
| 16 | + const fixedDate = new Date(2025, 2, 3) |
| 17 | + |
| 18 | + // Next Tuesday, March 4, 2025 |
| 19 | + const expectedDate = new Date(2025, 2, 4) |
| 20 | + |
| 21 | + jest.setSystemTime(fixedDate) |
| 22 | + |
| 23 | + const result = getNextDigestAt("Weekly") |
| 24 | + expect(result).toEqual(Timestamp.fromDate(expectedDate)) |
| 25 | + }) |
| 26 | + |
| 27 | + // Extra test covering this case because getting this |
| 28 | + // wrong could cause an expensive infinite function loop |
| 29 | + it("should return the next Tuesday for weekly frequency when the current day is Tuesday", () => { |
| 30 | + // Set a fixed date: Tuesday, March 4, 2025 |
| 31 | + const fixedDate = new Date(2025, 2, 4) |
| 32 | + |
| 33 | + // Next Tuesday, March 11, 2025 |
| 34 | + const expectedDate = new Date(2025, 2, 11) |
| 35 | + |
| 36 | + jest.setSystemTime(fixedDate) |
| 37 | + |
| 38 | + const result = getNextDigestAt("Weekly") |
| 39 | + expect(result).toEqual(Timestamp.fromDate(expectedDate)) |
| 40 | + }) |
| 41 | + |
| 42 | + it("should return the next 1st of the month for monthly frequency", () => { |
| 43 | + // Set a fixed date: March 15, 2025 |
| 44 | + const fixedDate = new Date(2025, 2, 15) |
| 45 | + // Next 1st of the month, April 1, 2025 |
| 46 | + const expectedDate = new Date(2025, 3, 1) |
| 47 | + |
| 48 | + jest.setSystemTime(fixedDate) |
| 49 | + |
| 50 | + const result = getNextDigestAt("Monthly") |
| 51 | + expect(result).toEqual(Timestamp.fromDate(expectedDate)) |
| 52 | + }) |
| 53 | + |
| 54 | + // Extra test covering this case because getting this |
| 55 | + // wrong could cause an expensive infinite function loop |
| 56 | + it("should return the next 1st of the month for monthly frequency when it is the 1st of the month", () => { |
| 57 | + // Set a fixed date: March 1, 2025 |
| 58 | + const fixedDate = new Date(2025, 2, 1) |
| 59 | + // Next 1st of the month, April 1, 2025 |
| 60 | + const expectedDate = new Date(2025, 3, 1) |
| 61 | + |
| 62 | + jest.setSystemTime(fixedDate) |
| 63 | + |
| 64 | + const result = getNextDigestAt("Monthly") |
| 65 | + expect(result).toEqual(Timestamp.fromDate(expectedDate)) |
| 66 | + }) |
| 67 | + |
| 68 | + it("should return null for none frequency", () => { |
| 69 | + const result = getNextDigestAt("None") |
| 70 | + expect(result).toBeNull() |
| 71 | + }) |
| 72 | + |
| 73 | + it("should log an error and return null for unknown frequency", () => { |
| 74 | + const consoleSpy = jest.spyOn(console, "error").mockImplementation() |
| 75 | + const result = getNextDigestAt("SomeNewFrequency" as Frequency) |
| 76 | + expect(result).toBeNull() |
| 77 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 78 | + "Unknown notification frequency: SomeNewFrequency" |
| 79 | + ) |
| 80 | + consoleSpy.mockRestore() |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +describe("getNotificationStartDate", () => { |
| 85 | + it("should return the previous Tuesday for weekly frequency", () => { |
| 86 | + const fixedDate = Timestamp.fromDate(new Date(2025, 2, 11)) |
| 87 | + const expectedDate = Timestamp.fromDate(new Date(2025, 2, 4)) |
| 88 | + |
| 89 | + const result = getNotificationStartDate("Weekly", fixedDate) |
| 90 | + expect(result).toEqual(expectedDate) |
| 91 | + }) |
| 92 | + |
| 93 | + it("should return the previous 1st of the month for monthly frequency", () => { |
| 94 | + const fixedDate = Timestamp.fromDate(new Date(2025, 2, 1)) |
| 95 | + const expectedDate = Timestamp.fromDate(new Date(2025, 1, 1)) |
| 96 | + |
| 97 | + const result = getNotificationStartDate("Monthly", fixedDate) |
| 98 | + expect(result).toEqual(expectedDate) |
| 99 | + }) |
| 100 | +}) |
0 commit comments