import { screen, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { platformComponents as components } from 'api-types' import { LOCAL_STORAGE_KEYS } from 'common' import { mockIntersectionObserver } from 'jsdom-testing-mocks' import { HttpResponse } from 'msw' import { beforeEach, describe, expect, test, vi } from 'vitest' import { HomePageActions } from './HomePageActions' import { customRender } from '@/tests/lib/custom-render' import { addAPIMock } from '@/tests/lib/msw' import { createMockProfileContext } from '@/tests/lib/profile-helpers' type OrganizationProjectsResponse = components['schemas']['OrganizationProjectsResponse_Output'] mockIntersectionObserver() const ORG_SLUG = 'test-org' const { track, params } = vi.hoisted(() => ({ track: vi.fn(), params: { slug: undefined as string | undefined }, })) vi.mock('@/lib/telemetry/track', () => ({ useTrack: () => track })) vi.mock('common', async (importOriginal) => { const actual = (await importOriginal()) as typeof import('common') return { ...actual, useParams: () => params, useIsLoggedIn: () => true } }) vi.mock('@/lib/constants', async (importOriginal) => { const actual = await importOriginal>() return { ...actual, IS_PLATFORM: true } }) const MATCHING_PROJECT_COUNT = 3 const renderActions = () => { addAPIMock({ method: 'get', path: '/platform/organizations/:slug/projects', response: () => HttpResponse.json({ projects: [], pagination: { count: MATCHING_PROJECT_COUNT, limit: 96, offset: 0 }, }), }) return customRender(, { profileContext: createMockProfileContext() }) } describe('HomePageActions tracking', () => { beforeEach(() => { track.mockClear() params.slug = ORG_SLUG window.localStorage.clear() }) test('switching view mode tracks once, and picking the current view tracks nothing', async () => { const user = userEvent.setup() renderActions() await user.click(await screen.findByRole('radio', { name: 'Toggle grid view' })) expect(track).not.toHaveBeenCalled() await user.click(screen.getByRole('radio', { name: 'Toggle list view' })) expect(track).toHaveBeenCalledTimes(1) expect(track).toHaveBeenCalledWith('org_projects_view_mode_updated', { viewMode: 'table' }) }) test('saving a status filter tracks the saved statuses, and saving it unchanged tracks nothing', async () => { const user = userEvent.setup() renderActions() await user.click(await screen.findByRole('button', { name: /Status/ })) await user.click(screen.getByRole('checkbox', { name: 'Paused' })) await user.click(screen.getByRole('button', { name: 'Save' })) expect(track).toHaveBeenCalledTimes(1) expect(track).toHaveBeenCalledWith('org_projects_filter_applied', { filterType: 'status', statuses: ['paused'], }) track.mockClear() await user.click(screen.getByRole('button', { name: /Status/ })) await user.click(screen.getByRole('button', { name: 'Save' })) expect(track).not.toHaveBeenCalled() }) test('saving the same statuses in a different order tracks nothing', async () => { window.localStorage.setItem( LOCAL_STORAGE_KEYS.PROJECTS_FILTER, JSON.stringify(['INACTIVE', 'ACTIVE_HEALTHY']) ) const user = userEvent.setup() renderActions() await user.click(await screen.findByRole('button', { name: /Status/ })) await waitFor(() => expect(screen.getByRole('checkbox', { name: 'Paused' })).toBeChecked()) expect(screen.getByRole('checkbox', { name: 'Active' })).toBeChecked() await user.click(screen.getByRole('button', { name: 'Save' })) expect(track).not.toHaveBeenCalled() }) test('without a slug (the project picker), filter events are skipped but view mode still tracks', async () => { params.slug = undefined const user = userEvent.setup() renderActions() await user.click(await screen.findByRole('button', { name: /Status/ })) await user.click(screen.getByRole('checkbox', { name: 'Paused' })) await user.click(screen.getByRole('button', { name: 'Save' })) await user.type(screen.getByPlaceholderText('Search for a project'), 'abc') await new Promise((resolve) => setTimeout(resolve, 900)) expect(track).not.toHaveBeenCalled() await user.click(screen.getByRole('radio', { name: 'Toggle list view' })) expect(track).toHaveBeenCalledTimes(1) expect(track).toHaveBeenCalledWith('org_projects_view_mode_updated', { viewMode: 'table' }) }) test('searching tracks once after typing settles, with the result count and no search text', async () => { const user = userEvent.setup() renderActions() await user.type(await screen.findByPlaceholderText('Search for a project'), 'secret-name') await waitFor(() => expect(track).toHaveBeenCalledTimes(1), { timeout: 3000 }) expect(track).toHaveBeenCalledWith('org_projects_filter_applied', { filterType: 'search', projectCount: MATCHING_PROJECT_COUNT, }) expect(JSON.stringify(track.mock.calls)).not.toContain('secret-name') }) test('clearing the search before it settles tracks nothing', async () => { const user = userEvent.setup() renderActions() const input = await screen.findByPlaceholderText('Search for a project') await user.type(input, 'abc') await user.clear(input) await new Promise((resolve) => setTimeout(resolve, 900)) expect(track).not.toHaveBeenCalled() }) })