|
| 1 | +import { http, HttpResponse } from 'msw'; |
| 2 | +import { describe, expect, expectTypeOf, it } from 'vitest'; |
| 3 | + |
| 4 | +import { server, validateHeaders } from '../../mock-server'; |
| 5 | +import { createBackendApiClient } from '../factory'; |
| 6 | +import type { PaginatedResourceResponse } from '../resources/Deserializer'; |
| 7 | +import type { OrganizationInvitation } from '../resources/OrganizationInvitation'; |
| 8 | + |
| 9 | +describe('OrganizationAPI', () => { |
| 10 | + const apiClient = createBackendApiClient({ |
| 11 | + apiUrl: 'https://api.clerk.test', |
| 12 | + secretKey: 'deadbeef', |
| 13 | + }); |
| 14 | + |
| 15 | + const organizationId = 'org_123'; |
| 16 | + |
| 17 | + const mockInvitation = { |
| 18 | + object: 'organization_invitation', |
| 19 | + id: 'orginv_1', |
| 20 | + email_address: 'one@example.com', |
| 21 | + role: 'org:member', |
| 22 | + role_name: 'Member', |
| 23 | + organization_id: organizationId, |
| 24 | + status: 'pending', |
| 25 | + public_metadata: {}, |
| 26 | + private_metadata: {}, |
| 27 | + url: null, |
| 28 | + created_at: 1640995200, |
| 29 | + updated_at: 1640995200, |
| 30 | + expires_at: 1643673600, |
| 31 | + }; |
| 32 | + |
| 33 | + describe('createOrganizationInvitationBulk', () => { |
| 34 | + it('returns a paginated { data, totalCount } response matching the Backend API shape', async () => { |
| 35 | + server.use( |
| 36 | + http.post( |
| 37 | + `https://api.clerk.test/v1/organizations/${organizationId}/invitations/bulk`, |
| 38 | + validateHeaders(async ({ request }) => { |
| 39 | + // The SDK should forward the invitations as a snake_cased array body. |
| 40 | + const body = (await request.json()) as Array<Record<string, unknown>>; |
| 41 | + expect(body).toHaveLength(2); |
| 42 | + expect(body[0]).toMatchObject({ |
| 43 | + email_address: 'one@example.com', |
| 44 | + role: 'org:member', |
| 45 | + inviter_user_id: 'user_1', |
| 46 | + }); |
| 47 | + |
| 48 | + return HttpResponse.json({ |
| 49 | + data: [mockInvitation, { ...mockInvitation, id: 'orginv_2', email_address: 'two@example.com' }], |
| 50 | + total_count: 2, |
| 51 | + }); |
| 52 | + }), |
| 53 | + ), |
| 54 | + ); |
| 55 | + |
| 56 | + const response = await apiClient.organizations.createOrganizationInvitationBulk(organizationId, [ |
| 57 | + { emailAddress: 'one@example.com', role: 'org:member', inviterUserId: 'user_1' }, |
| 58 | + { emailAddress: 'two@example.com', role: 'org:member', inviterUserId: 'user_1' }, |
| 59 | + ]); |
| 60 | + |
| 61 | + // The Backend API wraps bulk results in a paginated envelope, and the |
| 62 | + // deserializer surfaces it as `{ data, totalCount }`. |
| 63 | + expect(response.data).toHaveLength(2); |
| 64 | + expect(response.data[0].emailAddress).toBe('one@example.com'); |
| 65 | + expect(response.data[0].organizationId).toBe(organizationId); |
| 66 | + expect(response.totalCount).toBe(2); |
| 67 | + |
| 68 | + // The declared return type must reflect that paginated shape, the same |
| 69 | + // way the sibling `getOrganizationInvitationList` does. |
| 70 | + expectTypeOf(response).toEqualTypeOf<PaginatedResourceResponse<OrganizationInvitation[]>>(); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments