Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 17 additions & 15 deletions packages/react/src/components/PostHogFeature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,23 @@
const shouldTrackInteraction = trackInteraction ?? true
const shouldTrackView = trackView ?? true

if (isUndefined(match) || variant === match) {
const childNode: React.ReactNode = isFunction(children) ? children(payload) : children
return (
<VisibilityAndClickTrackers
flag={flag}
options={visibilityObserverOptions}
trackInteraction={shouldTrackInteraction}
trackView={shouldTrackView}
onInteract={() => captureFeatureInteraction({ flag, posthog, flagVariant: variant })}
onView={() => captureFeatureView({ flag, posthog, flagVariant: variant })}
{...props}
>
{childNode}
</VisibilityAndClickTrackers>
)
if (variant !== undefined) {

Check failure on line 34 in packages/react/src/components/PostHogFeature.tsx

View workflow job for this annotation

GitHub Actions / Lint

Use isUndefined() instead of direct undefined checks
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://posthog.com/docs/libraries/js/usage#enriched-flag-analytics
anyone can pass any flag to PostHogFeature
how do we know if the flag actually exists?
it could that payload and variant are undefined here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah your last commit checks for variant

if (isUndefined(match) || variant === match) {
const childNode: React.ReactNode = isFunction(children) ? children(payload) : children
return (
<VisibilityAndClickTrackers
flag={flag}
options={visibilityObserverOptions}
trackInteraction={shouldTrackInteraction}
trackView={shouldTrackView}
onInteract={() => captureFeatureInteraction({ flag, posthog, flagVariant: variant })}
onView={() => captureFeatureView({ flag, posthog, flagVariant: variant })}
{...props}
>
{childNode}
</VisibilityAndClickTrackers>
)
}
}
return <>{fallback}</>
}
Expand Down
39 changes: 39 additions & 0 deletions packages/react/src/components/__tests__/PostHogFeature.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,43 @@ describe('PostHogFeature component', () => {
fireEvent.click(screen.getByTestId('hi_example_feature_1_payload'))
expect(posthog.capture).toHaveBeenCalledTimes(1)
})

it('should not render when flag does not exist and no match is specified', () => {
render(
<PostHogProvider client={posthog}>
<PostHogFeature flag={'nonexistent_flag'}>
<div data-testid="helloDiv">Hello</div>
</PostHogFeature>
</PostHogProvider>
)

expect(screen.queryByTestId('helloDiv')).not.toBeInTheDocument()
expect(posthog.capture).not.toHaveBeenCalled()
})

it('should render fallback when flag does not exist (like new-cta example)', () => {
render(
<PostHogProvider client={posthog}>
<PostHogFeature flag={'new-cta'} fallback={<div data-testid="oldButton">Old Button</div>}>
<div data-testid="newButton">New Button</div>
</PostHogFeature>
</PostHogProvider>
)

expect(screen.queryByTestId('newButton')).not.toBeInTheDocument()
expect(screen.queryByTestId('oldButton')).toBeInTheDocument()
expect(posthog.capture).not.toHaveBeenCalled()
})

it('should render content when match=false and flag variant is false', () => {
render(
<PostHogProvider client={posthog}>
<PostHogFeature flag={'test_false'} match={false}>
<div data-testid="disabledUI">Show when disabled</div>
</PostHogFeature>
</PostHogProvider>
)

expect(screen.queryByTestId('disabledUI')).toBeInTheDocument()
})
})
Loading