2024-03-06 10:47:16 +00:00
|
|
|
import { act, fireEvent, render, screen, within } from '@testing-library/react';
|
2022-03-31 17:16:30 +00:00
|
|
|
import { VegaWalletConnectButton } from './vega-wallet-connect-button';
|
2023-01-26 08:52:49 +00:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2024-03-01 14:25:56 +00:00
|
|
|
import {
|
|
|
|
mockConfig,
|
|
|
|
MockedWalletProvider,
|
|
|
|
} from '@vegaprotocol/wallet-react/testing';
|
2024-03-06 10:47:16 +00:00
|
|
|
import { MockedProvider, type MockedResponse } from '@apollo/react-testing';
|
|
|
|
import {
|
|
|
|
PartyProfilesDocument,
|
|
|
|
type PartyProfilesQuery,
|
|
|
|
} from './__generated__/PartyProfiles';
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2023-09-15 12:36:08 +00:00
|
|
|
jest.mock('../../lib/hooks/use-get-current-route-id', () => ({
|
|
|
|
useGetCurrentRouteId: jest.fn().mockReturnValue('current-route-id'),
|
|
|
|
}));
|
|
|
|
|
2024-03-06 10:47:16 +00:00
|
|
|
const key = { publicKey: '123456__123456', name: 'test' };
|
|
|
|
const key2 = { publicKey: 'abcdef__abcdef', name: 'test2' };
|
|
|
|
const keys = [key, key2];
|
|
|
|
const keyProfile = {
|
|
|
|
__typename: 'PartyProfile' as const,
|
|
|
|
partyId: key.publicKey,
|
|
|
|
alias: `${key.name} alias`,
|
|
|
|
metadata: [],
|
|
|
|
};
|
|
|
|
|
2024-03-01 14:25:56 +00:00
|
|
|
const renderComponent = (mockOnClick = jest.fn()) => {
|
2024-03-06 10:47:16 +00:00
|
|
|
const partyProfilesMock: MockedResponse<PartyProfilesQuery> = {
|
|
|
|
request: {
|
|
|
|
query: PartyProfilesDocument,
|
|
|
|
variables: { partyIds: keys.map((k) => k.publicKey) },
|
|
|
|
},
|
|
|
|
result: {
|
|
|
|
data: {
|
|
|
|
partiesProfilesConnection: {
|
|
|
|
__typename: 'PartiesProfilesConnection',
|
|
|
|
edges: [
|
|
|
|
{
|
|
|
|
__typename: 'PartyProfileEdge',
|
|
|
|
node: keyProfile,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-03-31 17:16:30 +00:00
|
|
|
return (
|
2024-03-06 10:47:16 +00:00
|
|
|
<MockedProvider mocks={[partyProfilesMock]}>
|
|
|
|
<MockedWalletProvider>
|
|
|
|
<VegaWalletConnectButton onClick={mockOnClick} />
|
|
|
|
</MockedWalletProvider>
|
|
|
|
</MockedProvider>
|
2022-03-31 17:16:30 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-04-20 11:56:48 +00:00
|
|
|
describe('VegaWalletConnectButton', () => {
|
2024-03-01 14:25:56 +00:00
|
|
|
afterEach(() => {
|
|
|
|
act(() => {
|
|
|
|
mockConfig.reset();
|
|
|
|
});
|
|
|
|
});
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2024-03-01 14:25:56 +00:00
|
|
|
it('should fire dialog when not connected', async () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
render(renderComponent(onClick));
|
2023-08-02 15:34:04 +00:00
|
|
|
const button = screen.getByTestId('connect-vega-wallet');
|
|
|
|
expect(button).toHaveTextContent('Get started');
|
2024-03-01 14:25:56 +00:00
|
|
|
await userEvent.click(button);
|
|
|
|
expect(onClick).toHaveBeenCalled();
|
2023-08-02 15:34:04 +00:00
|
|
|
});
|
|
|
|
|
2024-03-01 14:25:56 +00:00
|
|
|
it('should render "Connect" when browser wallet is detected', async () => {
|
2023-08-02 15:34:04 +00:00
|
|
|
window.vega = window.vega || ({} as Vega);
|
2024-03-01 14:25:56 +00:00
|
|
|
render(renderComponent());
|
2023-04-20 11:56:48 +00:00
|
|
|
const button = screen.getByTestId('connect-vega-wallet');
|
2023-07-31 16:08:55 +00:00
|
|
|
expect(button).toHaveTextContent('Connect');
|
2023-04-20 11:56:48 +00:00
|
|
|
});
|
|
|
|
|
2024-03-01 14:25:56 +00:00
|
|
|
it('should open dropdown and refresh keys when connected', async () => {
|
|
|
|
mockConfig.store.setState({
|
|
|
|
status: 'connected',
|
|
|
|
keys,
|
|
|
|
pubKey: key.publicKey,
|
|
|
|
});
|
|
|
|
|
|
|
|
const refreshKeys = jest.spyOn(mockConfig, 'refreshKeys');
|
|
|
|
const disconnect = jest.spyOn(mockConfig, 'disconnect');
|
|
|
|
const setPubKey = jest.spyOn(mockConfig.store, 'setState');
|
2023-04-20 11:56:48 +00:00
|
|
|
|
2024-03-01 14:25:56 +00:00
|
|
|
render(renderComponent());
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2024-03-01 14:25:56 +00:00
|
|
|
expect(screen.queryByTestId('connect-vega-wallet')).not.toBeInTheDocument();
|
2023-04-20 11:56:48 +00:00
|
|
|
const button = screen.getByTestId('manage-vega-wallet');
|
2024-03-06 10:47:16 +00:00
|
|
|
expect(button).toHaveTextContent(key.name);
|
2024-03-01 14:25:56 +00:00
|
|
|
|
|
|
|
fireEvent.click(button);
|
|
|
|
|
|
|
|
expect(await screen.findByRole('menu')).toBeInTheDocument();
|
2024-03-06 10:47:16 +00:00
|
|
|
const menuItems = await screen.findAllByRole('menuitemradio');
|
|
|
|
expect(menuItems).toHaveLength(keys.length);
|
|
|
|
|
|
|
|
expect(within(menuItems[0]).getByTestId('alias')).toHaveTextContent(
|
|
|
|
keyProfile.alias
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(within(menuItems[1]).getByTestId('alias')).toHaveTextContent(
|
|
|
|
'No alias'
|
2024-03-01 14:25:56 +00:00
|
|
|
);
|
2024-03-06 10:47:16 +00:00
|
|
|
|
2024-03-01 14:25:56 +00:00
|
|
|
expect(refreshKeys).toHaveBeenCalled();
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByTestId(`key-${key2.publicKey}`));
|
|
|
|
expect(setPubKey).toHaveBeenCalledWith({ pubKey: key2.publicKey });
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByTestId('disconnect'));
|
|
|
|
expect(disconnect).toHaveBeenCalled();
|
2023-04-20 11:56:48 +00:00
|
|
|
});
|
2022-03-31 17:16:30 +00:00
|
|
|
});
|