vega-frontend-monorepo/libs/wallet/src/provider.spec.tsx

99 lines
2.8 KiB
TypeScript
Raw Normal View History

import { act, fireEvent, render, screen } from '@testing-library/react';
import type { VegaKey } from './wallet-types';
2022-03-11 00:11:56 +00:00
import { RestConnector } from './connectors';
Feat/471 cancel order (#610) * chore: [#471] update @vegaprotocol/vegawallet-service-api-client to 0.4.12 * fix: [#471] set up storybook in order-list lib and add tailwind * fix: [#471] organize order list components * chore: [471] pull theme switcher changes * feat: [#471] add cancel order button * feat: [#471] initial impl of use order cancel hook * fix: [#471] fix format of the price in order list * fix: #471 fix static assets issue when merging * fix: #471 refactor order dialog to vega tx dialog * fix: #471 move use cancel order hook in wallet lib * fix: [#471] cancel order dialog and hook refactor * fix: [#471] remove commented code from storybook preview and fix test * fix: [#471] update order-list.tsx * fix: [#471] fix update subscription - show order is cancelled * fix: [#471] fix eslint error * chore: [#471] refactoring and add tests for dialogs and cancel hook * fix: #471 add ref to order list table * fix: #471 add field for cancel fix test * fix: #471 rename vega-order-transaction-dialog, error handiling, open dialog on finalized order * fix: #471 sendTx body mandatory * fix: #471 use BusEventType.Order to check the typename * fix: #471 revert using BusEventType.Order to check the typename * Update libs/wallet/src/order-hooks/use-order-cancel.tsx Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * fix: #471 fix order-list refactoring and fixes * fix: #471 generate orders added as a mock in order-list * fix: #471 reset transaction after order updated * fix: #471 remove unused import useEffect * fix: #471 generate mock orders * fix: #471 revert generate mock orders * fix: #471 order list price set to display all decimals * fix: #471 generate orders updates * fix: #471 remove unused import * fix: #471 remove __typename from mock orders genOrder * Update libs/wallet/src/order-hooks/order-event-query.ts Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * fix: #471 update order event sub and pull master changes Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>
2022-06-29 09:03:20 +00:00
import { useVegaWallet } from './use-vega-wallet';
2022-03-11 00:11:56 +00:00
import { VegaWalletProvider } from './provider';
import { WALLET_KEY } from './storage-keys';
2022-03-11 00:11:56 +00:00
const restConnector = new RestConnector();
afterAll(() => {
localStorage.clear();
});
const TestComponent = () => {
const { connect, disconnect, keypairs, keypair, selectPublicKey } =
useVegaWallet();
return (
<div data-testid="children">
<button
onClick={() => {
connect(restConnector);
}}
>
Connect
</button>
<button
onClick={() => {
disconnect();
}}
>
Disconnect
</button>
<p data-testid="current-keypair">{keypair?.pub}</p>
{keypairs?.length ? (
<ul data-testid="keypair-list">
{keypairs.map((kp) => (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-noninteractive-element-interactions
2022-03-11 00:11:56 +00:00
<li key={kp.pub} onClick={() => selectPublicKey(kp.pub)}>
{kp.pub}
</li>
))}
</ul>
) : null}
</div>
);
};
const generateJSX = () => (
<VegaWalletProvider>
<TestComponent />
</VegaWalletProvider>
);
it('Can connect, disconnect and retrieve keypairs', async () => {
2022-03-11 00:11:56 +00:00
const mockKeypairs = [{ pub: 'public key 1' }, { pub: 'public key 2' }];
jest
.spyOn(restConnector, 'connect')
.mockImplementation(() => Promise.resolve(mockKeypairs as VegaKey[]));
2022-03-11 00:11:56 +00:00
jest
.spyOn(restConnector, 'disconnect')
.mockImplementation(() => Promise.resolve());
const { unmount } = render(generateJSX());
expect(screen.getByTestId('children')).toBeInTheDocument();
await act(async () => {
fireEvent.click(screen.getByText('Connect'));
});
expect(screen.getByTestId('keypair-list').children).toHaveLength(
mockKeypairs.length
);
expect(screen.getByTestId('current-keypair')).toHaveTextContent(
mockKeypairs[0].pub
);
// Change current keypair
fireEvent.click(screen.getByTestId('keypair-list').children[1]);
expect(screen.getByTestId('current-keypair')).toHaveTextContent(
mockKeypairs[1].pub
);
// Current keypair should persist
unmount();
render(generateJSX());
await act(async () => {
fireEvent.click(screen.getByText('Connect'));
});
expect(screen.getByTestId('current-keypair')).toHaveTextContent(
mockKeypairs[1].pub
);
await act(async () => {
fireEvent.click(screen.getByText('Disconnect'));
});
expect(screen.getByTestId('current-keypair')).toBeEmptyDOMElement();
expect(screen.queryByTestId('keypairs-list')).not.toBeInTheDocument();
expect(localStorage.getItem(WALLET_KEY)).toBe(null);
2022-03-11 00:11:56 +00:00
});