Files
supabase/apps/studio/components/interfaces/HomePageActions.test.tsx
Drew McLellan d4ef834d4d feat(studio): track usage on the org projects list (FE-5025) (#54417)
## Problem

We don't have visibility into how people use the org projects list
(`/org/[slug]`) or the organizations list (`/organizations`): whether
they search, filter, sort or switch views, and which project card
actions they use. Resolves FE-5025.

## Solution

Adds four events, defined in `packages/common/telemetry-constants.ts`
with org-level groups (no project group):

- `org_projects_filter_applied` with `filterType: 'search' | 'status' |
'sort'`. Search sends only the result count, status sends the saved
statuses (`active`/`paused`), and sort sends the sort value.
- `org_projects_view_mode_updated` with `viewMode`.
- `org_project_card_action_clicked` with `action: 'copy_id' |
'settings'` and `viewMode`, from the grid card menu and the table row
menu and inline ref button.
- `org_list_search_submitted` with `orgCount`.

Notes for review:

- Every event fires from its handler, not an effect, so restoring
filters from local storage doesn't fire anything.
- Search events use the same 500ms debounce as the query, skip empty
input, and never include the search text. The project search count is
read from the list query's cache entry via `fetchInfiniteQuery` on the
same key, which reuses the in-flight request.
- Status, sort and view events don't fire when the value didn't change.
- `HomePageActions` and `ProjectList` also render on the `/project`
picker, so `@page` lists both routes.
- I didn't add the `data-attr` attributes from the ticket: autocapture
is off, so they would send nothing, and the telemetry standards ask for
explicit `track()` calls.

## How to test

- `pnpm --filter studio exec vitest run
components/interfaces/HomePageActions.test.tsx` covers view mode, status
filter, search and clearing before the debounce settles.
- In the browser network tab on `/org/[slug]`, each action sends one
`telemetry/event` request with the properties above. Checking the events
in PostHog staging is still to do.
2026-12-02 11:47:18 +00:00

154 lines
5.5 KiB
TypeScript

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<Record<string, unknown>>()
return { ...actual, IS_PLATFORM: true }
})
const MATCHING_PROJECT_COUNT = 3
const renderActions = () => {
addAPIMock({
method: 'get',
path: '/platform/organizations/:slug/projects',
response: () =>
HttpResponse.json<OrganizationProjectsResponse>({
projects: [],
pagination: { count: MATCHING_PROJECT_COUNT, limit: 96, offset: 0 },
}),
})
return customRender(<HomePageActions />, { 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()
})
})