Skip to content

Commit

Permalink
feat: fix api calls, update test
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkparti committed Aug 15, 2023
1 parent a381aa8 commit d43d914
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 21 deletions.
34 changes: 17 additions & 17 deletions src/pages/Wallet/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { MessageType } from '../../components/UI/components/Message/Message';
import AuthContext from '../../store/auth-context';
import { getWalletById } from '../../api/wallets';
import WalletHeader from './WalletHeader/WalletHeader';
// import { getTransfers } from '../../api/transfers';
// import TransferFilter from '../../models/TransferFilter';
import { getTransfers } from '../../api/transfers';
import TransferFilter from '../../models/TransferFilter';

const Wallet = () => {
const [isLoading, setIsLoading] = useState(true);
Expand All @@ -23,7 +23,7 @@ const Wallet = () => {
};

const [wallet, setWallet] = useState(defaultWallet);
// const [pendingTransfers, setPendingTransfers] = useState(null);
const [pendingTransfers, setPendingTransfers] = useState(null);

const authContext = useContext(AuthContext);

Expand Down Expand Up @@ -53,18 +53,18 @@ const Wallet = () => {


// get pending transfers
// const pendingTransferFilter = new TransferFilter({ state: 'pending' });
// getTransfers({ filter: pendingTransferFilter })
// .then(pendingTransfers => {
// setPendingTransfers(pendingTransfers);
// })
// .catch(error => {
// console.error(error);
// setErrorMessage('An error occurred while fetching transfers data.');
// })
// .finally(() => {
// setIsLoading(false);
// });
const pendingTransferFilter = new TransferFilter({ state: 'pending' });
getTransfers({ filter: pendingTransferFilter })
.then(pendingTransfersData => {
setPendingTransfers(pendingTransfersData.transfers);
})
.catch(error => {
console.error(error);
setErrorMessage('An error occurred while fetching transfers data.');
})
.finally(() => {
setIsLoading(false);
});
}, []);


Expand All @@ -84,9 +84,9 @@ const Wallet = () => {
messageType={MessageType.Error}
/>
)}

<ContentContainer>
<WalletHeader walletName={wallet.name} walletLogoURL={wallet.logoURL} pendingTransfers={12} />
<WalletHeader walletName={wallet.name} walletLogoURL={wallet.logoURL}
pendingTransfers={pendingTransfers.length} />
<ContentGrid>
<WalletInfoBlock
title={`Wallet ${wallet.name}`}
Expand Down
81 changes: 77 additions & 4 deletions src/pages/Wallet/Wallet.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,90 @@
import { render, screen } from '@testing-library/react';
import React from 'react';
import apiClient from '../../utils/apiClient';
import { getTransfers } from '../../api/transfers';
import Wallet from './Wallet';

jest.mock('../../utils/apiClient', () => ({
get: jest.fn(),
}));

jest.mock('../../api/transfers', () => ({
getTransfers: jest.fn(),
}));


const mockPendingTransfers = {
transfers: [
{
'id': '903cf9eb-1d16-4831-9b5e-774136ad4539',
'type': 'send',
'parameters': {
'bundle': {
'bundleSize': '1',
},
},
'state': 'pending',
'created_at': '2023-08-08T02:17:48.249Z',
'closed_at': '2023-08-08T02:17:48.249Z',
'active': true,
'claim': false,
'originating_wallet': 'testuser',
'source_wallet': 'fff',
'destination_wallet': 'testuser',
'token_count': 1,
},
{
'id': 'cf56cab1-15ed-4ddb-bbe2-e50157b52b5e',
'type': 'send',
'parameters': {
'bundle': {
'bundleSize': '2',
},
},
'state': 'pending',
'created_at': '2023-08-02T04:41:37.121Z',
'closed_at': '2023-08-02T04:41:37.121Z',
'active': true,
'claim': false,
'originating_wallet': 'testuser',
'source_wallet': 'fff',
'destination_wallet': 'testuser',
'token_count': 2,
},
{
'id': '4377c592-8ab0-4448-a3d3-ebf64f719a3b',
'type': 'send',
'parameters': {
'bundle': {
'bundleSize': '1',
},
},
'state': 'pending',
'created_at': '2023-08-10T21:17:35.801Z',
'closed_at': '2023-08-10T21:17:35.801Z',
'active': true,
'claim': false,
'originating_wallet': 'testuser',
'source_wallet': 'test11',
'destination_wallet': 'testnewwallet',
'token_count': 1,
},
],
'query': {
'limit': 200,
'offset': 0,
},
'total': 3,
};

describe('<Wallet />', () => {
beforeEach(() => {
localStorage.setItem(
'wallet',
JSON.stringify({
id: '9d6c674f-ae62-4fab-8d14-ae5de9f14ab8',
wallet: 'test wallet',
})
}),
);
});

Expand All @@ -32,6 +102,8 @@ describe('<Wallet />', () => {
},
});

getTransfers.mockResolvedValueOnce(mockPendingTransfers);

render(<Wallet />);

expect(screen.getByRole('progressbar')).toBeInTheDocument();
Expand All @@ -45,16 +117,17 @@ describe('<Wallet />', () => {

it('displays error message on fetch failure', async () => {
apiClient.get.mockImplementationOnce(() => Promise.reject('API error'));
getTransfers.mockImplementation(() => Promise.reject('API error'));

render(<Wallet />);

expect(screen.getByRole('progressbar')).toBeInTheDocument();

// TODO: fix and uncomment
//await screen.findByRole('alert');

// await screen.findByRole('alert');
//
// expect(screen.getByRole('alert')).toHaveTextContent(
// 'An error occurred while fetching wallet data.'
// 'An error occurred while fetching wallet data.',
// );
});
});

0 comments on commit d43d914

Please sign in to comment.