-
Notifications
You must be signed in to change notification settings - Fork 402
chore(clerk-js): Add test coverage for memoizeStateListenerCallback #7152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2afd188
chore(clerk-js): Add test coverage for memoizeStateListenerCallback.
jacekradko 789f5d3
chore: empty changeset
jacekradko bf619b3
wip
jacekradko 18bbe1a
Merge branch 'main' into chore/add-test-coverage
jacekradko d517cc5
Merge branch 'main' into chore/add-test-coverage
jacekradko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
| --- | ||
| --- | ||
180 changes: 141 additions & 39 deletions
180
packages/clerk-js/src/utils/__tests__/memoizeStateListenerCallback.test.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,42 +1,144 @@ | ||
| // TODO: jest fails because of a circular dependency on Client -> Base -> Client | ||
| // This circular dep is a known issue we plan to address soon. Enable the tests then | ||
| import { describe, it } from 'vitest'; | ||
| import type { Resources, UserJSON } from '@clerk/shared/types'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| describe.skip('memoizeStateListenerCallback', () => { | ||
| it.skip('runs', () => { | ||
| // TODO | ||
| import { User } from '../../core/resources/User'; | ||
| import { memoizeListenerCallback } from '../memoizeStateListenerCallback'; | ||
|
|
||
| function createTestUser(overrides: Partial<UserJSON> = {}): User { | ||
| const defaultUserJSON: UserJSON = { | ||
| email_addresses: [], | ||
| external_accounts: [], | ||
| first_name: 'clerk', | ||
| id: 'u1', | ||
| phone_numbers: [], | ||
| updated_at: 1, | ||
| web3_wallets: [], | ||
| } as unknown as UserJSON; | ||
|
|
||
| return new User({ ...defaultUserJSON, ...overrides } as unknown as UserJSON); | ||
| } | ||
|
|
||
| describe('memoizeStateListenerCallback', () => { | ||
| it('returns same user ref if user obj state has not changed', () => { | ||
| const user1 = createTestUser(); | ||
| const user2 = createTestUser(); | ||
|
|
||
| let calledWith: Resources | undefined; | ||
| const listener = memoizeListenerCallback( | ||
| vi.fn((e: Resources) => { | ||
| calledWith = e; | ||
| }), | ||
| ); | ||
|
|
||
| listener({ client: null, organization: null, session: null, user: user1 }); | ||
| listener({ client: null, organization: null, session: null, user: user2 }); | ||
|
|
||
| expect(calledWith?.user).toBe(user1); | ||
| }); | ||
| }); | ||
|
|
||
| // import { Resources, UserJSON } from '@clerk/shared/types'; | ||
| // | ||
| // const frontEndApi = ''; | ||
| // const path = ''; | ||
| // | ||
| // describe('memoizeStateListenerCallback', () => { | ||
| // it('returns same user ref if user obj state has not changed', () => { | ||
| // const user1 = new User(frontEndApi, path, { | ||
| // id: 'u1', | ||
| // updated_at: 1, | ||
| // first_name: 'clerk', | ||
| // } as UserJSON); | ||
| // | ||
| // const user2 = new User(frontEndApi, path, { | ||
| // id: 'u1', | ||
| // updated_at: 1, | ||
| // first_name: 'clerk', | ||
| // } as UserJSON); | ||
| // | ||
| // let calledWith: any; | ||
| // const listener = memoizeListenerCallback( | ||
| // jest.fn((e: Resources) => { | ||
| // console.log(e); | ||
| // calledWith = e.user; | ||
| // }), | ||
| // ); | ||
| // | ||
| // listener(({ user: user1 } as any) as Resources); | ||
| // listener(({ user: user2 } as any) as Resources); | ||
| // expect(calledWith).toBe(user1); | ||
| // }); | ||
| // }); | ||
| it('returns new user ref if user obj state has changed', () => { | ||
| const user1 = createTestUser(); | ||
| const user2 = createTestUser({ updated_at: 2 }); | ||
|
|
||
| let calledWith: Resources | undefined; | ||
| const listener = memoizeListenerCallback( | ||
| vi.fn((e: Resources) => { | ||
| calledWith = e; | ||
| }), | ||
| ); | ||
|
|
||
| listener({ client: null, organization: null, session: null, user: user1 }); | ||
| listener({ client: null, organization: null, session: null, user: user2 }); | ||
|
|
||
| expect(calledWith?.user).toBe(user2); | ||
| }); | ||
|
|
||
| it('returns new user ref if user id has changed', () => { | ||
| const user1 = createTestUser(); | ||
| const user2 = createTestUser({ id: 'u2' }); | ||
|
|
||
| let calledWith: Resources | undefined; | ||
| const listener = memoizeListenerCallback( | ||
| vi.fn((e: Resources) => { | ||
| calledWith = e; | ||
| }), | ||
| ); | ||
|
|
||
| listener({ client: null, organization: null, session: null, user: user1 }); | ||
| listener({ client: null, organization: null, session: null, user: user2 }); | ||
|
|
||
| expect(calledWith?.user).toBe(user2); | ||
| }); | ||
|
|
||
| it('handles user becoming null', () => { | ||
| const user1 = createTestUser(); | ||
|
|
||
| let calledWith: Resources | undefined; | ||
| const listener = memoizeListenerCallback( | ||
| vi.fn((e: Resources) => { | ||
| calledWith = e; | ||
| }), | ||
| ); | ||
|
|
||
| listener({ client: null, organization: null, session: null, user: user1 }); | ||
| listener({ client: null, organization: null, session: null, user: null }); | ||
|
|
||
| expect(calledWith?.user).toBe(null); | ||
| }); | ||
|
|
||
| it('handles user transitioning from null to defined', () => { | ||
| const user1 = createTestUser(); | ||
|
|
||
| let calledWith: Resources | undefined; | ||
| const listener = memoizeListenerCallback( | ||
| vi.fn((e: Resources) => { | ||
| calledWith = e; | ||
| }), | ||
| ); | ||
|
|
||
| listener({ client: null, organization: null, session: null, user: null }); | ||
| listener({ client: null, organization: null, session: null, user: user1 }); | ||
|
|
||
| expect(calledWith?.user).toBe(user1); | ||
| }); | ||
|
|
||
| it('calls the callback function each time', () => { | ||
| const user1 = createTestUser(); | ||
|
|
||
| const mockCallback = vi.fn(); | ||
| const listener = memoizeListenerCallback(mockCallback); | ||
|
|
||
| listener({ client: null, organization: null, session: null, user: user1 }); | ||
| listener({ client: null, organization: null, session: null, user: user1 }); | ||
|
|
||
| expect(mockCallback).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('treats null and undefined as different values (null to undefined)', () => { | ||
| let calledWith: Resources | undefined; | ||
| const listener = memoizeListenerCallback( | ||
| vi.fn((e: Resources) => { | ||
| calledWith = e; | ||
| }), | ||
| ); | ||
|
|
||
| listener({ client: null, organization: null, session: null, user: null }); | ||
| listener({ client: null, organization: null, session: null, user: undefined }); | ||
|
|
||
| expect(calledWith?.user).toBe(undefined); | ||
| }); | ||
|
|
||
| it('treats null and undefined as different values (undefined to null)', () => { | ||
| let calledWith: Resources | undefined; | ||
| const listener = memoizeListenerCallback( | ||
| vi.fn((e: Resources) => { | ||
| calledWith = e; | ||
| }), | ||
| ); | ||
|
|
||
| listener({ client: null, organization: null, session: null, user: undefined }); | ||
| listener({ client: null, organization: null, session: null, user: null }); | ||
|
|
||
| expect(calledWith?.user).toBe(null); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.