2022-03-14 13:18:11 +00:00
|
|
|
import { act, fireEvent, render, screen } from '@testing-library/react';
|
2022-03-30 09:49:48 +00:00
|
|
|
import type { VegaKey } from '@vegaprotocol/vegawallet-service-api-client';
|
2022-03-11 00:11:56 +00:00
|
|
|
import { RestConnector } from './connectors';
|
|
|
|
import { useVegaWallet } from './hooks';
|
|
|
|
import { VegaWalletProvider } from './provider';
|
2022-03-25 07:43:49 +00:00
|
|
|
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) => (
|
|
|
|
<li key={kp.pub} onClick={() => selectPublicKey(kp.pub)}>
|
|
|
|
{kp.pub}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateJSX = () => (
|
|
|
|
<VegaWalletProvider>
|
|
|
|
<TestComponent />
|
|
|
|
</VegaWalletProvider>
|
|
|
|
);
|
|
|
|
|
2022-05-12 12:32:14 +00:00
|
|
|
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')
|
2022-03-20 01:31:44 +00:00
|
|
|
.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();
|
2022-03-25 07:43:49 +00:00
|
|
|
expect(localStorage.getItem(WALLET_KEY)).toBe(null);
|
2022-03-11 00:11:56 +00:00
|
|
|
});
|