Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gin-lsl committed May 20, 2024
1 parent 3bd5d95 commit 7b38bd7
Show file tree
Hide file tree
Showing 10 changed files with 730 additions and 524 deletions.
1,172 changes: 684 additions & 488 deletions packages/icons/src/__tests__/__snapshots__/index.test.tsx.snap

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ describe('SolanaWeb3ConfigProvider balance', () => {
};
});

it('availabel show balance', async () => {
it('available show balance', async () => {
const BalanceDisplay: FC = () => {
const { balance } = useProvider();

return <div className="shown-balance">{balance?.value?.toString()}</div>;
};

const App: FC = () => (
<SolanaWeb3ConfigProvider balance>
<SolanaWeb3ConfigProvider balance wallets={[]}>
<div className="content">test</div>
<BalanceDisplay />
</SolanaWeb3ConfigProvider>
Expand Down
17 changes: 10 additions & 7 deletions packages/solana/src/solana-provider/__tests__/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('SolanaWeb3ConfigProvider', () => {

it('mount correctly', () => {
const App = () => (
<SolanaWeb3ConfigProvider>
<SolanaWeb3ConfigProvider wallets={[]}>
<div className="content">test</div>
</SolanaWeb3ConfigProvider>
);
Expand All @@ -120,7 +120,7 @@ describe('SolanaWeb3ConfigProvider', () => {
};

const App = () => (
<SolanaWeb3ConfigProvider chains={[fakeChain]}>
<SolanaWeb3ConfigProvider wallets={[]} chains={[fakeChain]}>
<div>test</div>
</SolanaWeb3ConfigProvider>
);
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('SolanaWeb3ConfigProvider', () => {
const switchChain = vi.fn();

const App = () => (
<SolanaWeb3ConfigProvider>
<SolanaWeb3ConfigProvider wallets={[]}>
<Connector switchChain={switchChain}>
<CustomButton />
</Connector>
Expand All @@ -172,7 +172,7 @@ describe('SolanaWeb3ConfigProvider', () => {
const mockRpcProvider = vi.fn(() => mockRpcEndpoint);

const App = () => (
<SolanaWeb3ConfigProvider rpcProvider={mockRpcProvider}>
<SolanaWeb3ConfigProvider wallets={[]} rpcProvider={mockRpcProvider}>
<div className="content">test</div>
</SolanaWeb3ConfigProvider>
);
Expand All @@ -185,7 +185,10 @@ describe('SolanaWeb3ConfigProvider', () => {

it('ConnectionProvider', () => {
const App = () => (
<SolanaWeb3ConfigProvider rpcProvider={() => `https://main-beta.fake-domain.com/`}>
<SolanaWeb3ConfigProvider
wallets={[]}
rpcProvider={() => `https://main-beta.fake-domain.com/`}
>
<div className="content">test</div>
</SolanaWeb3ConfigProvider>
);
Expand All @@ -206,7 +209,7 @@ describe('SolanaWeb3ConfigProvider', () => {
const App = () => {
const { connect } = useWallet();
return (
<SolanaWeb3ConfigProvider>
<SolanaWeb3ConfigProvider wallets={[]}>
<div>
<div className="content">test</div>
<button
Expand Down Expand Up @@ -255,7 +258,7 @@ describe('SolanaWeb3ConfigProvider', () => {

const App: FC = () => {
return (
<SolanaWeb3ConfigProvider>
<SolanaWeb3ConfigProvider wallets={[]}>
<CustomConnector />
</SolanaWeb3ConfigProvider>
);
Expand Down
37 changes: 22 additions & 15 deletions packages/solana/src/solana-provider/__tests__/connect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ vi.mock('@solana/wallet-adapter-react', async () => {
mockSelectWalletFnNotWalletName(walletName);
const mockWalletAdapter = {
adapter: { name: walletName, readyState: WalletReadyState.Installed },
readyState: WalletReadyState.Installed,
};
currentWalletRef.value = mockWalletAdapter;
setCurrentWallet(mockWalletAdapter);
Expand All @@ -100,18 +101,21 @@ vi.mock('@solana/wallet-adapter-react', async () => {
name: 'Coinbase Wallet',
readyState: WalletReadyState.Installed,
},
readyState: WalletReadyState.Installed,
},
{
adapter: {
name: 'Phantom Wallet',
readyState: WalletReadyState.Installed,
},
readyState: WalletReadyState.Installed,
},
{
adapter: {
name: 'OKX Wallet',
readyState: WalletReadyState.NotDetected,
},
readyState: WalletReadyState.NotDetected,
},
],
};
Expand Down Expand Up @@ -208,11 +212,9 @@ describe('Solana Connect', () => {
expect(shownConnectRunDone.textContent).toBe('false');

fireEvent.click(switchWalletBtn);
await vi.waitFor(() => {
expect(switchWalletRunned).toBeCalled();
});

await vi.waitFor(() => {
expect(switchWalletRunned).toBeCalled();
expect(mockWalletChanged).toBeCalled();
});

Expand All @@ -224,25 +226,25 @@ describe('Solana Connect', () => {
expect(mockSelectWalletFn).toBeCalledTimes(3);
});

await vi.waitFor(
() => {
expect(connectRunned).toBeCalled();
expect(shownConnectRunDone.textContent).toBe('true');
},
{
timeout: 5000,
},
);
await vi.waitFor(() => {
expect(connectRunned).toBeCalled();
expect(shownConnectRunDone.textContent).toBe('true');
});
});

it('call connect but not provide wallet', async () => {
const { useWallet } = await import('@solana/wallet-adapter-react');
const connectRunned = vi.fn();
const availableWalletFound = vi.fn();

const CustomConnectBtn: React.FC = () => {
const { connect } = useProvider();
const { connect, availableWallets } = useProvider();
const { connect: connectWallet } = useWallet();

useEffect(() => {
availableWalletFound(availableWallets?.length || 0);
}, [availableWallets]);

return (
<div className="custom-connectbtn">
<button
Expand All @@ -255,13 +257,14 @@ describe('Solana Connect', () => {
>
Connect
</button>
<div className="availableWallets">{availableWallets?.length || 0}</div>
</div>
);
};

const App = () => {
return (
<SolanaWeb3ConfigProvider>
<SolanaWeb3ConfigProvider wallets={[]}>
<div>
<div className="content">test</div>
<CustomConnectBtn />
Expand All @@ -272,11 +275,15 @@ describe('Solana Connect', () => {

const { selector } = xrender(App);
expect(selector('.content')?.textContent).toBe('test');
expect(selector('.availableWallets')?.textContent).toBe('0');

const connectBtn = selector('.btn-connect')!;
expect(connectBtn).not.toBeNull();

fireEvent.click(connectBtn);
await vi.waitFor(() => {
expect(availableWalletFound).toBeCalledWith(0);
});
await vi.waitFor(() => {
expect(connectRunned).toBeCalled();
expect(mockSelectWalletFnNotWalletName).toBeCalledWith(null);
Expand All @@ -289,7 +296,7 @@ describe('Solana Connect', () => {
const [hasExtensionInstalled, setHasExtensionInstalled] = useState(false);

useEffect(() => {
availableWallets![0]?.hasExtensionInstalled?.().then((v) => {
availableWallets?.[0]?.hasExtensionInstalled?.().then((v) => {
setHasExtensionInstalled(v);
});
}, [availableWallets]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('GetNFTMetadata', () => {
const App: React.FC = () => {
return (
<SolanaWeb3ConfigProvider
wallets={[WalletFactory(new CoinbaseWalletAdapter(), metadata_CoinbaseWallet)]}
wallets={[WalletFactory(new CoinbaseWalletAdapter(), metadata_CoinbaseWallet)()]}
>
<NFTMetadata />
</SolanaWeb3ConfigProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('SolanaWeb3ConfigProvider rpcProvider', () => {
const mockRpcProvider = vi.fn(() => mockRpcEndpoint);

const App = () => (
<SolanaWeb3ConfigProvider rpcProvider={mockRpcProvider}>
<SolanaWeb3ConfigProvider wallets={[]} rpcProvider={mockRpcProvider}>
<div className="content">test</div>
</SolanaWeb3ConfigProvider>
);
Expand All @@ -111,7 +111,7 @@ describe('SolanaWeb3ConfigProvider rpcProvider', () => {

it('use default solana rpcProvider if not chain connected', async () => {
const App = () => (
<SolanaWeb3ConfigProvider>
<SolanaWeb3ConfigProvider wallets={[]}>
<div className="content">test</div>
</SolanaWeb3ConfigProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('SolanaWeb3ConfigProvider Standard wallet', () => {
const [walletReady, setWalletReady] = useState(false);

useEffect(() => {
availableWallets![0]?.hasWalletReady?.().then((v) => {
availableWallets?.[0]?.hasWalletReady?.().then((v) => {
setWalletReady(v);
});
}, [availableWallets]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('Switch network', () => {
);
};
const App = () => (
<SolanaWeb3ConfigProvider>
<SolanaWeb3ConfigProvider wallets={[]}>
<Display />
</SolanaWeb3ConfigProvider>
);
Expand All @@ -104,7 +104,7 @@ describe('Switch network', () => {
);
};
const App = () => (
<SolanaWeb3ConfigProvider chains={[solanaDevnet, solanaTestnet]}>
<SolanaWeb3ConfigProvider wallets={[]} chains={[solanaDevnet, solanaTestnet]}>
<Display />
</SolanaWeb3ConfigProvider>
);
Expand All @@ -126,7 +126,7 @@ describe('Switch network', () => {
balance={false}
autoConnect
chains={[solana, solanaDevnet, solanaTestnet]}
wallets={[WalletFactory(new CoinbaseWalletAdapter(), metadata_CoinbaseWallet)]}
wallets={[WalletFactory(() => new CoinbaseWalletAdapter(), metadata_CoinbaseWallet)()]}
>
<Connector>
<ConnectButton />
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('Switch network', () => {
availableChains={[solanaDevnet]}
balance={false}
availableWallets={[
WalletFactory(new CoinbaseWalletAdapter(), metadata_CoinbaseWallet).create(),
WalletFactory(new CoinbaseWalletAdapter(), metadata_CoinbaseWallet)().create(),
]}
onCurrentChainChange={(chain) => {
mockSwitchChain(chain?.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ describe('AntDesignWeb3ConfigProvider wallet', () => {
availableChains={[solana, solanaDevnet]}
chainAssets={[Solana, SolanaDevnet]}
availableWallets={[
WalletFactory(new CoinbaseWalletAdapter(), metadata_CoinbaseWallet).create(),
WalletFactory(new PhantomWalletAdapter(), metadata_Phantom).create(),
WalletFactory(new CoinbaseWalletAdapter(), metadata_CoinbaseWallet)().create(),
WalletFactory(new PhantomWalletAdapter(), metadata_Phantom)().create(),
]}
>
<CustomConnector />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('SolanaWeb3ConfigProvider WalletConnect', () => {
return (
<button
onClick={() => {
connect?.(availableWallets![0], { connectType: 'qrCode' });
connect?.(availableWallets?.[0], { connectType: 'qrCode' });
}}
>
Click to connect
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('SolanaWeb3ConfigProvider WalletConnect', () => {

return (
<div className="plugin-check">
{availableWallets![0].hasExtensionInstalled === undefined ? 'true' : 'false'}
{availableWallets?.[0].hasExtensionInstalled === undefined ? 'true' : 'false'}
</div>
);
};
Expand Down

0 comments on commit 7b38bd7

Please sign in to comment.