-
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
cca9342
commit c571195
Showing
12 changed files
with
141 additions
and
54 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 |
---|---|---|
@@ -1,27 +1,33 @@ | ||
import {dappConnectorApiGetDappList} from './api' | ||
import {dappConnectorApiMaker} from './api' | ||
import {managerMock} from '../manager.mocks' | ||
|
||
describe('dappConnectorApiGetDappList', () => { | ||
it('should throw error if fetching fails', async () => { | ||
const fakeFetchData = () => Promise.reject(new Error('fake error')) | ||
await expect(() => dappConnectorApiGetDappList({request: fakeFetchData})()).rejects.toThrow() | ||
}) | ||
describe('dappConnectorApiMaker', () => { | ||
describe('getDApps', () => { | ||
it('should throw error if fetching fails', async () => { | ||
const fakeFetchData = () => Promise.reject(new Error('fake error')) | ||
await expect(() => dappConnectorApiMaker({request: fakeFetchData}).getDApps()).rejects.toThrow() | ||
}) | ||
|
||
it('should throw error if response is invalid', async () => { | ||
const fakeResult = {tag: 'right' as const, value: {data: {dapps: 1}}} as const | ||
const fakeFetchData = () => Promise.resolve(fakeResult) | ||
await expect(() => dappConnectorApiGetDappList({request: fakeFetchData as any})()).rejects.toThrow() | ||
}) | ||
it('should throw error if response is invalid', async () => { | ||
const fakeResult = {tag: 'right' as const, value: {data: {dapps: 1}}} as const | ||
const fakeFetchData = () => Promise.resolve(fakeResult) | ||
await expect(() => dappConnectorApiMaker({request: fakeFetchData as any}).getDApps()).rejects.toThrow() | ||
}) | ||
|
||
it('should throw error if response is left', async () => { | ||
const fakeResult = {tag: 'left' as const, error: {status: 404, message: 'Not found', responseData: null}} as const | ||
const fakeFetchData = () => Promise.resolve(fakeResult) | ||
await expect(() => dappConnectorApiGetDappList({request: fakeFetchData as any})()).rejects.toThrow() | ||
}) | ||
it('should throw error if response is left', async () => { | ||
const fakeResult = {tag: 'left' as const, error: {status: 404, message: 'Not found', responseData: null}} as const | ||
const fakeFetchData = () => Promise.resolve(fakeResult) | ||
await expect(() => dappConnectorApiMaker({request: fakeFetchData as any}).getDApps()).rejects.toThrow() | ||
}) | ||
|
||
it('should return data if response is valid', async () => { | ||
const fakeResult = {tag: 'right' as const, value: {data: {dapps: [], filters: {}}}} as const | ||
const fakeFetchData = () => Promise.resolve(fakeResult) | ||
const result = await dappConnectorApiGetDappList({request: fakeFetchData as any})() | ||
expect(result).toEqual(fakeResult.value.data) | ||
it('should return data if response is valid', async () => { | ||
const fakeResult = {tag: 'right' as const, value: {data: await managerMock.getDAppList()}} as const | ||
const fakeFetchData = () => Promise.resolve(fakeResult) | ||
const result = await dappConnectorApiMaker({request: fakeFetchData as any}).getDApps() | ||
expect(result).toEqual({ | ||
...fakeResult.value.data, | ||
dapps: fakeResult.value.data.dapps.map((d) => ({...d, logo: 'https://daehx1qv45z7c.cloudfront.net/icon.png'})), | ||
}) | ||
}) | ||
}) | ||
}) |
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,38 @@ | ||
import {DappConnectorManager} from './dapp-connector' | ||
import {DappListResponse} from './adapters/api' | ||
|
||
export const managerMock: DappConnectorManager = { | ||
getDAppList(): Promise<DappListResponse> { | ||
return Promise.resolve(mockedDAppList) | ||
}, | ||
listAllConnections(): Promise<[]> { | ||
return Promise.resolve([]) | ||
}, | ||
removeConnection(): Promise<void> { | ||
return Promise.resolve() | ||
}, | ||
addConnection(): Promise<void> { | ||
return Promise.resolve() | ||
}, | ||
getWalletConnectorScript(): string { | ||
return '' | ||
}, | ||
handleEvent(): Promise<void> { | ||
return Promise.resolve() | ||
}, | ||
} | ||
|
||
export const mockedDAppList = { | ||
dapps: [ | ||
{ | ||
id: 'example', | ||
description: 'Example DApp', | ||
logo: 'icon.png', | ||
name: 'Example DApp', | ||
category: 'example', | ||
uri: 'https://example.com', | ||
origins: ['https://example.com'], | ||
}, | ||
], | ||
filters: {'Category 1': ['Example 1', 'Example 2']}, | ||
} |
6 changes: 3 additions & 3 deletions
6
packages/dapp-connector/src/translators/reactjs/DappConnectorProvider.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
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
6 changes: 4 additions & 2 deletions
6
packages/dapp-connector/src/translators/reactjs/useDappList.ts
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import {useQuery, UseQueryOptions} from 'react-query' | ||
import {dappConnectorApiGetDappList, DappListResponse} from '../../adapters/api' | ||
import {DappListResponse} from '../../adapters/api' | ||
import {useDappConnector} from './DappConnectorProvider' | ||
|
||
export const useDappList = (options?: UseQueryOptions<DappListResponse, Error, DappListResponse, 'dappList'>) => { | ||
const {manager} = useDappConnector() | ||
return useQuery('dappList', { | ||
...options, | ||
queryFn: dappConnectorApiGetDappList(), | ||
queryFn: manager.getDAppList, | ||
}) | ||
} |