Skip to content
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
59 changes: 59 additions & 0 deletions src/hooks/useCommunityPromoToast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,65 @@ describe('useCommunityPromoToast', () => {
expect(mockToast).not.toHaveBeenCalled();
});

it('does not show toast when profile is still loading (forumUsername undefined)', () => {
renderHook(() =>
useCommunityPromoToast({
userCreatedAt: getDateDaysAgo(5),
forumUsername: undefined, // Profile hasn't loaded yet
isAuthenticated: true,
})
);

vi.advanceTimersByTime(2000);
expect(mockToast).not.toHaveBeenCalled();
});

it('shows toast only after profile loads with null forum_username', () => {
const { rerender } = renderHook(
({ forumUsername }) =>
useCommunityPromoToast({
userCreatedAt: getDateDaysAgo(5),
forumUsername,
isAuthenticated: true,
}),
{
initialProps: { forumUsername: undefined as string | null | undefined },
}
);

// Profile still loading - should not show toast
vi.advanceTimersByTime(2000);
expect(mockToast).not.toHaveBeenCalled();

// Profile loads with null forum_username - should show toast
rerender({ forumUsername: null });
vi.advanceTimersByTime(2000);
expect(mockToast).toHaveBeenCalledTimes(1);
});

it('does not show toast when profile loads with existing forum_username', () => {
const { rerender } = renderHook(
({ forumUsername }) =>
useCommunityPromoToast({
userCreatedAt: getDateDaysAgo(5),
forumUsername,
isAuthenticated: true,
}),
{
initialProps: { forumUsername: undefined as string | null | undefined },
}
);

// Profile still loading - should not show toast
vi.advanceTimersByTime(2000);
expect(mockToast).not.toHaveBeenCalled();

// Profile loads with existing forum_username - should NOT show toast
rerender({ forumUsername: 'existingUser' });
vi.advanceTimersByTime(2000);
expect(mockToast).not.toHaveBeenCalled();
});

it('does not show toast when less than 3 days since signup', () => {
renderHook(() =>
useCommunityPromoToast({
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/useCommunityPromoToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export function useCommunityPromoToast({
return;
}

// Guard: Wait for profile to load before deciding
// undefined = profile still loading, null = profile loaded but no forum username
if (forumUsername === undefined) {
return;
}

// Guard: User has already authenticated with Discourse
if (forumUsername) {
return;
Expand Down
Loading