From f209e22ffd698da15c6e9c4f146c81a89fa25b3c Mon Sep 17 00:00:00 2001 From: Korbinian Kasberger Date: Tue, 12 Sep 2023 14:43:07 +0200 Subject: [PATCH 1/2] fixed unit tests --- .../libs/storage/CustomTokenService.test.ts | 37 +++++++++++++- .../libs/token/getCrossChainAddress.test.ts | 12 +++-- .../libs/util/mergeTransactions.test.ts | 49 ++++++++++++++++--- 3 files changed, 87 insertions(+), 11 deletions(-) rename packages/bridge-ui-v2/src/{test => }/libs/util/mergeTransactions.test.ts (73%) diff --git a/packages/bridge-ui-v2/src/libs/storage/CustomTokenService.test.ts b/packages/bridge-ui-v2/src/libs/storage/CustomTokenService.test.ts index 6430e83549..572fe821b4 100644 --- a/packages/bridge-ui-v2/src/libs/storage/CustomTokenService.test.ts +++ b/packages/bridge-ui-v2/src/libs/storage/CustomTokenService.test.ts @@ -1,5 +1,5 @@ import { type Address, zeroAddress } from 'viem'; -import { describe, expect, test, vi } from 'vitest'; +import { describe, expect, vi } from 'vitest'; import { type Token, TokenType } from '$libs/token'; @@ -7,6 +7,39 @@ import { CustomTokenService } from './CustomTokenService'; const STORAGE_PREFIX = 'custom-tokens'; +function setupMocks() { + vi.mock('$customToken', () => { + return { + customToken: [ + { + name: 'Bull Token', + addresses: { + '31336': '0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0', + '167002': '0x9A9f2CCfdE556A7E9Ff0848998Aa4a0CFD8863AE', + }, + symbol: 'BLL', + decimals: 18, + type: 'ERC20', + logoURI: 'ipfs://QmezMTpT6ovJ3szb3SKDM9GVGeQ1R8DfjYyXG12ppMe2BY', + mintable: true, + }, + { + name: 'Horse Token', + addresses: { + '31336': '0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e', + '167002': '0x959922bE3CAee4b8Cd9a407cc3ac1C251C2007B1', + }, + symbol: 'HORSE', + decimals: 18, + type: 'ERC20', + logoURI: 'ipfs://QmU52ZxmSiGX24uDPNUGG3URyZr5aQdLpACCiD6tap4Mgc', + mintable: true, + }, + ], + }; + }); +} + describe('CustomTokenService', () => { const localStorage = global.localStorage; let token1: Token; @@ -20,6 +53,8 @@ describe('CustomTokenService', () => { const removeItemSpy = vi.spyOn(Storage.prototype, 'removeItem'); beforeEach(() => { + setupMocks(); + tokenService = new CustomTokenService(localStorage); address = '0x1234'; storageKey = STORAGE_PREFIX + '-' + address; diff --git a/packages/bridge-ui-v2/src/libs/token/getCrossChainAddress.test.ts b/packages/bridge-ui-v2/src/libs/token/getCrossChainAddress.test.ts index 61c0df32d2..0fa73137a1 100644 --- a/packages/bridge-ui-v2/src/libs/token/getCrossChainAddress.test.ts +++ b/packages/bridge-ui-v2/src/libs/token/getCrossChainAddress.test.ts @@ -25,13 +25,17 @@ const mockTokenVaultContract = { }, } as unknown as GetContractResult; -vi.mock('$libs/chain', () => ({ - chainContractsMap: { +vi.mock('$bridgeConfig', () => ({ + routingContractsMap: { 1: { - tokenVaultAddress: '0x00001', + 2: { + erc20VaultAddress: '0x00001', + }, }, 2: { - tokenVaultAddress: '0x00002', + 1: { + erc20VaultAddress: '0x00002', + }, }, }, })); diff --git a/packages/bridge-ui-v2/src/test/libs/util/mergeTransactions.test.ts b/packages/bridge-ui-v2/src/libs/util/mergeTransactions.test.ts similarity index 73% rename from packages/bridge-ui-v2/src/test/libs/util/mergeTransactions.test.ts rename to packages/bridge-ui-v2/src/libs/util/mergeTransactions.test.ts index c21857eff4..6d6e9125b8 100644 --- a/packages/bridge-ui-v2/src/test/libs/util/mergeTransactions.test.ts +++ b/packages/bridge-ui-v2/src/libs/util/mergeTransactions.test.ts @@ -1,10 +1,47 @@ import type { Address, Hex } from 'viem'; import { type BridgeTransaction, MessageStatus } from '$libs/bridge'; -import { TokenType } from '$libs/token'; +import type { TokenType } from '$libs/token'; import { mergeAndCaptureOutdatedTransactions } from '$libs/util/mergeTransactions'; +function setupMocks() { + vi.mock('$customToken', () => { + return { + customToken: [ + { + name: 'Bull Token', + addresses: { + '31336': '0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0', + '167002': '0x9A9f2CCfdE556A7E9Ff0848998Aa4a0CFD8863AE', + }, + symbol: 'BLL', + decimals: 18, + type: 'ERC20', + logoURI: 'ipfs://QmezMTpT6ovJ3szb3SKDM9GVGeQ1R8DfjYyXG12ppMe2BY', + mintable: true, + }, + { + name: 'Horse Token', + addresses: { + '31336': '0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e', + '167002': '0x959922bE3CAee4b8Cd9a407cc3ac1C251C2007B1', + }, + symbol: 'HORSE', + decimals: 18, + type: 'ERC20', + logoURI: 'ipfs://QmU52ZxmSiGX24uDPNUGG3URyZr5aQdLpACCiD6tap4Mgc', + mintable: true, + }, + ], + }; + }); +} + describe('mergeUniqueTransactions', () => { + beforeEach(() => { + setupMocks(); + }); + // Given const localTxs: BridgeTransaction[] = [ { @@ -18,7 +55,7 @@ describe('mergeUniqueTransactions', () => { status: MessageStatus.DONE, msgHash: 'msg1' as Hex, receipt: undefined, - tokenType: TokenType.ERC20, + tokenType: 'ERC20' as TokenType, }, { hash: 'hash2' as Hex, @@ -31,7 +68,7 @@ describe('mergeUniqueTransactions', () => { status: MessageStatus.DONE, msgHash: 'msg2' as Hex, receipt: undefined, - tokenType: TokenType.ERC20, + tokenType: 'ERC20' as TokenType, }, ]; @@ -47,7 +84,7 @@ describe('mergeUniqueTransactions', () => { status: MessageStatus.DONE, msgHash: 'msg3' as Hex, receipt: undefined, - tokenType: TokenType.ERC20, + tokenType: 'ERC20' as TokenType, }, { hash: 'hash4' as Hex, @@ -60,7 +97,7 @@ describe('mergeUniqueTransactions', () => { status: MessageStatus.DONE, msgHash: 'msg4' as Hex, receipt: undefined, - tokenType: TokenType.ERC20, + tokenType: 'ERC20' as TokenType, }, ]; @@ -102,7 +139,7 @@ describe('mergeUniqueTransactions', () => { status: MessageStatus.DONE, msgHash: 'msg2' as Hex, receipt: undefined, - tokenType: TokenType.ERC20, + tokenType: 'ERC20' as TokenType, }, ]; From a2b55e301879d0f1720233253666321062843354 Mon Sep 17 00:00:00 2001 From: Korbinian Kasberger Date: Tue, 12 Sep 2023 14:43:15 +0200 Subject: [PATCH 2/2] add relayer test --- .../libs/relayer/RelayerAPIService.test.ts | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 packages/bridge-ui-v2/src/libs/relayer/RelayerAPIService.test.ts diff --git a/packages/bridge-ui-v2/src/libs/relayer/RelayerAPIService.test.ts b/packages/bridge-ui-v2/src/libs/relayer/RelayerAPIService.test.ts new file mode 100644 index 0000000000..8ec8e3170d --- /dev/null +++ b/packages/bridge-ui-v2/src/libs/relayer/RelayerAPIService.test.ts @@ -0,0 +1,88 @@ +import axios from 'axios'; +import type { Address } from 'viem'; + +import { RelayerAPIService } from './RelayerAPIService'; + +vi.mock('axios'); + +function setupMocks() { + vi.mock('$customToken', () => { + return { + customToken: [ + { + name: 'Bull Token', + addresses: { + '31336': '0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0', + '167002': '0x9A9f2CCfdE556A7E9Ff0848998Aa4a0CFD8863AE', + }, + symbol: 'BLL', + decimals: 18, + type: 'ERC20', + logoURI: 'ipfs://QmezMTpT6ovJ3szb3SKDM9GVGeQ1R8DfjYyXG12ppMe2BY', + mintable: true, + }, + { + name: 'Horse Token', + addresses: { + '31336': '0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e', + '167002': '0x959922bE3CAee4b8Cd9a407cc3ac1C251C2007B1', + }, + symbol: 'HORSE', + decimals: 18, + type: 'ERC20', + logoURI: 'ipfs://QmU52ZxmSiGX24uDPNUGG3URyZr5aQdLpACCiD6tap4Mgc', + mintable: true, + }, + ], + }; + }); +} + +describe('RelayerAPIService', () => { + beforeEach(() => { + setupMocks(); + }); + + // Given + const mockedAxios = vi.mocked(axios, true); + + test('getTransactionsFromAPI should return API response', async () => { + // Given + const baseUrl = 'http://example.com'; + const relayerAPIService = new RelayerAPIService(baseUrl); + const params = { address: '0x123' as Address, chainID: 1, event: 'MessageSent' }; + const mockResponse = { + data: { + page: 1, + size: 10, + total: 100, + items: [], + }, + status: 200, + }; + mockedAxios.get.mockResolvedValue(mockResponse); + + // When + const result = await relayerAPIService.getTransactionsFromAPI(params); + + // Then + expect(result).toEqual(mockResponse.data); + }); + + test('getAllBridgeTransactionByAddress should return filtered transactions', async () => { + // Given + const baseUrl = 'http://example.com'; + const relayerAPIService = new RelayerAPIService(baseUrl); + const address = '0x123'; + const paginationParams = { page: 1, size: 10 }; + const chainID = 1; + + // When + const result = await relayerAPIService.getAllBridgeTransactionByAddress(address, paginationParams, chainID); + + // Then + expect(result).toBeDefined(); + expect(result.txs).toBeInstanceOf(Array); + expect(result.paginationInfo).toBeDefined(); + }); +});