From 60c60dd418e02f7c3ee2b24dba15079ccb269b7e Mon Sep 17 00:00:00 2001 From: maciek Date: Fri, 8 Sep 2023 12:33:03 +0200 Subject: [PATCH] chore: standalone pages for withdraw and transfer - add some unit tests --- .../client-pages/withdraw/withdraw.spec.tsx | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 apps/trading/client-pages/withdraw/withdraw.spec.tsx diff --git a/apps/trading/client-pages/withdraw/withdraw.spec.tsx b/apps/trading/client-pages/withdraw/withdraw.spec.tsx new file mode 100644 index 000000000..261416f32 --- /dev/null +++ b/apps/trading/client-pages/withdraw/withdraw.spec.tsx @@ -0,0 +1,108 @@ +import { act, render, screen, waitFor } from '@testing-library/react'; +import { MockedProvider } from '@apollo/react-testing'; +import type { VegaWalletContextShape } from '@vegaprotocol/wallet'; +import { VegaWalletContext } from '@vegaprotocol/wallet'; +import { useWithdrawStore } from '@vegaprotocol/withdraws'; +import { MemoryRouter } from 'react-router-dom'; +import type { Asset } from '@vegaprotocol/assets'; +import { Withdraw } from './withdraw'; + +const mockWalletContext = {} as unknown as VegaWalletContextShape; + +jest.mock('../../components/withdraw-container', () => ({ + WithdrawContainer: jest.fn(({ assetId }: { assetId?: string }) => ( +
{assetId}
+ )), +})); + +let mockSearchParamsResult: string | undefined = undefined; +const mockSearchParams = { + get: jest.fn(() => mockSearchParamsResult), +}; +const mockSetSearchParams = jest.fn(); +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useSearchParams: jest.fn(() => [mockSearchParams, mockSetSearchParams]), +})); + +describe('Withdraw', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + it('should be properly rendered', async () => { + render( + + + + + + + + ); + expect( + screen.getByRole('heading', { level: 1, name: 'Withdraw' }) + ).toBeInTheDocument(); + expect(screen.getByTestId('assetId')).toBeEmptyDOMElement(); + }); + + it('assetId should be passed down', async () => { + mockSearchParamsResult = 'assetId'; + render( + + + + + + + + ); + expect( + screen.getByRole('heading', { level: 1, name: 'Withdraw' }) + ).toBeInTheDocument(); + expect(screen.getByTestId('assetId')).toHaveTextContent('assetId'); + }); + + it('is store returns different asset, search param should be cleared', async () => { + mockSearchParamsResult = 'assetId'; + render( + + + + + + + + ); + expect( + screen.getByRole('heading', { level: 1, name: 'Withdraw' }) + ).toBeInTheDocument(); + expect(screen.getByTestId('assetId')).toHaveTextContent('assetId'); + }); + + it('if store returns different asset, search param should be cleared', async () => { + useWithdrawStore.setState({ asset: { id: 'assetId' } as unknown as Asset }); + mockSearchParamsResult = 'assetId'; + render( + + + + + + + + ); + expect( + screen.getByRole('heading', { level: 1, name: 'Withdraw' }) + ).toBeInTheDocument(); + expect(screen.getByTestId('assetId')).toHaveTextContent('assetId'); + expect(mockSetSearchParams).not.toHaveBeenCalled(); + act(() => { + useWithdrawStore.setState({ + asset: { id: 'different-assetId' } as unknown as Asset, + }); + }); + await waitFor(() => { + expect(mockSetSearchParams).toHaveBeenCalledWith({}); + }); + }); +});