forked from hirosystems/explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Pinned the @types/react-csv version - Changed component name to pascal case - Removed console.log - Converted getTxsData to hook and renamed functions - Added unit test cases for useTxsCSVData hook - Removed zIndex - Removed setTimeout code
- Loading branch information
1 parent
03c3fd5
commit 5e62dfb
Showing
11 changed files
with
380 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/common/hooks/__snapshots__/useTxsCSVData.spec.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`useTxsCSVData Custom Hook should match the formatTxsCSVData result with the snapshot 1`] = ` | ||
Array [ | ||
Object { | ||
"Date": "2023-05-04T18:10:58.000Z", | ||
"Fee Amount": "0.456789", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": NaN, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.987654", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.123456", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_1234", | ||
}, | ||
Object { | ||
"Date": "2023-06-12T08:30:15.000Z", | ||
"Fee Amount": "0.789012", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": NaN, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.234567", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.987654", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_5678", | ||
}, | ||
Object { | ||
"Date": "2023-07-25T13:45:30.000Z", | ||
"Fee Amount": "0.345678", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": NaN, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.789012", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.234567", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_9012", | ||
}, | ||
Object { | ||
"Date": "2023-08-05T16:20:05.000Z", | ||
"Fee Amount": "0.123456", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": NaN, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.345678", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.789012", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_3456", | ||
}, | ||
Object { | ||
"Date": "2023-09-15T21:05:40.000Z", | ||
"Fee Amount": "0.987654", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": NaN, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.123456", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.345678", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_7890", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`useTxsCSVData Custom Hook should match the getTxsCSVData result with the snapshot 1`] = ` | ||
Array [ | ||
Object { | ||
"Date": "2023-05-04T18:10:58.000Z", | ||
"Fee Amount": "0.456789", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": 1.987654, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.987654", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.123456", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_1234", | ||
}, | ||
Object { | ||
"Date": "2023-06-12T08:30:15.000Z", | ||
"Fee Amount": "0.789012", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": 1.234567, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.234567", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.987654", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_5678", | ||
}, | ||
Object { | ||
"Date": "2023-07-25T13:45:30.000Z", | ||
"Fee Amount": "0.345678", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": 1.789012, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.789012", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.234567", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_9012", | ||
}, | ||
Object { | ||
"Date": "2023-08-05T16:20:05.000Z", | ||
"Fee Amount": "0.123456", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": 1.345678, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.345678", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.789012", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_3456", | ||
}, | ||
Object { | ||
"Date": "2023-09-15T21:05:40.000Z", | ||
"Fee Amount": "0.987654", | ||
"Fee Currency": "STX", | ||
"Net Worth Amount": 1.123456, | ||
"Net Worth Currency": "STX", | ||
"Received Amount": "0.123456", | ||
"Received Currency": "STX", | ||
"Sent Amount": "0.345678", | ||
"Sent Currency": "STX", | ||
"TxHash": "tx_7890", | ||
}, | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
import { useTxsCSVData } from './useTxsCSVData'; | ||
import { QueryClient, QueryClientProvider } from 'react-query'; | ||
import { Provider } from 'react-redux'; | ||
import { store } from '../state/store'; | ||
import { addressBalanceMockData, addressTxsMockData, mockAddress } from '../mockData/txsData'; | ||
import { AddressTransactionWithTransfers } from '@stacks/stacks-blockchain-api-types'; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
queryClient.setQueryData( | ||
['addressConfirmedTxsWithTransfersInfinite', mockAddress], | ||
addressTxsMockData | ||
); | ||
|
||
queryClient.setQueryData(['accountBalance', mockAddress], addressBalanceMockData); | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<Provider store={store}> | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
</Provider> | ||
); | ||
|
||
describe('useTxsCSVData Custom Hook', () => { | ||
it('should match the getTxsCSVData result with the snapshot', () => { | ||
const { result } = renderHook(() => useTxsCSVData(), { wrapper }); | ||
|
||
const txsCsvData = result.current.getTxsCSVData(mockAddress); | ||
|
||
expect(txsCsvData.length).toBe(addressTxsMockData.pages[0].results.length); | ||
expect(txsCsvData).toMatchSnapshot(); | ||
}); | ||
|
||
it('should match the formatTxsCSVData result with the snapshot', () => { | ||
const { result } = renderHook(() => useTxsCSVData(), { wrapper }); | ||
|
||
const formatTxsData = result.current.formatTxsCSVData( | ||
addressTxsMockData.pages[0].results as AddressTransactionWithTransfers[], | ||
mockAddress | ||
); | ||
|
||
expect(formatTxsData.length).toBe(addressTxsMockData.pages[0].results.length); | ||
expect(formatTxsData).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
AddressBalanceResponse, | ||
AddressTransactionsWithTransfersListResponse, | ||
AddressTransactionWithTransfers, | ||
} from '@stacks/stacks-blockchain-api-types'; | ||
import { InfiniteData, useQueryClient } from 'react-query'; | ||
|
||
import { useFilterState } from '@/app/common/hooks/use-filter-state'; | ||
import { microStxToStx, microToStacks } from '../utils'; | ||
|
||
export type CSVDownloadObjectType = { | ||
Date: string; | ||
'Sent Amount': string | number; | ||
'Sent Currency': string; | ||
'Received Amount': string | number; | ||
'Received Currency': string; | ||
'Fee Amount': string | number; | ||
'Fee Currency': string; | ||
'Net Worth Amount': string | number; | ||
'Net Worth Currency': string; | ||
TxHash: string; | ||
}; | ||
|
||
export const useTxsCSVData = () => { | ||
const queryClient = useQueryClient(); | ||
const { activeFilters } = useFilterState(); | ||
|
||
const getTxsCSVData = (address: string): CSVDownloadObjectType[] => { | ||
const txsQueryData = queryClient.getQueriesData< | ||
InfiniteData<AddressTransactionsWithTransfersListResponse> | ||
>({ | ||
queryKey: ['addressConfirmedTxsWithTransfersInfinite', address], | ||
}); | ||
|
||
const addressBalanceQueryData = queryClient.getQueriesData<AddressBalanceResponse>({ | ||
queryKey: ['accountBalance', address], | ||
}); | ||
|
||
const balance = addressBalanceQueryData[0][1].stx.balance; | ||
|
||
const txs = txsQueryData[0][1].pages.reduce<AddressTransactionWithTransfers[]>( | ||
(acc, { results }) => { | ||
return acc.concat(results); | ||
}, | ||
[] | ||
); | ||
|
||
const filteredTxs = txs?.filter(tx => activeFilters[tx.tx.tx_type]); | ||
return formatTxsCSVData(filteredTxs, balance); | ||
}; | ||
|
||
const formatTxsCSVData = ( | ||
txs: AddressTransactionWithTransfers[], | ||
balance: string | number | ||
): CSVDownloadObjectType[] => { | ||
let stxBalance = Number(balance); | ||
return txs.map((transaction, i) => { | ||
const { | ||
stx_sent, | ||
stx_received, | ||
tx: { burn_block_time_iso, fee_rate, tx_id }, | ||
} = transaction; | ||
if (i > 0) { | ||
stxBalance += Number(stx_received) - Number(stx_sent); | ||
} | ||
return { | ||
Date: burn_block_time_iso, | ||
'Sent Amount': microToStacks(stx_sent), | ||
'Sent Currency': 'STX', | ||
'Received Amount': microToStacks(stx_received), | ||
'Received Currency': 'STX', | ||
'Fee Amount': microToStacks(fee_rate), | ||
'Fee Currency': 'STX', | ||
'Net Worth Amount': microStxToStx(stxBalance), | ||
'Net Worth Currency': 'STX', | ||
TxHash: tx_id, | ||
}; | ||
}); | ||
}; | ||
|
||
return { getTxsCSVData, formatTxsCSVData }; | ||
}; |
Oops, something went wrong.