-
Notifications
You must be signed in to change notification settings - Fork 454
test(shared): add query-client hotload compatibility regression #8618
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
+193
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8ec94f7
test(shared): add query-client hotload compatibility regression
jacekradko c3759d3
test(shared): note transitive use of __internal_attemptToEnableEnviro…
jacekradko 573c23b
test(shared): type fapiRequest mock to accept request arg
jacekradko e6c9543
Merge branch 'main' into jacek/query-client-compat-regression
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 @@ | ||
| --- | ||
| --- |
191 changes: 191 additions & 0 deletions
191
packages/shared/src/react/hooks/__tests__/useOrganizationList.hotload-compat.spec.tsx
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,191 @@ | ||
| import type { QueryClient } from '@tanstack/query-core'; | ||
| import { QueryClient as TanstackQueryClient } from '@tanstack/query-core'; | ||
| import { renderHook, waitFor } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const mockState = vi.hoisted(() => ({ | ||
| clerk: undefined as any, | ||
| user: undefined as any, | ||
| })); | ||
|
|
||
| vi.mock('../../contexts', () => ({ | ||
| useAssertWrappedByClerkProvider: () => {}, | ||
| useClerkInstanceContext: () => mockState.clerk, | ||
| useInitialStateContext: () => undefined, | ||
| })); | ||
|
|
||
| vi.mock('../base/useUserBase', () => ({ | ||
| useUserBase: () => mockState.user, | ||
| })); | ||
|
|
||
| // Models pre-PR #8434 @clerk/shared, which reads the hotloaded Clerk.js | ||
| // `__internal_queryClient` bridge and waits for `queryClientStatus`. | ||
| vi.mock('../../query/use-clerk-query-client', () => { | ||
| type RecursiveMock = { | ||
| (...args: unknown[]): RecursiveMock; | ||
| } & { | ||
| readonly [key in string | symbol]: RecursiveMock; | ||
| }; | ||
|
|
||
| function createRecursiveProxy(label: string): RecursiveMock { | ||
| const callableTarget = function noop(): void {}; | ||
| const handler: ProxyHandler<typeof callableTarget> = { | ||
| get(_target, prop) { | ||
| if (prop === 'then') { | ||
| return undefined; | ||
| } | ||
| if (prop === 'toString') { | ||
| return () => `[${label}]`; | ||
| } | ||
| if (prop === Symbol.toPrimitive) { | ||
| return () => 0; | ||
| } | ||
| return self; | ||
| }, | ||
| apply() { | ||
| return self; | ||
| }, | ||
| construct() { | ||
| return self as unknown as object; | ||
| }, | ||
| has() { | ||
| return false; | ||
| }, | ||
| set() { | ||
| return false; | ||
| }, | ||
| }; | ||
|
|
||
| const self = new Proxy(callableTarget, handler) as unknown as RecursiveMock; | ||
| return self; | ||
| } | ||
|
|
||
| const mockQueryClient = createRecursiveProxy('LegacyClerkMockQueryClient') as unknown as QueryClient; | ||
| const isLegacyQueryClient = (value: unknown): value is { __tag: 'clerk-rq-client'; client: QueryClient } => | ||
| typeof value === 'object' && | ||
| value !== null && | ||
| '__tag' in value && | ||
| (value as { __tag?: string }).__tag === 'clerk-rq-client'; | ||
|
|
||
| const useClerkQueryClient = (): [QueryClient, boolean] => { | ||
| const clerk = mockState.clerk; | ||
| const queryClient = clerk.__internal_queryClient; | ||
| const [queryClientLoaded, setQueryClientLoaded] = React.useState(isLegacyQueryClient(queryClient)); | ||
|
|
||
| React.useEffect(() => { | ||
| const setLoaded = () => setQueryClientLoaded(true); | ||
| clerk.on('queryClientStatus', setLoaded); | ||
| return () => clerk.off('queryClientStatus', setLoaded); | ||
| }, [clerk]); | ||
|
|
||
| const isLoaded = queryClientLoaded && isLegacyQueryClient(queryClient); | ||
| return [isLoaded ? queryClient.client : mockQueryClient, isLoaded]; | ||
| }; | ||
|
|
||
| return { useClerkQueryClient }; | ||
| }); | ||
|
|
||
| import { useOrganizationList } from '../useOrganizationList'; | ||
|
|
||
| function createHotloadedClerkQueryClientShim() { | ||
| const listeners = new Set<(status: 'ready') => void>(); | ||
| let isResolving = false; | ||
| let queryClient: { __tag: 'clerk-rq-client'; client: TanstackQueryClient } | undefined; | ||
|
|
||
| return { | ||
| loaded: true, | ||
| telemetry: { record: vi.fn() }, | ||
| setActive: vi.fn(), | ||
| createOrganization: vi.fn(), | ||
| // Reached transitively via useAttemptToEnableOrganizations. | ||
| __internal_attemptToEnableEnvironmentSetting: vi.fn(), | ||
| get __internal_queryClient() { | ||
| if (!queryClient && !isResolving) { | ||
| isResolving = true; | ||
| void Promise.resolve().then(() => { | ||
| queryClient = { | ||
| __tag: 'clerk-rq-client', | ||
| client: new TanstackQueryClient({ | ||
| defaultOptions: { | ||
| queries: { | ||
| retry: false, | ||
| staleTime: Infinity, | ||
| refetchOnMount: false, | ||
| refetchOnReconnect: false, | ||
| refetchOnWindowFocus: false, | ||
| }, | ||
| }, | ||
| }), | ||
| }; | ||
| listeners.forEach(listener => listener('ready')); | ||
| }); | ||
| } | ||
| return queryClient; | ||
| }, | ||
| on: vi.fn((event: string, listener: (status: 'ready') => void) => { | ||
| if (event === 'queryClientStatus') { | ||
| listeners.add(listener); | ||
| } | ||
| }), | ||
| off: vi.fn((event: string, listener: (status: 'ready') => void) => { | ||
| if (event === 'queryClientStatus') { | ||
| listeners.delete(listener); | ||
| } | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockState.clerk = undefined; | ||
| mockState.user = undefined; | ||
| }); | ||
|
|
||
| describe('useOrganizationList hotload compatibility', () => { | ||
| it('leaves the legacy mock query-client state and requests organization memberships', async () => { | ||
| const fapiRequest = vi.fn((_request: unknown) => | ||
| Promise.resolve({ | ||
| data: [], | ||
| total_count: 0, | ||
| }), | ||
| ); | ||
| const membershipRequest = vi.fn((params?: { initialPage?: number; pageSize?: number }) => { | ||
| return fapiRequest({ | ||
| path: '/me/organization_memberships', | ||
| method: 'GET', | ||
| search: params, | ||
| }); | ||
| }); | ||
|
|
||
| mockState.clerk = createHotloadedClerkQueryClientShim(); | ||
| mockState.user = { | ||
| id: 'user_123', | ||
| getOrganizationMemberships: membershipRequest, | ||
| }; | ||
|
|
||
| const { result } = renderHook(() => | ||
| useOrganizationList({ | ||
| userMemberships: { | ||
| pageSize: 2, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| expect(result.current.isLoaded).toBe(true); | ||
| expect(result.current.userMemberships.isLoading).toBe(true); | ||
| expect(membershipRequest).not.toHaveBeenCalled(); | ||
|
|
||
| await waitFor(() => expect(membershipRequest).toHaveBeenCalledTimes(1)); | ||
| expect(membershipRequest).toHaveBeenCalledWith({ initialPage: 1, pageSize: 2 }); | ||
| expect(fapiRequest).toHaveBeenCalledWith({ | ||
| path: '/me/organization_memberships', | ||
| method: 'GET', | ||
| search: { initialPage: 1, pageSize: 2 }, | ||
| }); | ||
| await waitFor(() => expect(result.current.userMemberships.isLoading).toBe(false)); | ||
|
|
||
| expect(result.current.userMemberships.isFetching).toBe(false); | ||
| expect(result.current.userMemberships.count).toBe(0); | ||
| }); | ||
| }); | ||
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.