Skip to content

Commit

Permalink
test(core): Move new test to last of setActive
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Jul 26, 2024
1 parent 1228b43 commit 01e1f3b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
78 changes: 38 additions & 40 deletions packages/clerk-js/src/core/__tests__/clerk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,46 +257,6 @@ describe('Clerk singleton', () => {
await sut.setActive({ session: mockSession as any as ActiveSessionResource });
});

it('sets active organization by slug', async () => {
const mockSession2 = {
id: '1',
status: 'active',
user: {
organizationMemberships: [
{
id: 'orgmem_id',
organization: {
id: 'org_id',
slug: 'some-org-slug',
},
},
],
},
touch: jest.fn(),
getToken: jest.fn(),
};
mockClientFetch.mockReturnValue(Promise.resolve({ activeSessions: [mockSession2] }));
const sut = new Clerk(productionPublishableKey);
await sut.load();

mockSession2.touch.mockImplementationOnce(() => {
sut.session = mockSession2 as any;
return Promise.resolve();
});
mockSession2.getToken.mockImplementation(() => {
return 'mocked-token';
});

await sut.setActive({ organization: 'some-org-slug' });

await waitFor(() => {
expect(mockSession2.touch).toHaveBeenCalled();
expect(mockSession2.getToken).toHaveBeenCalled();
expect((mockSession2 as any as ActiveSessionResource)?.lastActiveOrganizationId).toEqual('org_id');
expect(sut.session).toMatchObject(mockSession2);
});
});

it('calls __unstable__onAfterSetActive after beforeEmit and session.touch', async () => {
const beforeEmitMock = jest.fn();
mockSession.touch.mockReturnValueOnce(Promise.resolve());
Expand Down Expand Up @@ -387,6 +347,44 @@ describe('Clerk singleton', () => {
});
});

it('sets active organization by slug', async () => {
const mockSession2 = {
id: '1',
status: 'active',
user: {
organizationMemberships: [
{
id: 'orgmem_id',
organization: {
id: 'org_id',
slug: 'some-org-slug',
},
},
],
},
touch: jest.fn(),
getToken: jest.fn(),
};
mockClientFetch.mockReturnValue(Promise.resolve({ activeSessions: [mockSession2] }));
const sut = new Clerk(productionPublishableKey);
await sut.load();

mockSession2.touch.mockImplementationOnce(() => {
sut.session = mockSession2 as any;
return Promise.resolve();
});
mockSession2.getToken.mockImplementation(() => 'mocked-token');

await sut.setActive({ organization: 'some-org-slug' });

await waitFor(() => {
expect(mockSession2.touch).toHaveBeenCalled();
expect(mockSession2.getToken).toHaveBeenCalled();
expect((mockSession2 as any as ActiveSessionResource)?.lastActiveOrganizationId).toEqual('org_id');
expect(sut.session).toMatchObject(mockSession2);
});
});

mockNativeRuntime(() => {
it('calls session.touch in a non-standard browser', async () => {
mockClientFetch.mockReturnValue(Promise.resolve({ activeSessions: [mockSession] }));
Expand Down
3 changes: 1 addition & 2 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,8 @@ export class Clerk implements ClerkInterface {
// However, if the `organization` parameter is not given (i.e. `undefined`), we want
// to keep the organization id that the session had.
const shouldSwitchOrganization = organization !== undefined;
console.log('hello');

if (newSession && shouldSwitchOrganization) {
console.log('world', organization);
const organizationIdOrSlug = typeof organization === 'string' ? organization : organization?.id;

if (isOrganizationId(organizationIdOrSlug!)) {
Expand Down

0 comments on commit 01e1f3b

Please sign in to comment.