From b06e771d58f9222841849df3a2a0d5a257eaf046 Mon Sep 17 00:00:00 2001 From: Paul Asjes Date: Fri, 27 Dec 2024 16:51:29 +0200 Subject: [PATCH] Add onSuccess test --- __tests__/authkit-callback-route.spec.ts | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/__tests__/authkit-callback-route.spec.ts b/__tests__/authkit-callback-route.spec.ts index f074643..2851ef1 100644 --- a/__tests__/authkit-callback-route.spec.ts +++ b/__tests__/authkit-callback-route.spec.ts @@ -232,5 +232,30 @@ describe('authkit-callback-route', () => { expect(response.status).toBe(500); }); + + it('should call onSuccess if provided', async () => { + const mockAuthResponse = { + accessToken: 'access123', + refreshToken: 'refresh123', + user: { id: 'user_123' }, + oauthTokens: { + access_token: 'access123', + refresh_token: 'refresh123', + expires_at: 1719811200, + scopes: ['foo', 'bar'], + }, + }; + + (workos.userManagement.authenticateWithCode as jest.Mock).mockResolvedValue(mockAuthResponse); + + // Set up request with code + request.nextUrl.searchParams.set('code', 'test-code'); + + const onSuccess = jest.fn(); + const handler = handleAuth({ onSuccess: onSuccess }); + await handler(request); + + expect(onSuccess).toHaveBeenCalledWith(mockAuthResponse); + }); }); });