-
Notifications
You must be signed in to change notification settings - Fork 142
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: check url matching on URL change for widget-type surveys #1755
fix: check url matching on URL change for widget-type surveys #1755
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This PR extracts URL matching logic into a reusable hook to fix widget-type surveys not properly hiding when URLs no longer match conditions, similar to a previous fix for popover surveys.
- Added
useToggleSurveyOnURLChange
hook in/src/extensions/surveys.tsx
to handle URL matching across different navigation types - Modified
doesSurveyUrlMatch
in/src/posthog-surveys.ts
to acceptPick<Survey, 'conditions'>
for better type safety and reusability - Added comprehensive test coverage in
/src/__tests__/posthog-surveys.test.ts
for URL matching scenarios including exact, regex, and contains matches - Added tests in
/src/__tests__/extensions/surveys.test.ts
for browser navigation methods (pushState, replaceState, popstate) - Improved cleanup handling for event listeners and history state modifications
4 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile
it('should match using exact match type', () => { | ||
mockWindowLocation('https://example.com') | ||
|
||
const survey = { | ||
conditions: { | ||
url: 'https://example.com', | ||
urlMatchType: 'exact' as const, | ||
events: null, | ||
actions: null, | ||
}, | ||
} | ||
expect(doesSurveyUrlMatch(survey)).toBe(true) | ||
|
||
const nonMatchingSurvey = { | ||
conditions: { | ||
url: 'https://example.com/path', | ||
urlMatchType: 'exact' as const, | ||
events: null, | ||
actions: null, | ||
}, | ||
} | ||
expect(doesSurveyUrlMatch(nonMatchingSurvey)).toBe(false) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider adding test for exact matching with trailing slashes and query parameters
expect(mockSetSurveyVisible).not.toHaveBeenCalled() | ||
expect(mockRemoveSurveyFromFocus).not.toHaveBeenCalled() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Initial mount visibility test may be flaky - should explicitly test initial visibility state matches URL condition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it shouldn't, as this hook is only to trigger after URL changes - the initial filter is done by getActiveMatchingSurveys
Size Change: +2.12 kB (+0.06%) Total Size: 3.32 MB
ℹ️ View Unchanged
|
Changes
we already did this fix for popover-type surveys here: #1732
however, we just got a ticket that the same is stil lhappening for widget-type surveys
so I extracted that new logic into a hook, create tests for it (and also for
doesSurveyUrlMatch
), and then applied it to both casesChecklist