Skip to content

Commit

Permalink
fix: last seen survey date logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasheriques committed Feb 18, 2025
1 parent 6bd5095 commit 4cf5000
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
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

0 comments on commit 4cf5000

Please sign in to comment.