2022-10-03 18:12:34 +00:00
|
|
|
import { act, renderHook } from '@testing-library/react';
|
|
|
|
import type { Transaction } from './connectors';
|
2022-03-11 00:11:56 +00:00
|
|
|
import { RestConnector } from './connectors';
|
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';
|
2022-10-03 18:12:34 +00:00
|
|
|
import { LocalStorage } from '@vegaprotocol/react-helpers';
|
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import { WALLET_KEY } from './storage';
|
2022-03-11 00:11:56 +00:00
|
|
|
|
|
|
|
const restConnector = new RestConnector();
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
const setup = () => {
|
|
|
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
|
|
<VegaWalletProvider>{children}</VegaWalletProvider>
|
2022-03-11 00:11:56 +00:00
|
|
|
);
|
2022-10-03 18:12:34 +00:00
|
|
|
return renderHook(() => useVegaWallet(), { wrapper });
|
2022-03-11 00:11:56 +00:00
|
|
|
};
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
describe('VegaWalletProvider', () => {
|
|
|
|
afterAll(() => {
|
|
|
|
localStorage.clear();
|
|
|
|
});
|
2022-03-11 00:11:56 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
const mockPubKeys = [
|
|
|
|
{ publicKey: '111', name: 'public key 1' },
|
|
|
|
{ publicKey: '222', name: 'public key 2' },
|
|
|
|
];
|
|
|
|
const spyOnConnect = jest
|
2022-03-11 00:11:56 +00:00
|
|
|
.spyOn(restConnector, 'connect')
|
2022-10-03 18:12:34 +00:00
|
|
|
.mockImplementation(() => Promise.resolve(mockPubKeys));
|
|
|
|
const spyOnSend = jest
|
|
|
|
.spyOn(restConnector, 'sendTx')
|
|
|
|
.mockImplementation(() => Promise.resolve(null));
|
|
|
|
const storageSpy = jest.spyOn(LocalStorage, 'setItem');
|
|
|
|
const spyOnDisconnect = jest
|
2022-03-11 00:11:56 +00:00
|
|
|
.spyOn(restConnector, 'disconnect')
|
|
|
|
.mockImplementation(() => Promise.resolve());
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
it('connects, disconnects and retrieve keypairs', async () => {
|
|
|
|
const { result } = setup();
|
2022-03-11 00:11:56 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
// Default state
|
|
|
|
expect(result.current).toEqual({
|
|
|
|
pubKey: null,
|
|
|
|
pubKeys: null,
|
|
|
|
selectPubKey: expect.any(Function),
|
|
|
|
connect: expect.any(Function),
|
|
|
|
disconnect: expect.any(Function),
|
|
|
|
sendTx: expect.any(Function),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Connect
|
|
|
|
await act(async () => {
|
|
|
|
result.current.connect(restConnector);
|
|
|
|
});
|
|
|
|
expect(spyOnConnect).toHaveBeenCalled();
|
|
|
|
expect(result.current.pubKeys).toHaveLength(mockPubKeys.length);
|
|
|
|
expect(result.current.pubKey).toBe(mockPubKeys[0].publicKey);
|
|
|
|
|
|
|
|
// Change current pubkey
|
|
|
|
await act(async () => {
|
|
|
|
result.current.selectPubKey(mockPubKeys[1].publicKey);
|
|
|
|
});
|
|
|
|
expect(result.current.pubKey).toBe(mockPubKeys[1].publicKey);
|
|
|
|
expect(storageSpy).toHaveBeenCalledWith(
|
|
|
|
WALLET_KEY,
|
|
|
|
mockPubKeys[1].publicKey
|
|
|
|
);
|
2022-03-11 00:11:56 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
// Send tx
|
|
|
|
await act(async () => {
|
|
|
|
result.current.sendTx(mockPubKeys[1].publicKey, {} as Transaction);
|
|
|
|
});
|
|
|
|
expect(spyOnSend).toHaveBeenCalledWith(mockPubKeys[1].publicKey, {});
|
2022-03-11 00:11:56 +00:00
|
|
|
});
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
it('persists selected pubkey and disconnects', async () => {
|
|
|
|
const { result } = setup();
|
|
|
|
expect(result.current.pubKey).toBe(null);
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
result.current.connect(restConnector);
|
|
|
|
});
|
|
|
|
expect(result.current.pubKey).toBe(mockPubKeys[1].publicKey);
|
|
|
|
|
|
|
|
// Disconnect
|
|
|
|
await act(async () => {
|
|
|
|
result.current.disconnect();
|
|
|
|
});
|
|
|
|
expect(result.current.pubKey).toBe(null);
|
|
|
|
expect(result.current.pubKeys).toBe(null);
|
|
|
|
expect(spyOnDisconnect).toHaveBeenCalled();
|
|
|
|
expect(localStorage.getItem(WALLET_KEY)).toBe(null);
|
2022-03-11 00:11:56 +00:00
|
|
|
});
|
|
|
|
});
|