1 import type { PropsWithChildren } from 'react';
3 import { renderHook } from '@testing-library/react-hooks';
4 import type { MockedFunction } from 'vitest';
6 import type { WasmBitcoinAddressClient } from '@proton/andromeda';
7 import { ProtonStoreProvider } from '@proton/redux-shared-store';
8 import { getMockedApi } from '@proton/wallet/tests';
10 import { extendStore, setupStore } from '../store';
11 import { useGetBitcoinAddressHighestIndex } from './useBitcoinAddressHighestIndex';
13 describe('useBitcoinAddressHighestIndex', () => {
14 let mockedGetBitcoinAddressHighestIndex: MockedFunction<WasmBitcoinAddressClient['getBitcoinAddressHighestIndex']>;
16 describe('useGetBitcoinAddressHighestIndex', () => {
18 mockedGetBitcoinAddressHighestIndex = vi
20 .mockResolvedValueOnce(BigInt(3))
21 .mockResolvedValueOnce(BigInt(16))
22 .mockResolvedValueOnce(BigInt(3))
23 .mockResolvedValueOnce(BigInt(16));
26 walletApi: getMockedApi({
28 getBitcoinAddressHighestIndex: mockedGetBitcoinAddressHighestIndex,
34 it('should not cache result for each account', async () => {
35 const store = setupStore();
37 function Wrapper({ children }: PropsWithChildren<{}>): JSX.Element {
38 return <ProtonStoreProvider store={store}>{children}</ProtonStoreProvider>;
41 const { result } = renderHook(() => useGetBitcoinAddressHighestIndex(), {
45 expect(await result.current('01', '001')).toBe(3);
46 expect(mockedGetBitcoinAddressHighestIndex).toHaveBeenCalledTimes(1);
47 expect(mockedGetBitcoinAddressHighestIndex).toHaveBeenCalledWith('01', '001');
49 expect(await result.current('01', '002')).toBe(16);
50 expect(mockedGetBitcoinAddressHighestIndex).toHaveBeenCalledTimes(2);
51 expect(mockedGetBitcoinAddressHighestIndex).toHaveBeenCalledWith('01', '002');
53 // should not makes call again
54 mockedGetBitcoinAddressHighestIndex.mockClear();
56 expect(await result.current('01', '001')).toBe(3);
57 expect(mockedGetBitcoinAddressHighestIndex).toHaveBeenCalledTimes(1);
58 expect(mockedGetBitcoinAddressHighestIndex).toHaveBeenCalledWith('01', '001');
60 expect(await result.current('01', '002')).toBe(16);
61 expect(mockedGetBitcoinAddressHighestIndex).toHaveBeenCalledTimes(2);
62 expect(mockedGetBitcoinAddressHighestIndex).toHaveBeenCalledWith('01', '002');