Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bridge-ui-v2): fix unit tests #14679

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions packages/bridge-ui-v2/src/libs/relayer/RelayerAPIService.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
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';

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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ const mockTokenVaultContract = {
},
} as unknown as GetContractResult<readonly unknown[], unknown>;

vi.mock('$libs/chain', () => ({
chainContractsMap: {
vi.mock('$bridgeConfig', () => ({
routingContractsMap: {
1: {
tokenVaultAddress: '0x00001',
2: {
erc20VaultAddress: '0x00001',
},
},
2: {
tokenVaultAddress: '0x00002',
1: {
erc20VaultAddress: '0x00002',
},
},
},
}));
Expand Down
Original file line number Diff line number Diff line change
@@ -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[] = [
{
Expand All @@ -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,
Expand All @@ -31,7 +68,7 @@ describe('mergeUniqueTransactions', () => {
status: MessageStatus.DONE,
msgHash: 'msg2' as Hex,
receipt: undefined,
tokenType: TokenType.ERC20,
tokenType: 'ERC20' as TokenType,
},
];

Expand All @@ -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,
Expand All @@ -60,7 +97,7 @@ describe('mergeUniqueTransactions', () => {
status: MessageStatus.DONE,
msgHash: 'msg4' as Hex,
receipt: undefined,
tokenType: TokenType.ERC20,
tokenType: 'ERC20' as TokenType,
},
];

Expand Down Expand Up @@ -102,7 +139,7 @@ describe('mergeUniqueTransactions', () => {
status: MessageStatus.DONE,
msgHash: 'msg2' as Hex,
receipt: undefined,
tokenType: TokenType.ERC20,
tokenType: 'ERC20' as TokenType,
},
];

Expand Down
Loading