-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62f1d24
commit a673cf6
Showing
6 changed files
with
181 additions
and
10 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
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,18 @@ | ||
import {ErrorBoundary, SuspenseBoundary} from '@yoroi/common' | ||
import * as React from 'react' | ||
import {QueryClient, QueryClientProvider} from 'react-query' | ||
|
||
type Props = { | ||
queryClient: QueryClient | ||
} | ||
|
||
export const wrapperMaker = | ||
({queryClient}: Props) => | ||
({children}: {children: React.ReactNode}) => | ||
( | ||
<QueryClientProvider client={queryClient}> | ||
<ErrorBoundary> | ||
<SuspenseBoundary>{children}</SuspenseBoundary> | ||
</ErrorBoundary> | ||
</QueryClientProvider> | ||
) |
95 changes: 95 additions & 0 deletions
95
packages/portfolio/src/translators/reactjs/usePortfolioTokenDiscovery.test.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,95 @@ | ||
import * as React from 'react' | ||
import {QueryClient} from 'react-query' | ||
import {Text, View} from 'react-native' | ||
import {render, waitFor} from '@testing-library/react-native' | ||
import {queryClientFixture} from '@yoroi/common' | ||
import {Chain} from '@yoroi/types' | ||
|
||
import {tokenDiscoveryMocks} from '../../adapters/token-discovery.mocks' | ||
import {usePorfolioTokenDiscovery} from './usePortfolioTokenDiscovery' | ||
import {wrapperMaker} from '../../fixtures/wrapperMaker' | ||
|
||
describe('usePortfolioTokenDiscovery', () => { | ||
let queryClient: QueryClient | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
queryClient = queryClientFixture() | ||
}) | ||
|
||
afterEach(() => { | ||
queryClient.clear() | ||
}) | ||
|
||
it('success', async () => { | ||
const mockedGetTokenDiscovery = jest | ||
.fn() | ||
.mockResolvedValue(tokenDiscoveryMocks.apiResponseResult.success) | ||
|
||
const TestComponent = () => { | ||
const {data} = usePorfolioTokenDiscovery( | ||
{ | ||
id: tokenDiscoveryMocks.nftCryptoKitty.id, | ||
network: Chain.Network.Mainnet, | ||
getTokenDiscovery: mockedGetTokenDiscovery, | ||
}, | ||
{ | ||
suspense: true, | ||
}, | ||
) | ||
return ( | ||
<View> | ||
<Text testID="data">{JSON.stringify(data?.id)}</Text> | ||
</View> | ||
) | ||
} | ||
const wrapper = wrapperMaker({ | ||
queryClient, | ||
}) | ||
const {getByTestId} = render(<TestComponent />, {wrapper}) | ||
|
||
await waitFor(() => { | ||
expect(getByTestId('data')).toBeDefined() | ||
}) | ||
|
||
expect(getByTestId('data').props.children).toEqual( | ||
JSON.stringify(tokenDiscoveryMocks.nftCryptoKitty.id), | ||
) | ||
expect(mockedGetTokenDiscovery).toHaveBeenCalled() | ||
expect(mockedGetTokenDiscovery).toHaveBeenCalledWith( | ||
tokenDiscoveryMocks.nftCryptoKitty.id, | ||
) | ||
}) | ||
|
||
it('error', async () => { | ||
const mockedGetTokenDiscovery = jest | ||
.fn() | ||
.mockResolvedValue(tokenDiscoveryMocks.apiResponseResult.error) | ||
|
||
const TestComponent = () => { | ||
const {data} = usePorfolioTokenDiscovery( | ||
{ | ||
id: tokenDiscoveryMocks.nftCryptoKitty.id, | ||
network: Chain.Network.Mainnet, | ||
getTokenDiscovery: mockedGetTokenDiscovery, | ||
}, | ||
{ | ||
suspense: true, | ||
}, | ||
) | ||
return ( | ||
<View> | ||
<Text testID="data">{JSON.stringify(data?.id)}</Text> | ||
</View> | ||
) | ||
} | ||
const wrapper = wrapperMaker({ | ||
queryClient, | ||
}) | ||
const {getByTestId} = render(<TestComponent />, {wrapper}) | ||
|
||
await waitFor(() => { | ||
expect(getByTestId('hasError')).toBeDefined() | ||
}) | ||
}) | ||
}) |
36 changes: 36 additions & 0 deletions
36
packages/portfolio/src/translators/reactjs/usePortfolioTokenDiscovery.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,36 @@ | ||
import {isRight} from '@yoroi/common' | ||
import {Chain, Portfolio} from '@yoroi/types' | ||
import {UseQueryOptions, useQuery} from 'react-query' | ||
|
||
export function usePorfolioTokenDiscovery( | ||
{ | ||
id, | ||
getTokenDiscovery, | ||
network, | ||
}: { | ||
id: Portfolio.Token.Id | ||
getTokenDiscovery: Portfolio.Api.Api['tokenDiscovery'] | ||
network: Chain.SupportedNetworks | ||
}, | ||
options?: UseQueryOptions< | ||
Portfolio.Token.Discovery, | ||
Error, | ||
Portfolio.Token.Discovery, | ||
[Chain.SupportedNetworks, 'usePorfolioTokenDiscovery', Portfolio.Token.Id] | ||
>, | ||
) { | ||
const query = useQuery({ | ||
queryKey: [network, 'usePorfolioTokenDiscovery', id], | ||
...options, | ||
queryFn: async () => { | ||
const response = await getTokenDiscovery(id) | ||
if (isRight(response)) return response.value.data | ||
throw new Error('usePorfolioTokenDiscovery') | ||
}, | ||
}) | ||
|
||
return { | ||
...query, | ||
tokenDiscovery: query.data, | ||
} | ||
} |