Skip to content

Commit

Permalink
test(clerk-js): Update tests for Client singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
octoper committed Nov 2, 2023
1 parent 0a58ff2 commit f2b57ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
28 changes: 22 additions & 6 deletions packages/clerk-js/src/core/resources/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import type { ClientJSON } from '@clerk/types';
import { createSession } from '../test/fixtures';
import { BaseResource, Client } from './internal';

export const mockJwt =
'eyJhbGciOiJSUzI1NiIsImtpZCI6Imluc18yR0lvUWhiVXB5MGhYN0IyY1ZrdVRNaW5Yb0QiLCJ0eXAiOiJKV1QifQ.eyJhenAiOiJodHRwczovL2FjY291bnRzLmluc3BpcmVkLnB1bWEtNzQubGNsLmRldiIsImV4cCI6MTY2NjY0ODMxMCwiaWF0IjoxNjY2NjQ4MjUwLCJpc3MiOiJodHRwczovL2NsZXJrLmluc3BpcmVkLnB1bWEtNzQubGNsLmRldiIsIm5iZiI6MTY2NjY0ODI0MCwic2lkIjoic2Vzc18yR2JEQjRlbk5kQ2E1dlMxenBDM1h6Zzl0SzkiLCJzdWIiOiJ1c2VyXzJHSXBYT0VwVnlKdzUxcmtabjlLbW5jNlN4ciJ9.n1Usc-DLDftqA0Xb-_2w8IGs4yjCmwc5RngwbSRvwevuZOIuRoeHmE2sgCdEvjfJEa7ewL6EVGVcM557TWPW--g_J1XQPwBy8tXfz7-S73CEuyRFiR97L2AHRdvRtvGtwR-o6l8aHaFxtlmfWbQXfg4kFJz2UGe9afmh3U9-f_4JOZ5fa3mI98UMy1-bo20vjXeWQ9aGrqaxHQxjnzzC-1Kpi5LdPvhQ16H0dPB8MHRTSM5TAuLKTpPV7wqixmbtcc2-0k6b9FKYZNqRVTaIyV-lifZloBvdzlfOF8nW1VVH_fx-iW5Q3hovHFcJIULHEC1kcAYTubbxzpgeVQepGg';

describe('Client Singleton', () => {
it('destroy', async () => {
const session = createSession({});
const session = createSession();
const clientObjectJSON: ClientJSON = {
object: 'client',
id: 'test_id',
Expand All @@ -17,14 +14,33 @@ describe('Client Singleton', () => {
sign_in: null,
sign_up: null,
sessions: [session],
created_at: jest.now(),
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 clientObjectDeletedJSON = {
id: 'test_id_deleted',
status: 'ended',
last_active_session_id: null,
sign_in: null,
sign_up: null,
sessions: [destroyedSession],
created_at: jest.now() - 1000,
updated_at: jest.now(),
};

// @ts-expect-error This is a private method that we are mocking
BaseResource._fetch = jest.fn().mockReturnValue(
Promise.resolve({
response: clientObjectJSON,
client: null,
response: clientObjectDeletedJSON,
}),
);

Expand Down
24 changes: 13 additions & 11 deletions packages/clerk-js/src/core/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type WithUserParams = Omit<
organization_memberships?: Array<string | OrgParams>;
};

type WithSessionParams = Partial<SessionJSON>;

export const getOrganizationId = (orgParams: OrgParams) => orgParams?.id || orgParams?.name || 'test_id';

export const createOrganizationMembership = (params: OrgParams): OrganizationMembershipJSON => {
Expand Down Expand Up @@ -164,17 +166,17 @@ export const createUser = (params: WithUserParams): UserJSON => {
return res;
};

export const createSession = (params: WithUserParams) => {
const user = createUser(params);
export const createSession = (sessionParams: WithSessionParams = {}, userParams: WithUserParams = {}) => {
const user = createUser(userParams);
return {
object: 'session',
id: 'session_1',
status: 'active',
expire_at: jest.now() + 5000,
abandon_at: jest.now() + 5000,
last_active_at: jest.now(),
last_active_organization_id: null,
actor: null,
id: sessionParams.id,
status: sessionParams.status,
expire_at: sessionParams.expire_at || jest.now() + 5000,
abandon_at: sessionParams.abandon_at,
last_active_at: sessionParams.last_active_at || jest.now(),
last_active_organization_id: sessionParams.last_active_organization_id,
actor: sessionParams.actor,
user: createUser({}),
public_user_data: {
first_name: user.first_name,
Expand All @@ -184,8 +186,8 @@ export const createSession = (params: WithUserParams) => {
identifier: user.email_addresses.find(e => e.id === user.primary_email_address_id)?.email_address || '',
profile_image_url: user.profile_image_url,
},
created_at: 1697449745962,
updated_at: 1698763366892,
created_at: sessionParams.created_at || jest.now() - 1000,
updated_at: sessionParams.updated_at || jest.now(),
last_active_token: {
object: 'token',
jwt: mockJwt,
Expand Down

0 comments on commit f2b57ba

Please sign in to comment.