import { QueryClient } from '@tanstack/react-query' import { createMemoryHistory, createRootRoute, createRouter, type AnyRouter, } from '@tanstack/react-router' import { act, fireEvent, screen, waitFor, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { platformComponents as components } from 'api-types' import { LOCAL_STORAGE_KEYS, safeLocalStorage } from 'common' import { delay, HttpResponse } from 'msw' import { Toaster } from 'sonner' import { afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' import { ONBOARDING_SURVEY_FOLLOW_UP_WINDOW_MS, VERCEL_MARKETPLACE_SURVEY_START, } from './OnboardingSurvey.constants' import { markFollowUpPending } from './OnboardingSurveyFollowUp.utils' import { OnboardingSurveyFollowUpCard } from './OnboardingSurveyFollowUpCard' import { BannerStack } from '@/components/ui/BannerStack/BannerStack' import { BannerStackProvider } from '@/components/ui/BannerStack/BannerStackProvider' import { organizationKeys } from '@/data/organizations/keys' import { castOrganizationSlugResponseToOrganization } from '@/data/organizations/organization-query' import { castOrganizationResponseToOrganization } from '@/data/organizations/organizations-query' import { parseSearch, stringifySearch } from '@/lib/router-search-params' import { createMockOrganizationResponse } from '@/tests/helpers' import { customRender } from '@/tests/lib/custom-render' import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw' import { createMockProfile, createMockProfileContext } from '@/tests/lib/profile-helpers' type OrganizationResponse = components['schemas']['OrganizationResponse_Output'] type OrganizationDetailResponse = components['schemas']['OrganizationSlugResponse_Output'] type OnboardingSurveyBody = components['schemas']['OnboardingSurveyBody'] const PROFILE_ID = 'user-1' const ORG_SLUG = 'new-org' // Telemetry is consent-gated and never reaches the network in tests, so the // event itself is captured here const { track } = vi.hoisted(() => ({ track: vi.fn() })) vi.mock('@/lib/telemetry/track', () => ({ useTrack: () => track })) vi.mock(import('common'), async (importOriginal) => { const actual = await importOriginal() return { ...actual, useParams: () => ({ slug: ORG_SLUG }), useIsLoggedIn: () => true, } }) const storageKey = LOCAL_STORAGE_KEYS.ONBOARDING_SURVEY_FOLLOW_UP(PROFILE_ID, ORG_SLUG) const readStoredState = () => JSON.parse(safeLocalStorage.getItem(storageKey) ?? 'null') const mockSurveyEndpoint = (status: 204 | 500) => { const requests: OnboardingSurveyBody[] = [] addAPIMock({ method: 'post', path: '/platform/organizations/onboarding-survey', response: async ({ request }) => { requests.push((await request.json()) as OnboardingSurveyBody) // Long enough for the card to render the dismissal before the save settles await delay(50) if (status === 500) { return HttpResponse.json({ message: 'Boom' }, { status: 500 }) } return new HttpResponse(null, { status: 204 }) }, }) return requests } const toOrganizationDetail = ( organization: OrganizationResponse, createdAt: string ): OrganizationDetailResponse => ({ ...organization, created_at: createdAt, has_oriole_project: false, }) const mockOrganizationDetail = (organization: OrganizationResponse, createdAt: string) => { const requests: string[] = [] addAPIMock({ method: 'get', path: '/platform/organizations/:slug', response: ({ request }) => { requests.push(request.url) return HttpResponse.json( toOrganizationDetail(organization, createdAt) ) }, }) return requests } // The app root keeps the org list and a free org's details cached on every page const cachedQueryClient = (organization: OrganizationResponse, createdAt: string) => { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } }, }) queryClient.setQueryData(organizationKeys.list(), [ castOrganizationResponseToOrganization(organization), ]) queryClient.setQueryData( organizationKeys.detail(organization.slug), castOrganizationSlugResponseToOrganization(toOrganizationDetail(organization, createdAt)) ) return queryClient } const renderCard = ({ organization = createMockOrganizationResponse({ slug: ORG_SLUG }), queryClient, initialUrl = `/org/${ORG_SLUG}`, }: { organization?: OrganizationResponse queryClient?: QueryClient initialUrl?: string } = {}) => { addAPIMock({ method: 'get', path: '/platform/organizations', response: () => HttpResponse.json([organization]), }) const router = createRouter({ routeTree: createRootRoute(), history: createMemoryHistory({ initialEntries: [initialUrl] }), parseSearch, stringifySearch, }) const result = customRender( , { queryClient, router, profileContext: createMockProfileContext({ profile: createMockProfile({ gotrue_id: PROFILE_ID }), }), } ) return { ...result, router } } const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime }) // Radix Selects render a hidden native