Skip to content

Commit

Permalink
Complicate the api into a factory so that tests can be passed
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Feb 21, 2025
1 parent 5538644 commit 58d32e0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
3 changes: 2 additions & 1 deletion apps/wallet-mobile/src/features/Swap/common/useSwapConfig.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {getSwapConfig, useSwapTokensOnlyVerified} from '@yoroi/swap'
import {getSwapConfigApiMaker, useSwapTokensOnlyVerified} from '@yoroi/swap'
import {useQuery} from 'react-query'

import {usePortfolioTokenInfos} from '../../Portfolio/common/hooks/usePortfolioTokenInfos'
import {useSelectedWallet} from '../../WalletManager/common/hooks/useSelectedWallet'

export const useSwapConfig = () => {
const getSwapConfig = getSwapConfigApiMaker()
const query = useQuery({
suspense: true,
queryKey: ['useSwapConfig'],
Expand Down
16 changes: 11 additions & 5 deletions packages/swap/src/adapters/getSwapConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getSwapConfig} from './getSwapConfig'
import {getSwapConfigApiMaker} from './getSwapConfig'
import {FetchData} from '@yoroi/common'
import {Portfolio} from '@yoroi/types'
import {freeze} from 'immer'
Expand All @@ -25,9 +25,9 @@ const mockFetchData = (async ({url}: {url: string}) => {
})
}) as FetchData

describe('getSwapConfig', () => {
describe('getSwapConfigApiMaker', () => {
it('should return swap config data when the response is valid', async () => {
const result = await getSwapConfig({request: mockFetchData})
const result = await getSwapConfigApiMaker({request: mockFetchData})()
expect(result).toEqual({
initialPair: {
tokenIn: '.' as Portfolio.Token.Id,
Expand All @@ -52,7 +52,9 @@ describe('getSwapConfig', () => {
},
} as any)

await expect(getSwapConfig({request: invalidFetchData})).rejects.toThrow(
await expect(
getSwapConfigApiMaker({request: invalidFetchData})(),
).rejects.toThrow(
'Invalid swap config response: {"initialPair":{"tokenIn":123,"tokenOut":"tokenOutId"}}',
)
})
Expand All @@ -65,7 +67,11 @@ describe('getSwapConfig', () => {
} as any)

await expect(
getSwapConfig({request: networkErrorFetchData}),
getSwapConfigApiMaker({request: networkErrorFetchData})(),
).rejects.toThrow('Network error')
})

it('should not require passing a param', async () => {
expect(getSwapConfigApiMaker()).toBeDefined()
})
})
37 changes: 19 additions & 18 deletions packages/swap/src/adapters/getSwapConfig.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import {FetchData, fetchData, getApiError, isLeft} from '@yoroi/common'
import {TokenIdSchema} from '@yoroi/portfolio'
import {Portfolio} from '@yoroi/types'
import {freeze} from 'immer'
import {z} from 'zod'

type SwapConfig = z.infer<typeof SwapConfigResponseSchema>

const initialDeps = freeze({request: fetchData}, true)
export const getSwapConfigApiMaker =
(
{
request,
}: {
request: FetchData
} = {request: fetchData},
) =>
async (): Promise<SwapConfig> => {
const response = await request<SwapConfig>({
url: 'https://daehx1qv45z7c.cloudfront.net/swapConfig.json',
})

export const getSwapConfig = async ({
request,
}: {
request: FetchData
} = initialDeps): Promise<SwapConfig> => {
const response = await request<SwapConfig>({
url: 'https://daehx1qv45z7c.cloudfront.net/swapConfig.json',
})
if (isLeft(response)) throw getApiError(response.error)

if (isLeft(response)) throw getApiError(response.error)
if (!SwapConfigResponseSchema.safeParse(response.value.data).success) {
throw new Error(
'Invalid swap config response: ' + JSON.stringify(response.value.data),
)
}

if (!SwapConfigResponseSchema.safeParse(response.value.data).success) {
throw new Error(
'Invalid swap config response: ' + JSON.stringify(response.value.data),
)
return response.value.data
}

return response.value.data
}

const SwapConfigResponseSchema = z.object({
initialPair: z.object({
tokenIn: TokenIdSchema.refine((_): _ is Portfolio.Token.Id => true),
Expand Down
4 changes: 1 addition & 3 deletions packages/swap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,4 @@ export {
makeStorageMaker,
makeStorageMakerDefault,
} from './adapters/async-storage/storage.mocks'

// static
export {getSwapConfig} from './adapters/getSwapConfig'
export {getSwapConfigApiMaker} from './adapters/getSwapConfig'

0 comments on commit 58d32e0

Please sign in to comment.