Skip to content

Commit

Permalink
Accept eventPayload in onEvent handler (#1331)
Browse files Browse the repository at this point in the history
This is a bug fix for the scenario where we weren't removing a survey from the activated surveys list once the survey is shown.
  • Loading branch information
Phani Raj authored Jul 30, 2024
1 parent ab5346d commit 47b43f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 18 additions & 8 deletions src/__tests__/utils/survey-event-receiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,26 @@ describe('survey-event-receiver', () => {
it('receiver activates survey on event', () => {
const surveyEventReceiver = new SurveyEventReceiver(instance)
surveyEventReceiver.register(surveysWithEvents)
surveyEventReceiver.onEvent('billing_changed')
const registeredHook = instance._addCaptureHook.mock.calls[0][0]
registeredHook('billing_changed')
const activatedSurveys = surveyEventReceiver.getSurveys()
expect(activatedSurveys).toContain('first-survey')
})

it('receiver removes survey from list after its shown', () => {
const surveyEventReceiver = new SurveyEventReceiver(instance)
const firstSurvey = surveysWithEvents[0]
if (firstSurvey.conditions && firstSurvey.conditions?.events) {
firstSurvey.conditions.events.repeatedActivation = true
}

surveyEventReceiver.register(surveysWithEvents)
surveyEventReceiver.onEvent('billing_changed')
const registeredHook = instance._addCaptureHook.mock.calls[0][0]
registeredHook('billing_changed')
const activatedSurveys = surveyEventReceiver.getSurveys()
expect(activatedSurveys).toContain('first-survey')

surveyEventReceiver.onEvent('survey shown', {
registeredHook('survey shown', {
$set: undefined,
$set_once: undefined,
event: 'survey shown',
Expand All @@ -135,25 +142,28 @@ describe('survey-event-receiver', () => {
it('receiver activates same survey on multiple event', () => {
const surveyEventReceiver = new SurveyEventReceiver(instance)
surveyEventReceiver.register(surveysWithEvents)
surveyEventReceiver.onEvent('billing_changed')
const registeredHook = instance._addCaptureHook.mock.calls[0][0]
registeredHook('billing_changed')
expect(surveyEventReceiver.getSurveys()).toEqual(['first-survey'])
surveyEventReceiver.onEvent('billing_removed')
registeredHook('billing_removed')
expect(surveyEventReceiver.getSurveys()).toEqual(['first-survey'])
})

it('receiver activates multiple surveys on same event', () => {
const surveyEventReceiver = new SurveyEventReceiver(instance)
surveyEventReceiver.register(surveysWithEvents)
surveyEventReceiver.onEvent('user_subscribed')
const registeredHook = instance._addCaptureHook.mock.calls[0][0]
registeredHook('user_subscribed')
expect(surveyEventReceiver.getSurveys()).toEqual(['first-survey', 'third-survey'])
})

it('receiver activates multiple surveys on different events', () => {
const surveyEventReceiver = new SurveyEventReceiver(instance)
surveyEventReceiver.register(surveysWithEvents)
surveyEventReceiver.onEvent('billing_changed')
const registeredHook = instance._addCaptureHook.mock.calls[0][0]
registeredHook('billing_changed')
expect(surveyEventReceiver.getSurveys()).toEqual(['first-survey'])
surveyEventReceiver.onEvent('address_changed')
registeredHook('address_changed')
expect(surveyEventReceiver.getSurveys()).toEqual(['first-survey', 'third-survey'])
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/utils/survey-event-receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export class SurveyEventReceiver {
}

// match any events to its corresponding survey.
const matchEventToSurvey = (eventName: string) => {
this.onEvent(eventName)
const matchEventToSurvey = (eventName: string, eventPayload?: CaptureResult) => {
this.onEvent(eventName, eventPayload)
}
this.instance?._addCaptureHook(matchEventToSurvey)

Expand Down

0 comments on commit 47b43f5

Please sign in to comment.