2022-03-31 17:16:30 +00:00
|
|
|
import { fireEvent, render, screen } from '@testing-library/react';
|
|
|
|
import { VegaWalletContext } from '@vegaprotocol/wallet';
|
|
|
|
import type { VegaWalletContextShape } from '@vegaprotocol/wallet';
|
|
|
|
import { VegaWalletConnectButton } from './vega-wallet-connect-button';
|
2023-02-28 18:56:29 +00:00
|
|
|
import { truncateByChars } from '@vegaprotocol/utils';
|
2023-01-26 08:52:49 +00:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2022-10-14 15:42:53 +00:00
|
|
|
const mockUpdateDialogOpen = jest.fn();
|
|
|
|
jest.mock('@vegaprotocol/wallet', () => ({
|
|
|
|
...jest.requireActual('@vegaprotocol/wallet'),
|
2023-01-17 09:59:12 +00:00
|
|
|
useVegaWalletDialogStore: () => mockUpdateDialogOpen,
|
2022-10-14 15:42:53 +00:00
|
|
|
}));
|
2022-03-31 17:16:30 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-10-14 15:42:53 +00:00
|
|
|
jest.clearAllMocks();
|
2022-03-31 17:16:30 +00:00
|
|
|
});
|
|
|
|
|
2022-10-14 15:42:53 +00:00
|
|
|
const generateJsx = (context: VegaWalletContextShape) => {
|
2022-03-31 17:16:30 +00:00
|
|
|
return (
|
|
|
|
<VegaWalletContext.Provider value={context}>
|
2022-10-14 15:42:53 +00:00
|
|
|
<VegaWalletConnectButton />
|
2022-03-31 17:16:30 +00:00
|
|
|
</VegaWalletContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-04-20 11:56:48 +00:00
|
|
|
describe('VegaWalletConnectButton', () => {
|
|
|
|
it('should fire dialog when not connected', () => {
|
|
|
|
render(generateJsx({ pubKey: null } as VegaWalletContextShape));
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2023-04-20 11:56:48 +00:00
|
|
|
const button = screen.getByTestId('connect-vega-wallet');
|
|
|
|
expect(button).toHaveTextContent('Connect Vega wallet');
|
|
|
|
fireEvent.click(button);
|
|
|
|
expect(mockUpdateDialogOpen).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should retrieve keys when connected', async () => {
|
|
|
|
const pubKey = { publicKey: '123456__123456', name: 'test' };
|
|
|
|
const pubKey2 = { publicKey: '123456__123457', name: 'test2' };
|
|
|
|
render(
|
|
|
|
generateJsx({
|
|
|
|
pubKey: pubKey.publicKey,
|
|
|
|
pubKeys: [pubKey],
|
|
|
|
fetchPubKeys: () => Promise.resolve([pubKey, pubKey2]),
|
|
|
|
} as VegaWalletContextShape)
|
|
|
|
);
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2023-04-20 11:56:48 +00:00
|
|
|
const button = screen.getByTestId('manage-vega-wallet');
|
|
|
|
expect(button).toHaveTextContent(truncateByChars(pubKey.publicKey));
|
|
|
|
userEvent.click(button);
|
|
|
|
expect(mockUpdateDialogOpen).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fetch keys when connected', async () => {
|
|
|
|
const pubKey = { publicKey: '123456__123456', name: 'test' };
|
|
|
|
const context = {
|
2022-10-14 15:42:53 +00:00
|
|
|
pubKey: pubKey.publicKey,
|
|
|
|
pubKeys: [pubKey],
|
2023-04-20 11:56:48 +00:00
|
|
|
} as VegaWalletContextShape;
|
|
|
|
render(generateJsx(context));
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2023-04-20 11:56:48 +00:00
|
|
|
const button = screen.getByTestId('manage-vega-wallet');
|
|
|
|
expect(button).toHaveTextContent(truncateByChars(pubKey.publicKey));
|
|
|
|
userEvent.click(button);
|
|
|
|
expect(mockUpdateDialogOpen).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-03-31 17:16:30 +00:00
|
|
|
});
|