-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bd5095
commit 4cf5000
Showing
3 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { hasWaitPeriodPassed } from '../../extensions/surveys/surveys-utils' | ||
|
||
describe('hasWaitPeriodPassed', () => { | ||
let originalDate: DateConstructor | ||
let mockCurrentDate: Date | ||
|
||
beforeEach(() => { | ||
// Store the original Date constructor | ||
originalDate = global.Date | ||
// Mock the current date to be 2025-01-15 12:00:00 UTC | ||
mockCurrentDate = new Date('2025-01-15T12:00:00Z') | ||
|
||
global.Date = class extends Date { | ||
constructor(date?: string | number | Date) { | ||
if (date) { | ||
super(date) | ||
return new originalDate(date) | ||
} | ||
super() | ||
return mockCurrentDate | ||
} | ||
} as DateConstructor | ||
}) | ||
|
||
afterEach(() => { | ||
// Restore the original Date constructor | ||
global.Date = originalDate | ||
}) | ||
|
||
it('should return true when no wait period is specified', () => { | ||
expect(hasWaitPeriodPassed('2025-01-01T12:00:00Z', undefined)).toBe(true) | ||
}) | ||
|
||
it('should return true when no last seen date is provided', () => { | ||
expect(hasWaitPeriodPassed(null, 7)).toBe(true) | ||
}) | ||
|
||
it('should return false when less than wait period has passed', () => { | ||
const lastSeenDate = '2025-01-10T12:00:00Z' // 5 days ago | ||
expect(hasWaitPeriodPassed(lastSeenDate, 7)).toBe(false) | ||
}) | ||
|
||
it('should return false when the wait period has not passed yet', () => { | ||
const lastSeenDate = '2025-01-08T12:00:00Z' // 7 days ago | ||
expect(hasWaitPeriodPassed(lastSeenDate, 7)).toBe(false) | ||
}) | ||
|
||
it('should return true one second after the wait period has passed', () => { | ||
const lastSeenDate = '2025-01-08T11:59:59Z' // 7 days ago | ||
expect(hasWaitPeriodPassed(lastSeenDate, 1)).toBe(true) | ||
}) | ||
|
||
it('should return true when more than wait period has passed', () => { | ||
const lastSeenDate = '2025-01-01T12:00:00Z' // 14 days ago | ||
expect(hasWaitPeriodPassed(lastSeenDate, 7)).toBe(true) | ||
}) | ||
|
||
it('should handle decimal wait periods by rounding up days difference', () => { | ||
const lastSeenDate = '2025-01-10T00:00:00Z' // 5.5 days ago | ||
expect(hasWaitPeriodPassed(lastSeenDate, 5)).toBe(true) | ||
}) | ||
|
||
it('should handle invalid date strings by returning false', () => { | ||
expect(hasWaitPeriodPassed('invalid-date', 7)).toBe(false) | ||
}) | ||
|
||
// test case for when just 5 minutes have passed | ||
it('should return false when just 5 minutes have passed', () => { | ||
const lastSeenDate = '2025-01-15T11:55:00Z' // 5 minutes ago | ||
expect(hasWaitPeriodPassed(lastSeenDate, 1)).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters