-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
766 additions
and
55 deletions.
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
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,104 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { http, WagmiProvider, createConfig } from 'wagmi'; | ||
import { DefaultOnchainKitProviders } from './DefaultOnchainKitProviders'; | ||
import { useProviderDependencies } from './internal/hooks/useProviderDependencies'; | ||
|
||
const queryClient = new QueryClient(); | ||
const wagmiConfig = createConfig({ | ||
chains: [ | ||
{ | ||
id: 1, | ||
name: 'Mock Chain', | ||
nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 }, | ||
rpcUrls: { default: { http: ['http://localhost'] } }, | ||
}, | ||
], | ||
connectors: [], | ||
transports: { [1]: http() }, | ||
}); | ||
|
||
vi.mock('wagmi', async (importOriginal) => { | ||
const actual = await importOriginal<typeof import('wagmi')>(); | ||
return { | ||
...actual, | ||
WagmiProvider: vi.fn(({ children }) => ( | ||
<div data-testid="wagmi-provider">{children}</div> | ||
)), | ||
}; | ||
}); | ||
|
||
vi.mock('@tanstack/react-query', async (importOriginal) => { | ||
const actual = await importOriginal<typeof import('@tanstack/react-query')>(); | ||
return { | ||
...actual, | ||
QueryClientProvider: vi.fn(({ children }) => ( | ||
<div data-testid="query-client-provider">{children}</div> | ||
)), | ||
}; | ||
}); | ||
|
||
vi.mock('./internal/hooks/useProviderDependencies', () => ({ | ||
useProviderDependencies: vi.fn(() => ({ | ||
providedWagmiConfig: vi.fn(), | ||
providedQueryClient: vi.fn(), | ||
})), | ||
})); | ||
|
||
describe('DefaultOnchainKitProviders', () => { | ||
beforeEach(() => { | ||
(useProviderDependencies as Mock).mockReturnValue({ | ||
providedWagmiConfig: false, | ||
providedQueryClient: false, | ||
}); | ||
}); | ||
|
||
it('should wrap children in default providers', () => { | ||
render( | ||
<DefaultOnchainKitProviders> | ||
<div>Test Child</div> | ||
</DefaultOnchainKitProviders>, | ||
); | ||
|
||
expect(screen.getByText('Test Child')).toBeInTheDocument(); | ||
expect(screen.queryAllByTestId('wagmi-provider')).toHaveLength(1); | ||
expect(screen.queryAllByTestId('query-client-provider')).toHaveLength(1); | ||
}); | ||
|
||
it('should not render duplicate default providers when a wagmi provider already exists', () => { | ||
(useProviderDependencies as Mock).mockReturnValue({ | ||
providedWagmiConfig: wagmiConfig, | ||
providedQueryClient: null, | ||
}); | ||
|
||
render( | ||
<WagmiProvider config={wagmiConfig}> | ||
<DefaultOnchainKitProviders> | ||
<div>Test Child</div> | ||
</DefaultOnchainKitProviders> | ||
</WagmiProvider>, | ||
); | ||
|
||
expect(screen.getByText('Test Child')).toBeInTheDocument(); | ||
expect(screen.queryAllByTestId('wagmi-provider')).toHaveLength(1); | ||
}); | ||
|
||
it('should not render duplicate default providers when a query client already exists', () => { | ||
(useProviderDependencies as Mock).mockReturnValue({ | ||
providedWagmiConfig: null, | ||
providedQueryClient: queryClient, | ||
}); | ||
|
||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<DefaultOnchainKitProviders> | ||
<div>Test Child</div> | ||
</DefaultOnchainKitProviders> | ||
</QueryClientProvider>, | ||
); | ||
|
||
expect(screen.getByText('Test Child')).toBeInTheDocument(); | ||
expect(screen.queryAllByTestId('query-client-provider')).toHaveLength(1); | ||
}); | ||
}); |
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,59 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { type PropsWithChildren, useMemo } from 'react'; | ||
import { WagmiProvider } from 'wagmi'; | ||
import { coinbaseWallet } from 'wagmi/connectors'; | ||
import { createWagmiConfig } from './core/createWagmiConfig'; | ||
import type { CreateWagmiConfigParams } from './core/types'; | ||
import { useProviderDependencies } from './internal/hooks/useProviderDependencies'; | ||
|
||
export function DefaultOnchainKitProviders({ | ||
apiKey, | ||
appName, | ||
appLogoUrl, | ||
connectors = [ | ||
coinbaseWallet({ | ||
appName, | ||
appLogoUrl, | ||
preference: 'all', | ||
}), | ||
], | ||
children, | ||
}: PropsWithChildren<CreateWagmiConfigParams>) { | ||
// Check the React context for WagmiProvider and QueryClientProvider | ||
const { providedWagmiConfig, providedQueryClient } = | ||
useProviderDependencies(); | ||
|
||
const defaultConfig = useMemo(() => { | ||
// IMPORTANT: Don't create a new Wagmi configuration if one already exists | ||
// This prevents the user-provided WagmiConfig from being overridden | ||
return ( | ||
providedWagmiConfig || | ||
createWagmiConfig({ | ||
apiKey, | ||
appName, | ||
appLogoUrl, | ||
connectors, | ||
}) | ||
); | ||
}, [apiKey, appName, appLogoUrl, connectors, providedWagmiConfig]); | ||
|
||
const defaultQueryClient = useMemo(() => { | ||
// IMPORTANT: Don't create a new QueryClient if one already exists | ||
// This prevents the user-provided QueryClient from being overridden | ||
return providedQueryClient || new QueryClient(); | ||
}, [providedQueryClient]); | ||
|
||
// If both dependencies are missing, return a context with default parent providers | ||
// If only one dependency is provided, expect the user to also provide the missing one | ||
if (!providedWagmiConfig && !providedQueryClient) { | ||
return ( | ||
<WagmiProvider config={defaultConfig}> | ||
<QueryClientProvider client={defaultQueryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
</WagmiProvider> | ||
); | ||
} | ||
|
||
return children; | ||
} |
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
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
Oops, something went wrong.