Skip to content

Commit

Permalink
test(clerk-js): Update tests for Client singleton & created new fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
octoper committed Nov 2, 2023
1 parent f2b57ba commit f78a0be
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
28 changes: 18 additions & 10 deletions packages/clerk-js/src/core/resources/Client.test.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -49,13 +53,17 @@ 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();

expect(client.sessions.length).toBe(0);
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({
Expand Down
57 changes: 54 additions & 3 deletions packages/clerk-js/src/core/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
OrganizationPermission,
PhoneNumberJSON,
SessionJSON,
SignInJSON,
SignUpJSON,
UserJSON,
} from '@clerk/types';

Expand Down Expand Up @@ -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<UserJSON> = {}) => {
return {
object: 'session',
id: sessionParams.id,
Expand All @@ -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,
Expand All @@ -195,6 +196,56 @@ export const createSession = (sessionParams: WithSessionParams = {}, userParams:
} as SessionJSON;
};

export const createSignIn = (signInParams: Partial<SignInJSON> = {}, user: Partial<UserJSON> = {}) => {
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<SignUpJSON> = {}) => {
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({
Expand Down

0 comments on commit f78a0be

Please sign in to comment.