diff --git a/packages/clerk-js/src/core/resources/Client.test.ts b/packages/clerk-js/src/core/resources/Client.test.ts index ba6ceeef3c..20f460c310 100644 --- a/packages/clerk-js/src/core/resources/Client.test.ts +++ b/packages/clerk-js/src/core/resources/Client.test.ts @@ -1,29 +1,33 @@ import type { ClientJSON } from '@clerk/types'; -import { createSession } from '../test/fixtures'; +import { createSession, createSignIn, createSignUp, createUser } from '../test/fixtures'; import { BaseResource, Client } from './internal'; describe('Client Singleton', () => { it('destroy', async () => { - const session = createSession(); + const user = createUser({ first_name: 'John', last_name: 'Doe', id: 'user_1' }); + const session = createSession({ id: 'session_1' }, user); const clientObjectJSON: ClientJSON = { object: 'client', id: 'test_id', status: 'active', last_active_session_id: 'test_session_id', - sign_in: null, - sign_up: null, + sign_in: createSignIn({ id: 'test_sign_in_id' }, user), + sign_up: createSignUp({ id: 'test_sign_up_id' }), // This is only for testing purposes, this will never happen sessions: [session], created_at: jest.now() - 1000, updated_at: jest.now(), }; - const destroyedSession = createSession({ - id: 'test_session_id', - abandon_at: jest.now(), - status: 'ended', - last_active_token: undefined, - }); + const destroyedSession = createSession( + { + id: 'test_session_id', + abandon_at: jest.now(), + status: 'ended', + last_active_token: undefined, + }, + user, + ); const clientObjectDeletedJSON = { id: 'test_id_deleted', @@ -49,6 +53,8 @@ describe('Client Singleton', () => { expect(client.createdAt).not.toBeNull(); expect(client.updatedAt).not.toBeNull(); expect(client.lastActiveSessionId).not.toBeNull(); + expect(client.signUp.id).toBe('test_sign_up_id'); + expect(client.signIn.id).toBe('test_sign_in_id'); await client.destroy(); @@ -56,6 +62,8 @@ describe('Client Singleton', () => { expect(client.createdAt).toBeNull(); expect(client.updatedAt).toBeNull(); expect(client.lastActiveSessionId).toBeNull(); + expect(client.signUp.id).toBeUndefined(); + expect(client.signIn.id).toBeUndefined(); // @ts-expect-error This is a private method that we are mocking expect(BaseResource._fetch).toHaveBeenCalledWith({ diff --git a/packages/clerk-js/src/core/test/fixtures.ts b/packages/clerk-js/src/core/test/fixtures.ts index 75dae9b1f9..00db010a70 100644 --- a/packages/clerk-js/src/core/test/fixtures.ts +++ b/packages/clerk-js/src/core/test/fixtures.ts @@ -8,6 +8,8 @@ import type { OrganizationPermission, PhoneNumberJSON, SessionJSON, + SignInJSON, + SignUpJSON, UserJSON, } from '@clerk/types'; @@ -166,8 +168,7 @@ export const createUser = (params: WithUserParams): UserJSON => { return res; }; -export const createSession = (sessionParams: WithSessionParams = {}, userParams: WithUserParams = {}) => { - const user = createUser(userParams); +export const createSession = (sessionParams: WithSessionParams = {}, user: Partial = {}) => { return { object: 'session', id: sessionParams.id, @@ -183,7 +184,7 @@ export const createSession = (sessionParams: WithSessionParams = {}, userParams: last_name: user.last_name, image_url: user.image_url, has_image: user.has_image, - identifier: user.email_addresses.find(e => e.id === user.primary_email_address_id)?.email_address || '', + identifier: user.email_addresses?.find(e => e.id === user.primary_email_address_id)?.email_address || '', profile_image_url: user.profile_image_url, }, created_at: sessionParams.created_at || jest.now() - 1000, @@ -195,6 +196,56 @@ export const createSession = (sessionParams: WithSessionParams = {}, userParams: } as SessionJSON; }; +export const createSignIn = (signInParams: Partial = {}, user: Partial = {}) => { + return { + id: signInParams.id, + created_session_id: signInParams.created_session_id, + status: signInParams.status, + first_factor_verification: signInParams.first_factor_verification, + identifier: signInParams.identifier, + object: 'sign_in', + second_factor_verification: signInParams.second_factor_verification, + supported_external_accounts: signInParams.supported_external_accounts, + supported_first_factors: signInParams.supported_first_factors, + supported_identifiers: signInParams.supported_identifiers, + supported_second_factors: signInParams.supported_second_factors, + user_data: { + first_name: user.first_name, + last_name: user.last_name, + image_url: user.image_url, + has_image: user.has_image, + profile_image_url: user.profile_image_url, + }, + } as SignInJSON; +}; + +export const createSignUp = (signUpParams: Partial = {}) => { + return { + id: signUpParams.id, + created_session_id: signUpParams.created_session_id, + status: signUpParams.status, + abandon_at: signUpParams.abandon_at, + created_user_id: signUpParams.created_user_id, + email_address: signUpParams.email_address, + external_account: signUpParams.external_account, + external_account_strategy: signUpParams.external_account_strategy, + first_name: signUpParams.first_name, + has_password: signUpParams.has_password, + last_name: signUpParams.last_name, + missing_fields: signUpParams.missing_fields, + object: 'sign_up', + optional_fields: signUpParams.optional_fields, + phone_number: signUpParams.phone_number, + required_fields: signUpParams.required_fields, + supported_external_accounts: signUpParams.supported_external_accounts, + unsafe_metadata: signUpParams.unsafe_metadata, + unverified_fields: signUpParams.unverified_fields, + username: signUpParams.username, + verifications: signUpParams.verifications, + web3_wallet: signUpParams.web3_wallet, + } as SignUpJSON; +}; + export const clerkMock = () => { return { getFapiClient: jest.fn().mockReturnValue({