# Studio MSW Component Tests Mount a Studio component, intercept its network calls with MSW, and assert what renders and what gets sent. Load [studio-testing](../SKILL.md) first to confirm the change warrants a component test before using this template. ## When to use - The component (or a rendered descendant) calls a React Query hook or mutation that hits `/platform/...`, `/v1/...`, or another endpoint typed in `packages/api-types`. - You would otherwise be tempted to write `vi.mock('@/data/some-query', ...)`. **Do not** — mock the network instead (see "Why not `vi.mock`" below). If the component is purely presentational with no data fetching, render and assert directly without MSW. ## The template ```tsx import { screen } from '@testing-library/react' import { platformComponents as components } from 'api-types' import { mockAnimationsApi } from 'jsdom-testing-mocks' import { HttpResponse } from 'msw' import { describe, expect, test } from 'vitest' import { MyComponent } from './MyComponent' import { createMockOrganizationResponse } from '@/tests/helpers' import { customRender } from '@/tests/lib/custom-render' import { addAPIMock } from '@/tests/lib/msw' import { createMockProfileContext } from '@/tests/lib/profile-helpers' type OrganizationResponse = components['schemas']['OrganizationResponse_Output'] mockAnimationsApi() describe('MyComponent', () => { test('renders rows from the API', async () => { addAPIMock({ method: 'get', path: '/platform/organizations', response: () => HttpResponse.json([ createMockOrganizationResponse({ name: 'Acme' }), ]), }) customRender(, { profileContext: createMockProfileContext() }) expect(await screen.findByText('Acme')).toBeInTheDocument() }) }) ``` Call `mockAnimationsApi()` when the component renders Radix UI relying on Web Animations (Sheet, Modal, Popover). Server lifecycle (`listen` / `resetHandlers` / `close`) is managed in `apps/studio/tests/vitestSetup.ts` — register `addAPIMock` inside each test or `beforeEach` (never `beforeAll`, because handlers reset after every test). ## Gotchas that cost debugging time ### 1. Path params use `:slug`, not `{slug}` `addAPIMock` remaps OpenAPI `{param}` segments to MSW's `:param` syntax: ```ts // ❌ TypeScript error, MSW will not match path: '/platform/organizations/{slug}/projects' // ✅ Correct path: '/platform/organizations/:slug/projects' ``` ### 2. Match response body semantics (`204`, `201`, and JSON `GET`s) - **204 No Content:** Always return a bodyless `new HttpResponse(null, { status: 204 })` — never `HttpResponse.json({}, { status: 204 })` (204 responses must not carry a body or `Content-Type: application/json`). - **201 Created:** Use `HttpResponse.json(body, { status: 201 })` when the OpenAPI schema defines a response body; for empty-body 201 endpoints (`content?: never`), return a bodyless `new HttpResponse(null, { status: 201 })` so the real `normalizeEmptyBodyResponse` path in `apps/studio/data/fetchers.ts` is exercised. - **JSON `GET` endpoints:** Always return a real JSON body via `HttpResponse.json(data)` — an empty `application/json` `GET` throws `MalformedResponseError`. ```ts // ✅ 204 No Content response: () => new HttpResponse(null, { status: 204 }) // ✅ 201 with an OpenAPI JSON body (pass explicit generic — see gotcha #8) response: () => HttpResponse.json(createdItem, { status: 201 }) ``` ### 3. Submit buttons in Sheets and Modals need `fireEvent.click` External form submit buttons (`