Skip to content

Commit

Permalink
test(bitcoin): add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
AmAzing129 committed May 30, 2024
1 parent 3a42307 commit 00ea5fa
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/bitcoin/src/adapter/useBitcoinWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface BitcoinWallet<Provider = any> {
getBalance: () => Promise<Balance>;
connect: () => Promise<void>;
signMessage: (message: string) => Promise<string>;
sendTransfer: (prams: TransferParams) => Promise<string>;
sendTransfer: (params: TransferParams) => Promise<string>;
signPsbt: (params: SignPsbtParams) => Promise<SignPsbtResult>;
getInscriptions: (
offset?: number,
Expand Down
4 changes: 2 additions & 2 deletions packages/bitcoin/src/adapter/wallets/xverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ export class XverseBitcoinWallet implements BitcoinWallet {
}
};

getInscriptions = async (offset = 0, size = 20) => {
getInscriptions = async (offset = 0, limit = 20) => {
if (!this.account?.address) {
throw new NoAddressError();
}
const inscriptions = await getInscriptionsByAddress({
address: this.account?.address,
offset,
size,
limit,
});
return inscriptions;
};
Expand Down
9 changes: 4 additions & 5 deletions packages/bitcoin/src/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ export const getBalanceByMempool = async (address: string): Promise<Balance> =>

export const getInscriptionsByAddress = async ({
address,
size,
limit,
offset,
}: {
address: string;
size: number;
limit: number;
offset: number;
}): Promise<{ total: number; list: Inscription[] }> => {
const params = new URLSearchParams({ address, limit: `${size}`, offset: `${offset}` });

const res = await fetch(`${HIRO_API}/ordinals/v1/inscriptions?${params.toString()}`);
const params = { address, limit: `${limit}`, offset: `${offset}` };
const res = await fetch(`${HIRO_API}/ordinals/v1/inscriptions?${new URLSearchParams(params)}`);
if (res.ok) {
const { results, total } = await res.json();
const list = (results as HiroInscription[]).map(
Expand Down
4 changes: 2 additions & 2 deletions packages/bitcoin/src/provider/__tests__/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('BitcoinWeb3ConfigProvider', () => {
// @ts-ignore: vi.fn().mockResolvedValue
fetch.mockResolvedValue(() => Promise.resolve({ ok: false }));
try {
await getInscriptionsByAddress({ address: 'bc1p', size: 10, offset: 0 });
await getInscriptionsByAddress({ address: 'bc1p', limit: 10, offset: 0 });
} catch (e: any) {
console.log(e.message);
}
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('BitcoinWeb3ConfigProvider', () => {
fetch.mockResolvedValue(createFetchResponse(inscriptionResponse));

try {
await getInscriptionsByAddress({ address: 'bc1p', size: 10, offset: 0 });
await getInscriptionsByAddress({ address: 'bc1p', limit: 10, offset: 0 });
} catch (e: any) {
console.log(e.message);
}
Expand Down
53 changes: 53 additions & 0 deletions packages/bitcoin/src/provider/__tests__/nft.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { NFTCard } from '@ant-design/web3';
import { render, waitFor } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { BitcoinWeb3ConfigProvider } from '../index';

describe('BitcoinWeb3ConfigProvider getMetadata', () => {
it('NFT Metadata', async () => {
vi.spyOn(window, 'fetch').mockImplementation(() => {
return Promise.resolve({
ok: true,
json: () => {
return {
image: 'http://nft-metadata.com',
name: 'Inscription',
};
},
} as any);
});

const App = () => (
<BitcoinWeb3ConfigProvider>
<NFTCard address="0x1234" name="Inscription" />
</BitcoinWeb3ConfigProvider>
);
const { baseElement } = render(<App />);
await waitFor(() => {
expect(baseElement.querySelector('.ant-web3-nft-card-name')?.textContent).toBe('Inscription');
});
});
});

describe('BitcoinWeb3ConfigProvider getMetadata', () => {
it("can't get", async () => {
vi.spyOn(window, 'fetch').mockImplementation(() => {
return Promise.resolve({
ok: false,
} as any);
});

const App = () => (
<BitcoinWeb3ConfigProvider>
<NFTCard address="0x1234" />
</BitcoinWeb3ConfigProvider>
);
const { baseElement } = render(<App />);
await waitFor(() => {
expect(baseElement.querySelector('.ant-result-subtitle')?.textContent).toBe(
'Failed to get inscriptions',
);
});
});
});
2 changes: 1 addition & 1 deletion packages/wagmi/src/wagmi-provider/config-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export const AntDesignWeb3ConfigProvider: React.FC<AntDesignWeb3ConfigProviderPr

const getNFTMetadataFunc = React.useCallback(
async ({ address: contractAddress, tokenId }: { address: string; tokenId?: bigint }) =>
getNFTMetadata(config, contractAddress, tokenId ?? 0n, chain?.id),
getNFTMetadata(config, contractAddress, tokenId!, chain?.id),
[chain?.id],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`NFTImage > use custom getNFTMetadata 1`] = `
alt="Test_0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B_123"
class="ant-image-img css-dev-only-do-not-override-17seli4"
src="https://example.com/nft.png"
style="image-rendering: pixelated;"
/>
<div
class="ant-image-mask"
Expand Down

0 comments on commit 00ea5fa

Please sign in to comment.