Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: last seen survey date logic #1745

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/__tests__/extensions/surveys-utils.test.ts
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)
})
})
11 changes: 4 additions & 7 deletions src/extensions/surveys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
getContrastingTextColor,
getDisplayOrderQuestions,
getSurveySeen,
hasWaitPeriodPassed,
sendSurveyEvent,
style,
SurveyContext,
Expand Down Expand Up @@ -69,13 +70,9 @@ export class SurveyManager {
private handlePopoverSurvey = (survey: Survey): void => {
const surveyWaitPeriodInDays = survey.conditions?.seenSurveyWaitPeriodInDays
const lastSeenSurveyDate = localStorage.getItem(`lastSeenSurveyDate`)
if (surveyWaitPeriodInDays && lastSeenSurveyDate) {
const today = new Date()
const diff = Math.abs(today.getTime() - new Date(lastSeenSurveyDate).getTime())
const diffDaysFromToday = Math.ceil(diff / (1000 * 3600 * 24))
if (diffDaysFromToday < surveyWaitPeriodInDays) {
return
}

if (!hasWaitPeriodPassed(lastSeenSurveyDate, surveyWaitPeriodInDays)) {
return
}

const surveySeen = getSurveySeen(survey)
Expand Down
14 changes: 14 additions & 0 deletions src/extensions/surveys/surveys-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,20 @@ const getSurveyInteractionProperty = (survey: Survey, action: string): string =>
return surveyProperty
}

export const hasWaitPeriodPassed = (
lastSeenSurveyDate: string | null,
waitPeriodInDays: number | undefined
): boolean => {
if (!waitPeriodInDays || !lastSeenSurveyDate) {
return true
}

const today = new Date()
const diff = Math.abs(today.getTime() - new Date(lastSeenSurveyDate).getTime())
const diffDaysFromToday = Math.ceil(diff / (1000 * 3600 * 24))
return diffDaysFromToday > waitPeriodInDays
}

interface SurveyContextProps {
isPreviewMode: boolean
previewPageIndex: number | undefined
Expand Down
Loading