Skip to content

Commit

Permalink
feat(tangle-dapp): Setup Tests, Fix Infinite Render on Restaking (#2674)
Browse files Browse the repository at this point in the history
  • Loading branch information
AtelyPham authored Nov 27, 2024
1 parent 91037e9 commit f44e4c1
Show file tree
Hide file tree
Showing 76 changed files with 714 additions and 753 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: test 🧪

on:
workflow_dispatch:

# TODO: Re-enable once @webb-tools/sdk-core is removed
# pull_request:
# branches: [develop, staging, master, feature/**]

# push:
# branches: [develop, staging, master, feature/**]

jobs:
build_code:
name: run
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['>=18.12.x']

steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ secrets.GITHUB_TOKEN }}

- uses: actions/checkout@v4

- name: Enable corepack
run: |
corepack enable
corepack prepare [email protected] --activate
yarn set version 4.2.2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}
cache: yarn

- name: Install deps
run: yarn install

- name: Run tests
run: yarn nx run-many --all --target=test
# Fix: JavaScript heap out of memory
# https://github.com/actions/runner-images/issues/70#issuecomment-1191708172
env:
NODE_OPTIONS: '--max_old_space_size=4096'
1 change: 1 addition & 0 deletions __mocks__/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'span';
1 change: 1 addition & 0 deletions __mocks__/svgUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '/svgUrl';
2 changes: 1 addition & 1 deletion apps/tangle-cloud/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
displayName: 'tangle-cloud',
preset: '../../jest.preset.js',
preset: '../../jest.preset.cjs',
transform: {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
'^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/next/babel'] }],
Expand Down
31 changes: 31 additions & 0 deletions apps/tangle-dapp/app/blueprints/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { act, render } from '@testing-library/react';
import { PolkadotApiProvider } from '@webb-tools/tangle-shared-ui/context/PolkadotApiContext';

import { RestakeContextProvider } from '../../context/RestakeContext';
import { mockMatchMedia } from '../test-utils';
import Page from './page';

describe('Blueprints Page', () => {
beforeAll(() => {
mockMatchMedia();
});

afterAll(() => {
jest.restoreAllMocks();
});

it('renders without crashing', async () => {
let container;
await act(async () => {
const result = render(
<PolkadotApiProvider>
<RestakeContextProvider>
<Page />
</RestakeContextProvider>
</PolkadotApiProvider>,
);
container = result.container;
});
expect(container).toBeInTheDocument();
});
});
14 changes: 7 additions & 7 deletions apps/tangle-dapp/app/bridge/ChainSelectors.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { calculateTypedChainId } from '@webb-tools/dapp-types/TypedChainId';
import { ArrowRight } from '@webb-tools/icons/ArrowRight';
import { calculateTypedChainId } from '@webb-tools/sdk-core/typed-chain-id';
import {
Label,
Modal,
Expand Down Expand Up @@ -88,11 +88,11 @@ const ChainSelectors: FC = () => {
]);

