Skip to content

Commit

Permalink
testapp dropdown, update more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nateReiners committed Mar 5, 2024
1 parent d46616a commit a205342
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 36 deletions.
14 changes: 7 additions & 7 deletions apps/testapp/src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type LayoutProps = {
export const WIDTH_2XL = '1536px';

export function Layout({ children }: LayoutProps) {
const { provider, connectionPreference, setPreference } = useCBWSDK();
const { provider, smartWalletOnly, setPreference } = useCBWSDK();

const handleDisconnect = () => {
if (provider) {
Expand All @@ -37,17 +37,17 @@ export function Layout({ children }: LayoutProps) {
<Flex justifyContent="space-between" alignItems="center" gap={4}>
<Menu>
<MenuButton as={Button} rightIcon={<ChevronDownIcon />}>
{`Mode: ${connectionPreference}`}
{`smartWalletOnly: ${smartWalletOnly}`}
</MenuButton>
<MenuList>
{['default', 'embedded'].map((preference) => (
{[true, false].map((b) => (
<MenuItem
color={'MenuText'}
key={preference}
icon={preference === connectionPreference ? <CheckIcon /> : null}
onClick={() => setPreference(preference)}
key={b.toString()}
icon={b === smartWalletOnly ? <CheckIcon /> : null}
onClick={() => setPreference(b)}
>
{preference}
{b.toString()}
</MenuItem>
))}
</MenuList>
Expand Down
33 changes: 12 additions & 21 deletions apps/testapp/src/context/CBWSDKReactContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,48 @@
import CoinbaseWalletSDK from '@coinbase/wallet-sdk';
import { ConnectionPreference } from '@coinbase/wallet-sdk/dist/core/communicator/ConnectionPreference';
import React, { useEffect, useMemo } from 'react';

type CBWSDKProviderProps = {
children: React.ReactNode;
};

const CBWSDKReactContext = React.createContext(null);
const PREFERRED_CONNECTION_KEY = 'preferred_connection';
const SMART_WALLET_ONLY_KEY = 'smart_wallet_only';

export function CBWSDKReactContextProvider({ children }: CBWSDKProviderProps) {
const [connectionPreference, setConnectionPreference] = React.useState<
ConnectionPreference | undefined
>(undefined);
const [smartWalletOnly, setSmartWalletOnly] = React.useState<boolean | undefined>(undefined);
const [sdk, setSdk] = React.useState(null);
const [provider, setProvider] = React.useState(null);

useEffect(() => {
if (connectionPreference === undefined) {
const savedPreference = localStorage.getItem(
PREFERRED_CONNECTION_KEY
) as ConnectionPreference;
setConnectionPreference(
['default', 'embedded'].includes(savedPreference)
? savedPreference
: ('default' as ConnectionPreference)
);
if (smartWalletOnly === undefined) {
const smartWalletOnly = localStorage.getItem(SMART_WALLET_ONLY_KEY);
setSmartWalletOnly(smartWalletOnly === 'true' ? true : false);
}
}, [connectionPreference]);
}, [smartWalletOnly]);

useEffect(() => {
const cbwsdk = new CoinbaseWalletSDK({
appName: 'SDK Playground',
connectionPreference: connectionPreference ?? 'default',
smartWalletOnly,
});
setSdk(cbwsdk);
const cbwprovider = cbwsdk.makeWeb3Provider();
setProvider(cbwprovider);
cbwprovider.on('disconnect', () => {
location.reload();
});
}, [connectionPreference]);
}, [smartWalletOnly]);

const setPreference = (connectionPreference: ConnectionPreference) => {
localStorage.setItem(PREFERRED_CONNECTION_KEY, connectionPreference);
setConnectionPreference(connectionPreference);
const setPreference = (smartWalletOnly: boolean) => {
localStorage.setItem(SMART_WALLET_ONLY_KEY, smartWalletOnly.toString());
setSmartWalletOnly(smartWalletOnly);
};

const ctx = useMemo(
() => ({
sdk,
provider,
connectionPreference,
smartWalletOnly,
setPreference,
}),
[sdk, provider]
Expand Down
6 changes: 3 additions & 3 deletions packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('EIP1193Provider', () => {
scwUrl: 'http://fooUrl.com',
appName: 'TestApp',
appChainIds: [],
connectionPreference: 'default',
smartWalletOnly: false,
});
});

Expand All @@ -30,7 +30,7 @@ describe('EIP1193Provider', () => {
scwUrl: 'http://fooUrl.com',
appName: 'TestApp',
appChainIds: [8453, 84532],
connectionPreference: 'default',
smartWalletOnly: false,
});
expect(provider.chainId).toBe(8453);
});
Expand All @@ -40,7 +40,7 @@ describe('EIP1193Provider', () => {
scwUrl: 'http://fooUrl.com',
appName: 'TestApp',
appChainIds: [],
connectionPreference: 'default',
smartWalletOnly: false,
});
expect(provider.chainId).toBe(1);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/wallet-sdk/src/CoinbaseWalletSDK.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ describe('CoinbaseWalletSDK', () => {
expect(coinbaseWalletSDK2.makeWeb3Provider()).toEqual(mockProvider);
});

test('@makeWeb3Provider, but with connectionPreference as embedded', () => {
test('@makeWeb3Provider, but with smartWalletOnly as true', () => {
const sdk = new CoinbaseWalletSDK({
appName: 'Test',
appLogoUrl: 'http://coinbase.com/wallet-logo.png',
connectionPreference: 'embedded',
smartWalletOnly: true,
});
// Returns extension provider
const provider = sdk.makeWeb3Provider();
Expand Down Expand Up @@ -144,11 +144,11 @@ describe('CoinbaseWalletSDK', () => {
expect(coinbaseWalletSDK2.makeWeb3Provider()).toEqual(mockCipherProvider);
});

test('@makeWeb3Provider, it should ignore embedded connectionPreference', () => {
test('@makeWeb3Provider, it should ignore smartWalletOnly true', () => {
const sdk = new CoinbaseWalletSDK({
appName: 'Test',
appLogoUrl: 'http://coinbase.com/wallet-logo.png',
connectionPreference: 'embedded',
smartWalletOnly: true,
});
expect(sdk.makeWeb3Provider()).toEqual(mockCipherProvider);
});
Expand Down
1 change: 0 additions & 1 deletion packages/wallet-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { CoinbaseWalletSDK } from './CoinbaseWalletSDK';
import { ProviderInterface } from './core/type/ProviderInterface';

export { CoinbaseWalletSDK } from './CoinbaseWalletSDK';
export type { ConnectionPreference } from './core/communicator/ConnectionPreference';
export default CoinbaseWalletSDK;

declare global {
Expand Down

0 comments on commit a205342

Please sign in to comment.