|
| 1 | +import { hasWaitPeriodPassed } from '../../extensions/surveys/surveys-utils' |
| 2 | + |
| 3 | +describe('hasWaitPeriodPassed', () => { |
| 4 | + let originalDate: DateConstructor |
| 5 | + let mockCurrentDate: Date |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + // Store the original Date constructor |
| 9 | + originalDate = global.Date |
| 10 | + // Mock the current date to be 2025-01-15 12:00:00 UTC |
| 11 | + mockCurrentDate = new Date('2025-01-15T12:00:00Z') |
| 12 | + |
| 13 | + global.Date = class extends Date { |
| 14 | + constructor(date?: string | number | Date) { |
| 15 | + if (date) { |
| 16 | + super(date) |
| 17 | + return new originalDate(date) |
| 18 | + } |
| 19 | + super() |
| 20 | + return mockCurrentDate |
| 21 | + } |
| 22 | + } as DateConstructor |
| 23 | + }) |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + // Restore the original Date constructor |
| 27 | + global.Date = originalDate |
| 28 | + }) |
| 29 | + |
| 30 | + it('should return true when no wait period is specified', () => { |
| 31 | + expect(hasWaitPeriodPassed('2025-01-01T12:00:00Z', undefined)).toBe(true) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should return true when no last seen date is provided', () => { |
| 35 | + expect(hasWaitPeriodPassed(null, 7)).toBe(true) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should return false when less than wait period has passed', () => { |
| 39 | + const lastSeenDate = '2025-01-10T12:00:00Z' // 5 days ago |
| 40 | + expect(hasWaitPeriodPassed(lastSeenDate, 7)).toBe(false) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should return false when the wait period has not passed yet', () => { |
| 44 | + const lastSeenDate = '2025-01-08T12:00:00Z' // 7 days ago |
| 45 | + expect(hasWaitPeriodPassed(lastSeenDate, 7)).toBe(false) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should return true one second after the wait period has passed', () => { |
| 49 | + const lastSeenDate = '2025-01-08T11:59:59Z' // 7 days ago |
| 50 | + expect(hasWaitPeriodPassed(lastSeenDate, 1)).toBe(true) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should return true when more than wait period has passed', () => { |
| 54 | + const lastSeenDate = '2025-01-01T12:00:00Z' // 14 days ago |
| 55 | + expect(hasWaitPeriodPassed(lastSeenDate, 7)).toBe(true) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should handle decimal wait periods by rounding up days difference', () => { |
| 59 | + const lastSeenDate = '2025-01-10T00:00:00Z' // 5.5 days ago |
| 60 | + expect(hasWaitPeriodPassed(lastSeenDate, 5)).toBe(true) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should handle invalid date strings by returning false', () => { |
| 64 | + expect(hasWaitPeriodPassed('invalid-date', 7)).toBe(false) |
| 65 | + }) |
| 66 | + |
| 67 | + // test case for when just 5 minutes have passed |
| 68 | + it('should return false when just 5 minutes have passed', () => { |
| 69 | + const lastSeenDate = '2025-01-15T11:55:00Z' // 5 minutes ago |
| 70 | + expect(hasWaitPeriodPassed(lastSeenDate, 1)).toBe(false) |
| 71 | + }) |
| 72 | +}) |
0 commit comments