-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-js): Re-initialize Client singleton instance on Client dest…
…roy (#1913) * fix(clerk-js): Re-initialize Client singleton on Client destroy * fix(clerk-js): Assign default values instead of re-initializing Client class * chore(repo): Adds changeset * chore(repo): Adds changeset * chore(clerk-js): Fix typo in test * test(clerk-js): Added test for Client singleton destroy * test(clerk-js): Update tests for Client singleton * test(clerk-js): Update tests for Client singleton & created new fixtures
- Loading branch information
Showing
4 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
--- | ||
|
||
Re-initialize the Client to default values when is destroyed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { ClientJSON } from '@clerk/types'; | ||
|
||
import { createSession, createSignIn, createSignUp, createUser } from '../test/fixtures'; | ||
import { BaseResource, Client } from './internal'; | ||
|
||
describe('Client Singleton', () => { | ||
it('destroy', async () => { | ||
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: 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, | ||
}, | ||
user, | ||
); | ||
|
||
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({ | ||
client: null, | ||
response: clientObjectDeletedJSON, | ||
}), | ||
); | ||
|
||
const client = Client.getInstance().fromJSON(clientObjectJSON); | ||
expect(client.sessions.length).toBe(1); | ||
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({ | ||
method: 'DELETE', | ||
path: `/client`, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters