Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeron committed Jan 30, 2025
1 parent b9225a8 commit 467efd3
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 28 deletions.
144 changes: 119 additions & 25 deletions packages/app/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { webcrypto } from 'crypto';
// biome-ignore lint/style/useNodejsImportProtocol: <explanation>
import { TextDecoder, TextEncoder } from 'util';

import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { localStorageMock } from './src/mocks/localStorage';

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
Expand All @@ -21,6 +22,123 @@ import 'whatwg-fetch';

import { act } from 'react';

// Initialize the MSW server with the necessary request handlers
const server = setupServer(
rest.get('/convert_rate', (_req, res, ctx) => {
return res(ctx.status(200), ctx.json({ amount: '$0.00' }));
}),
rest.get('/assets.json', (_req, res, ctx) => {
return res(
ctx.status(200),
ctx.json([
{
name: 'Ethereum',
symbol: 'ETH',
icon: 'https://verified-assets.fuel.network/images/eth.svg',
networks: [
{
type: 'ethereum',
chain: 'sepolia',
decimals: 18,
chainId: 11155111,
},
{
type: 'ethereum',
chain: 'foundry',
decimals: 18,
chainId: 31337,
},
{
type: 'ethereum',
chain: 'mainnet',
decimals: 18,
chainId: 1,
},
{
type: 'fuel',
chain: 'devnet',
decimals: 9,
assetId:
'0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07',
chainId: 0,
},
{
type: 'fuel',
chain: 'testnet',
decimals: 9,
assetId:
'0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07',
chainId: 0,
},
{
type: 'fuel',
chain: 'mainnet',
decimals: 9,
assetId:
'0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07',
chainId: 9889,
},
],
},
{
name: 'Fuel',
symbol: 'FUEL',
icon: 'https://verified-assets.fuel.network/images/fuel.svg',
networks: [
{
type: 'ethereum',
chain: 'sepolia',
address: '0xd7fc4e8fb2c05567c313f4c9b9e07641a361a550',
decimals: 9,
chainId: 11155111,
},
{
type: 'ethereum',
chain: 'mainnet',
address: '0x675b68aa4d9c2d3bb3f0397048e62e6b7192079c',
decimals: 9,
chainId: 1,
},
{
type: 'fuel',
chain: 'testnet',
decimals: 9,
chainId: 0,
contractId:
'0xd02112ef9c39f1cea7c8527c26242ca1f5d26bcfe8d1564bee054d3b04175471',
subId:
'0xede43647e2aad1c0f1696201d6ba913aa67c917c3ac9a4a7d95662962ab25c5b',
assetId:
'0x324d0c35a4299ef88138a656d5272c5a3a9ccde2630ae055dacaf9d13443d53b',
},
{
type: 'fuel',
chain: 'mainnet',
decimals: 9,
chainId: 9889,
contractId:
'0x4ea6ccef1215d9479f1024dff70fc055ca538215d2c8c348beddffd54583d0e8',
subId:
'0xe81c89b8cf795c7c25e79f6c4f2f1cd233290b58e217ed4e9b6b18538badddaf',
assetId:
'0x1d5d97005e41cae2187a895fd8eab0506111e0e2f3331cd3912c15c24e3c1d82',
},
],
},
])
);
})
);

// Establish API mocking before all tests
beforeAll(() => server.listen());

// Reset any request handlers that are declared as a part of our tests (i.e., for testing one-time error scenarios)
afterEach(() => server.resetHandlers());

// Clean up after the tests are finished
afterAll(() => server.close());

// Replace ReactDOMTestUtils.act with React.act
jest.mock('react-dom/test-utils', () => {
const originalModule = jest.requireActual('react-dom/test-utils');
Expand Down Expand Up @@ -81,27 +199,3 @@ if (process.env.CI) {
logErrorsBeforeRetry: true,
});
}

const _mockNetworks = [
{
asset_id: 'TKN',
name: 'Token',
type: 'token',
symbol: 'TKN',
decimals: 9,
},
{
asset_id: 'ETH',
name: 'Ethereum',
type: 'token',
symbol: 'ETH',
decimals: 18,
},
{
asset_id: 'Fuel',
name: 'Fuel',
type: 'token',
symbol: 'Fuel',
decimals: 9,
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('BalanceWidget', () => {

it('should show formatted balance', async () => {
renderWithProvider(<BalanceWidget account={ACCOUNT} />);
expect(screen.getByText(/4\.999/)).toBeInTheDocument();
expect(screen.getByText('$0.00')).toBeInTheDocument();
});

it('should hide balance when user sets his balance to hide', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ describe('TxFee', () => {

it('should be able to show the transaction fee', async () => {
const feeCost = bn(6);
render(<TxFee fee={feeCost} />);
render(<TxFee fee={feeCost} tipInUsd="$0.01" />);
expect(await screen.findByText(/fee \(network\)/i)).toBeInTheDocument();
const valFee = screen.getByLabelText(/Fee value/i);
expect(valFee).toBeInTheDocument();
expect(valFee.innerHTML.trim()).toBe(`${feeCost.format()} ETH`);
expect(valFee.innerHTML.trim()).toBe(`(${feeCost.format()} ETH)`);

const valFeeUsd = screen.getByLabelText(/tip in usd/i);
expect(valFeeUsd).toBeInTheDocument();
expect(valFeeUsd.innerHTML.trim()).toBe('$0.01');
});
});

0 comments on commit 467efd3

Please sign in to comment.