Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jscriptcoder committed Jun 2, 2023
1 parent 0b24ae9 commit 97fe258
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2,269 deletions.
8 changes: 4 additions & 4 deletions packages/bridge-ui/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export default {
// - relayer-api/RelayerAPIService (partial test coverage)
// - bridge/ERC20Bridge (partial test coverage)
// - bridge/ETHBridge (partial test coverage)
statements: 92,
branches: 91,
functions: 96,
lines: 92,
statements: 93,
branches: 92,
functions: 97,
lines: 93,
},
},
modulePathIgnorePatterns: ['<rootDir>/public/build/'],
Expand Down
97 changes: 79 additions & 18 deletions packages/bridge-ui/src/relayer-api/RelayerAPIService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const mockContract = {
getMessageStatus: jest.fn(),
symbol: jest.fn(),
filters: {
// Returns this string to help us
// identify the filter in the tests
ERC20Sent: () => 'ERC20Sent',
ERC20Sent: () => jest.fn().mockReturnValue('ERC20Sent'),
},
};

Expand All @@ -46,32 +44,23 @@ const mockTxReceipt = {
blockNumber: 1,
} as ethers.providers.TransactionReceipt;

const mockEvent = {
args: {
message: {
owner: '0x123',
},
msgHash: '0x456',
amount: '100',
},
};

const mockErc20Event = {
args: {
token: '0x123',
amount: '100',
msgHash: '0x456',
msgHash:
'0x289d8464b91c2f31f6170942f29cdb815148dc527fbbbcd5ff158c0f5b9ac766',
message: {
owner: '0x123',
owner: walletAddress,
data: '0x789',
},
},
};

const mockQuery = [mockEvent];

const mockErc20Query = [mockErc20Event];

const baseUrl = RELAYER_URL.replace(/\/$/, '');

const relayerApi = new RelayerAPIService(RELAYER_URL, mockProviders);

describe('RelayerAPIService', () => {
Expand All @@ -87,7 +76,7 @@ describe('RelayerAPIService', () => {
.mockResolvedValue(mockTxReceipt);

mockContract.getMessageStatus.mockResolvedValue(MessageStatus.New);
mockContract.queryFilter.mockResolvedValue(mockQuery);
mockContract.queryFilter.mockResolvedValue(mockErc20Query);
mockContract.symbol.mockResolvedValue('BLL');
});

Expand Down Expand Up @@ -199,10 +188,82 @@ describe('RelayerAPIService', () => {
},
);

expect(txs.length).toBeGreaterThanOrEqual(1);

const chainIds = txs.map((tx) => tx.message.srcChainId);
expect(chainIds).not.toContain(L1_CHAIN_ID);
});

it('should get all transactions by address', async () => {
jest.mocked(axios.get).mockResolvedValueOnce({
data: eventsJson,
});

const { txs } = await relayerApi.getAllBridgeTransactionByAddress(
walletAddress,
{
page: 0,
size: 100,
},
);

expect(
mockProviders[L1_CHAIN_ID].getTransactionReceipt,
).toHaveBeenCalledTimes(3);

expect(
mockProviders[L2_CHAIN_ID].getTransactionReceipt,
).toHaveBeenCalledTimes(1);

expect(mockContract.getMessageStatus).toHaveBeenCalledTimes(4);
expect(mockContract.queryFilter).toHaveBeenCalledTimes(1);
expect(mockContract.symbol).toHaveBeenCalledTimes(1);
});

it('should not get transactions with wrong address', async () => {
jest.mocked(axios.get).mockResolvedValueOnce({
data: eventsJson,
});

const { txs } = await relayerApi.getAllBridgeTransactionByAddress(
'0xWrongAddress',
{
page: 0,
size: 100,
},
);

expect(txs.length).toEqual(0);
});

it('ignores transactions from chains not supported by the bridge', async () => {
// TODO: use structuredClone(). Nodejs support?
const noSupportedTx = JSON.parse(JSON.stringify(eventsJson.items[0]));
noSupportedTx.data.Message.SrcChainId = 666;

const newEventsJson = {
...eventsJson,
items: [noSupportedTx, ...eventsJson.items.slice(1)],
};

jest.mocked(axios.get).mockResolvedValueOnce({
data: newEventsJson,
});

const { txs } = await relayerApi.getAllBridgeTransactionByAddress(
walletAddress,
{
page: 0,
size: 100,
},
);

// expected = total_items - duplicated_tx - unsupported_tx
expect(txs.length).toEqual(eventsJson.items.length - 1 - 1);
});

// TODO: there are still some branches to cover here

it('should get block info', async () => {
jest.mocked(axios.get).mockResolvedValue({
data: blockInfoJson,
Expand Down
4 changes: 2 additions & 2 deletions packages/bridge-ui/src/relayer-api/RelayerAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ export class RelayerAPIService implements RelayerAPI {
return { txs: [], paginationInfo };
}

apiTxs.items = RelayerAPIService._filterDuplicateHashes(apiTxs.items);
const items = RelayerAPIService._filterDuplicateHashes(apiTxs.items);

const txs = apiTxs.items.map((tx: APIResponseTransaction) => {
const txs = items.map((tx: APIResponseTransaction) => {
let data = tx.data.Message.Data;
if (data === '') {
data = '0x'; // ethers does not allow "" for empty bytes value
Expand Down
Loading

0 comments on commit 97fe258

Please sign in to comment.