-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(wallet-mobile): network api to include get best block
- Loading branch information
1 parent
ddc4bd1
commit 8a86947
Showing
21 changed files
with
215 additions
and
79 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
21 changes: 21 additions & 0 deletions
21
apps/wallet-mobile/src/features/WalletManager/common/hooks/useBestBlock.tsx
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,21 @@ | ||
import {Chain} from '@yoroi/types' | ||
import {useQuery, UseQueryOptions} from 'react-query' | ||
|
||
import {useSelectedNetwork} from './useSelectedNetwork' | ||
|
||
export const useBestBlock = ({options}: {options?: UseQueryOptions<Chain.Cardano.BestBlock, Error>}) => { | ||
const {networkManager, network} = useSelectedNetwork() | ||
const query = useQuery<Chain.Cardano.BestBlock, Error>({ | ||
suspense: true, | ||
staleTime: 10_000, | ||
retry: 3, | ||
retryDelay: 1_000, | ||
queryKey: [network, 'tipStatus'], | ||
queryFn: () => networkManager.api.bestBlock(), | ||
...options, | ||
}) | ||
|
||
if (!query.data) throw new Error('Failed to retrive tipStatus') | ||
|
||
return query.data | ||
} |
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
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
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
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
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,9 @@ | ||
import {Chain} from '@yoroi/types' | ||
|
||
export const bestBlockMockResponse: Chain.Cardano.BestBlock = { | ||
epoch: 510, | ||
slot: 130081, | ||
globalSlot: 135086881, | ||
hash: 'ab0093eb78bcb0146355741388632eb50c69407df8fa32de85e5f198d725e8f4', | ||
height: 10850697, | ||
} |
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,79 @@ | ||
import {getBestBlock, isBestBlock} from './best-block' | ||
import {bestBlockMockResponse} from './best-block.mocks' | ||
import {fetcher, Fetcher} from '@yoroi/common' | ||
import axios from 'axios' | ||
|
||
jest.mock('axios') | ||
const mockedAxios = axios as jest.MockedFunction<typeof axios> | ||
|
||
describe('getBestBlock', () => { | ||
const baseUrl = 'https://localhost' | ||
const mockFetch = jest.fn() | ||
const customFetcher: Fetcher = jest | ||
.fn() | ||
.mockResolvedValue(bestBlockMockResponse) | ||
|
||
it('returns parsed data when response is valid', async () => { | ||
mockFetch.mockResolvedValue(bestBlockMockResponse) | ||
const tipStatus = getBestBlock(baseUrl, mockFetch) | ||
const result = await tipStatus() | ||
expect(result).toEqual(bestBlockMockResponse) | ||
}) | ||
|
||
it('throws an error if response is invalid', async () => { | ||
mockFetch.mockResolvedValue(null) | ||
const tipStatus = getBestBlock(baseUrl, mockFetch) | ||
await expect(tipStatus()).rejects.toThrow('Invalid best block response') | ||
}) | ||
|
||
it('rejects when response data fails validation', async () => { | ||
const invalidResponse = {unexpectedField: 'invalid data'} | ||
mockFetch.mockResolvedValue(invalidResponse) | ||
const tipStatus = getBestBlock(baseUrl, mockFetch) | ||
|
||
await expect(tipStatus()).rejects.toThrow('Invalid best block response') | ||
}) | ||
|
||
it('uses a custom fetcher function', async () => { | ||
const tipStatus = getBestBlock(baseUrl, customFetcher) | ||
const result = await tipStatus() | ||
expect(customFetcher).toHaveBeenCalled() | ||
expect(result).toEqual(bestBlockMockResponse) | ||
|
||
// coverage | ||
const tipStatus2 = getBestBlock(baseUrl) | ||
expect(tipStatus2).toBeDefined() | ||
}) | ||
|
||
it('uses fetcher and returns data on successful fetch', async () => { | ||
mockedAxios.mockResolvedValue({data: bestBlockMockResponse}) | ||
const tipStatus = getBestBlock(baseUrl, fetcher) | ||
const result = await tipStatus() | ||
|
||
expect(mockedAxios).toHaveBeenCalled() | ||
expect(result).toEqual(bestBlockMockResponse) | ||
}) | ||
|
||
it('throws an error on network issues', async () => { | ||
const networkError = new Error('Network Error') | ||
mockFetch.mockRejectedValue(networkError) | ||
const tipStatus = getBestBlock(baseUrl, mockFetch) | ||
await expect(tipStatus()).rejects.toThrow(networkError.message) | ||
}) | ||
}) | ||
|
||
describe('isBestBlock', () => { | ||
it('returns true for a valid best block response', () => { | ||
expect(isBestBlock(bestBlockMockResponse)).toBe(true) | ||
}) | ||
|
||
it('returns false for an invalid best block response', () => { | ||
const invalidResponse = {...bestBlockMockResponse, epoch: 'invalid'} | ||
expect(isBestBlock(invalidResponse)).toBe(false) | ||
}) | ||
|
||
it('returns false for an incomplete best block response', () => { | ||
const incompleteResponse = {bestBlock: {epoch: 1}} // Missing fields | ||
expect(isBestBlock(incompleteResponse)).toBe(false) | ||
}) | ||
}) |
Oops, something went wrong.