Skip to content

Commit 4cf5000

Browse files
committed
fix: last seen survey date logic
1 parent 6bd5095 commit 4cf5000

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
})

src/extensions/surveys.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
getContrastingTextColor,
3333
getDisplayOrderQuestions,
3434
getSurveySeen,
35+
hasWaitPeriodPassed,
3536
sendSurveyEvent,
3637
style,
3738
SurveyContext,
@@ -69,13 +70,9 @@ export class SurveyManager {
6970
private handlePopoverSurvey = (survey: Survey): void => {
7071
const surveyWaitPeriodInDays = survey.conditions?.seenSurveyWaitPeriodInDays
7172
const lastSeenSurveyDate = localStorage.getItem(`lastSeenSurveyDate`)
72-
if (surveyWaitPeriodInDays && lastSeenSurveyDate) {
73-
const today = new Date()
74-
const diff = Math.abs(today.getTime() - new Date(lastSeenSurveyDate).getTime())
75-
const diffDaysFromToday = Math.ceil(diff / (1000 * 3600 * 24))
76-
if (diffDaysFromToday < surveyWaitPeriodInDays) {
77-
return
78-
}
73+
74+
if (!hasWaitPeriodPassed(lastSeenSurveyDate, surveyWaitPeriodInDays)) {
75+
return
7976
}
8077

8178
const surveySeen = getSurveySeen(survey)

src/extensions/surveys/surveys-utils.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,20 @@ const getSurveyInteractionProperty = (survey: Survey, action: string): string =>
694694
return surveyProperty
695695
}
696696

697+
export const hasWaitPeriodPassed = (
698+
lastSeenSurveyDate: string | null,
699+
waitPeriodInDays: number | undefined
700+
): boolean => {
701+
if (!waitPeriodInDays || !lastSeenSurveyDate) {
702+
return true
703+
}
704+
705+
const today = new Date()
706+
const diff = Math.abs(today.getTime() - new Date(lastSeenSurveyDate).getTime())
707+
const diffDaysFromToday = Math.ceil(diff / (1000 * 3600 * 24))
708+
return diffDaysFromToday > waitPeriodInDays
709+
}
710+
697711
interface SurveyContextProps {
698712
isPreviewMode: boolean
699713
previewPageIndex: number | undefined

0 commit comments

Comments
 (0)