return (
<div className="flex flex-col md:flex-row justify-center md:justify-between items-center md:items-end md:gap-3">
<div className="flex flex-col items-center justify-center md:flex-row md:justify-between md:items-end md:gap-3">
{/* Source Chain Selector */}
<div className="flex flex-col gap-2 flex-1">
<div className="flex flex-col flex-1 gap-2">
<Label
className="text-mono-120 dark:text-mono-120 font-bold"
className="font-bold text-mono-120 dark:text-mono-120"
htmlFor="bridge-source-chain-selector"
>
From
Expand All @@ -108,16 +108,16 @@ const ChainSelectors: FC = () => {
</div>

<div
className="flex-shrink cursor-pointer px-1 pt-5 md:pt-0 md:pb-5"
className="flex-shrink px-1 pt-5 cursor-pointer md:pt-0 md:pb-5"
onClick={onSwitchChains}
>
<ArrowRight size="lg" className="rotate-90 md:rotate-0" />
</div>

{/* Destination Chain Selector */}
<div className="flex flex-col gap-2 flex-1">
<div className="flex flex-col flex-1 gap-2">
<Label
className="text-mono-120 dark:text-mono-120 font-bold"
className="font-bold text-mono-120 dark:text-mono-120"
htmlFor="bridge-destination-chain-selector"
>
To
Expand Down
2 changes: 1 addition & 1 deletion apps/tangle-dapp/app/bridge/hooks/useActionButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
useConnectWallet,
useWebContext,
} from '@webb-tools/api-provider-environment';
import { calculateTypedChainId } from '@webb-tools/sdk-core/typed-chain-id';
import { calculateTypedChainId } from '@webb-tools/dapp-types/TypedChainId';
import { useCallback, useMemo } from 'react';

import { useBridge } from '../../../context/BridgeContext';
Expand Down
2 changes: 1 addition & 1 deletion apps/tangle-dapp/app/bridge/hooks/useTypedChainId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { calculateTypedChainId } from '@webb-tools/sdk-core/typed-chain-id';
import { calculateTypedChainId } from '@webb-tools/dapp-types/TypedChainId';
import { useMemo } from 'react';

import { useBridge } from '../../../context/BridgeContext';
Expand Down
33 changes: 33 additions & 0 deletions apps/tangle-dapp/app/bridge/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { act, render } from '@testing-library/react';

import BridgeProvider from '../../context/BridgeContext';
import { mockMatchMedia } from '../test-utils';
import Page from './page';

const queryClient = new QueryClient();

describe('Bridge Page', () => {
beforeAll(() => {
mockMatchMedia();
});

afterAll(() => {
jest.restoreAllMocks();
});

it('renders without crashing', async () => {
let container;
await act(async () => {
const result = render(
<QueryClientProvider client={queryClient}>
<BridgeProvider>
<Page />
</BridgeProvider>
</QueryClientProvider>,
);
container = result.container;
});
expect(container).toBeInTheDocument();
});
});
14 changes: 14 additions & 0 deletions apps/tangle-dapp/app/claim/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { act, render } from '@testing-library/react';

import Page from './page';

describe('Claim Page', () => {
it('renders without crashing', async () => {
let container;
await act(async () => {
const result = render(<Page />);
container = result.container;
});
expect(container).toBeInTheDocument();
});
});
29 changes: 29 additions & 0 deletions apps/tangle-dapp/app/liquid-staking/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { act, render } from '@testing-library/react';

import Providers from '../providers';
import { mockFetch, mockMatchMedia } from '../test-utils';
import Page from './page';

describe('Liquid Staking Page', () => {
beforeAll(() => {
mockMatchMedia();
mockFetch({ country_code: 'US', state: 'CA' });
});

afterAll(() => {
jest.restoreAllMocks();
});

it('renders without crashing', async () => {
let container;
await act(async () => {
const result = render(
<Providers>
<Page />
</Providers>,
);
container = result.container;
});
expect(container).toBeInTheDocument();
});
});
29 changes: 29 additions & 0 deletions apps/tangle-dapp/app/nomination/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { act, render } from '@testing-library/react';

import Providers from '../providers';
import { mockFetch, mockMatchMedia } from '../test-utils';
import Page from './page';

describe('Nomination Page', () => {
beforeAll(() => {
mockMatchMedia();
mockFetch({ country_code: 'US', state: 'CA' });
});

afterAll(() => {
jest.restoreAllMocks();
});

it('renders without crashing', async () => {
let container;
await act(async () => {
const result = render(
<Providers>
<Page />
</Providers>,
);
container = result.container;
});
expect(container).toBeInTheDocument();
});
});
2 changes: 1 addition & 1 deletion apps/tangle-dapp/app/nomination/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const metadata: Metadata = createPageMetadata({
'Elevate your TNT tokens through NPoS by nominating validators on Tangle Network. Stake on EVM and Substrate to support network security and enjoy rewards.',
});

export default async function NominationPage() {
export default function NominationPage() {
return (
<div className="space-y-6 md:space-y-9 lg:space-y-12">
<div className="flex items-center justify-between">
Expand Down
29 changes: 29 additions & 0 deletions apps/tangle-dapp/app/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { act, render } from '@testing-library/react';

import Page from './page';
import Providers from './providers';
import { mockFetch, mockMatchMedia } from './test-utils';

describe('Account Page', () => {
beforeAll(() => {
mockMatchMedia();
mockFetch({ country_code: 'US', state: 'CA' });
});

afterAll(() => {
jest.restoreAllMocks();
});

it('renders without crashing', async () => {
let container;
await act(async () => {
const result = render(
<Providers>
<Page />
</Providers>,
);
container = result.container;
});
expect(container).toBeInTheDocument();
});
});
4 changes: 2 additions & 2 deletions apps/tangle-dapp/app/restake/deposit/DepositForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import { useWebContext } from '@webb-tools/api-provider-environment/webb-context';
import { ChainConfig } from '@webb-tools/dapp-config';
import { ZERO_BIG_INT } from '@webb-tools/dapp-config/constants';
import { calculateTypedChainId } from '@webb-tools/dapp-types/TypedChainId';
import isDefined from '@webb-tools/dapp-types/utils/isDefined';
import { calculateTypedChainId } from '@webb-tools/sdk-core';
import useRestakeOperatorMap from '@webb-tools/tangle-shared-ui/data/restake/useRestakeOperatorMap';
import { useRpcSubscription } from '@webb-tools/tangle-shared-ui/hooks/usePolkadotApi';
import useQueryState from '@webb-tools/tangle-shared-ui/hooks/useQueryState';
import { Card } from '@webb-tools/webb-ui-components';
import { type TokenListCardProps } from '@webb-tools/webb-ui-components/components/ListCard/types';
import { Modal } from '@webb-tools/webb-ui-components/components/Modal';
import { useModal } from '@webb-tools/webb-ui-components/hooks/useModal';
import { useQueryState } from 'nuqs';
import {
type ComponentProps,
useCallback,
Expand Down
19 changes: 19 additions & 0 deletions apps/tangle-dapp/app/restake/deposit/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { act, render } from '@testing-library/react';

import { mockMatchMedia } from '../../test-utils';
import Page from './page';

describe('Deposit Page', () => {
beforeAll(() => {
mockMatchMedia();
});

it('renders without crashing', async () => {
let container;
await act(async () => {
const result = render(<Page />);
container = result.container;
});
expect(container).toBeInTheDocument();
});
});
27 changes: 27 additions & 0 deletions apps/tangle-dapp/app/restake/operators/[address]/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { act, render } from '@testing-library/react';

import { mockMatchMedia } from '../../../test-utils';
import Page from './page';

const address = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';

describe('Operators Page', () => {
beforeAll(() => {
mockMatchMedia();
});

it('renders without crashing', async () => {
let container;
await act(async () => {
const result = render(
<Page
params={{
address,
}}
/>,
);
container = result.container;
});
expect(container).toBeInTheDocument();
});
});
5 changes: 1 addition & 4 deletions apps/tangle-dapp/app/restake/providers.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
'use client';

import { PolkadotApiProvider } from '@webb-tools/tangle-shared-ui/context/PolkadotApiContext';
import { NuqsAdapter } from 'nuqs/adapters/next/app';
import { type PropsWithChildren } from 'react';

import { RestakeContextProvider } from '../../context/RestakeContext';

export default function Providers({ children }: PropsWithChildren) {
return (
<PolkadotApiProvider>
<RestakeContextProvider>
<NuqsAdapter>{children}</NuqsAdapter>
</RestakeContextProvider>
<RestakeContextProvider>{children}</RestakeContextProvider>
</PolkadotApiProvider>
);
}
Loading

0 comments on commit f44e4c1

Please sign in to comment.