|
| 1 | +import { http, HttpResponse } from 'msw'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { server, validateHeaders } from '../../mock-server'; |
| 5 | +import { createBackendApiClient } from '../factory'; |
| 6 | + |
| 7 | +describe('ClientAPI', () => { |
| 8 | + const apiClient = createBackendApiClient({ |
| 9 | + apiUrl: 'https://api.clerk.test', |
| 10 | + secretKey: 'deadbeef', |
| 11 | + }); |
| 12 | + |
| 13 | + describe('getHandshakePayload', () => { |
| 14 | + it('successfully fetches the handshake payload with a valid nonce', async () => { |
| 15 | + const mockHandshakePayload = { |
| 16 | + directives: ['directive1', 'directive2'], |
| 17 | + }; |
| 18 | + |
| 19 | + server.use( |
| 20 | + http.get( |
| 21 | + 'https://api.clerk.test/v1/clients/handshake_payload', |
| 22 | + validateHeaders(({ request }) => { |
| 23 | + const url = new URL(request.url); |
| 24 | + expect(url.searchParams.get('nonce')).toBe('test-nonce-123'); |
| 25 | + return HttpResponse.json(mockHandshakePayload); |
| 26 | + }), |
| 27 | + ), |
| 28 | + ); |
| 29 | + |
| 30 | + const response = await apiClient.clients.getHandshakePayload({ |
| 31 | + nonce: 'test-nonce-123', |
| 32 | + }); |
| 33 | + |
| 34 | + expect(response.directives).toEqual(['directive1', 'directive2']); |
| 35 | + expect(response.directives.length).toBe(2); |
| 36 | + }); |
| 37 | + |
| 38 | + it('handles error responses correctly', async () => { |
| 39 | + const mockErrorPayload = { |
| 40 | + code: 'invalid_nonce', |
| 41 | + message: 'Invalid nonce provided', |
| 42 | + long_message: 'The nonce provided is invalid or has expired', |
| 43 | + meta: { param_name: 'nonce' }, |
| 44 | + }; |
| 45 | + const traceId = 'trace_id_handshake'; |
| 46 | + |
| 47 | + server.use( |
| 48 | + http.get( |
| 49 | + 'https://api.clerk.test/v1/clients/handshake_payload', |
| 50 | + validateHeaders(() => { |
| 51 | + return HttpResponse.json( |
| 52 | + { errors: [mockErrorPayload], clerk_trace_id: traceId }, |
| 53 | + { status: 400, headers: { 'cf-ray': traceId } }, |
| 54 | + ); |
| 55 | + }), |
| 56 | + ), |
| 57 | + ); |
| 58 | + |
| 59 | + const errResponse = await apiClient.clients.getHandshakePayload({ nonce: 'invalid-nonce' }).catch(err => err); |
| 60 | + |
| 61 | + expect(errResponse.clerkTraceId).toBe(traceId); |
| 62 | + expect(errResponse.status).toBe(400); |
| 63 | + expect(errResponse.errors[0].code).toBe('invalid_nonce'); |
| 64 | + expect(errResponse.errors[0].message).toBe('Invalid nonce provided'); |
| 65 | + expect(errResponse.errors[0].longMessage).toBe('The nonce provided is invalid or has expired'); |
| 66 | + expect(errResponse.errors[0].meta.paramName).toBe('nonce'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('requires a nonce parameter', async () => { |
| 70 | + server.use( |
| 71 | + http.get( |
| 72 | + 'https://api.clerk.test/v1/clients/handshake_payload', |
| 73 | + validateHeaders(() => { |
| 74 | + return HttpResponse.json( |
| 75 | + { |
| 76 | + errors: [ |
| 77 | + { |
| 78 | + code: 'missing_parameter', |
| 79 | + message: 'Missing required parameter', |
| 80 | + long_message: 'The nonce parameter is required', |
| 81 | + meta: { param_name: 'nonce' }, |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + { status: 400 }, |
| 86 | + ); |
| 87 | + }), |
| 88 | + ), |
| 89 | + ); |
| 90 | + |
| 91 | + // @ts-expect-error Testing invalid input |
| 92 | + const errResponse = await apiClient.clients.getHandshakePayload({}).catch(err => err); |
| 93 | + |
| 94 | + expect(errResponse.status).toBe(400); |
| 95 | + expect(errResponse.errors[0].code).toBe('missing_parameter'); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments