2023-07-24 08:37:18 +00:00
|
|
|
import { act, render, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2023-08-03 11:14:35 +00:00
|
|
|
import {
|
|
|
|
Sidebar,
|
|
|
|
SidebarButton,
|
|
|
|
SidebarContent,
|
|
|
|
ViewType,
|
|
|
|
useSidebar,
|
|
|
|
} from './sidebar';
|
2023-07-24 08:37:18 +00:00
|
|
|
import { MemoryRouter, Route, Routes } from 'react-router-dom';
|
2023-08-03 11:14:35 +00:00
|
|
|
import { VegaIconNames } from '@vegaprotocol/ui-toolkit';
|
2023-08-31 02:40:04 +00:00
|
|
|
import type { VegaWalletContextShape } from '@vegaprotocol/wallet';
|
|
|
|
import { VegaWalletContext } from '@vegaprotocol/wallet';
|
2023-09-20 20:28:34 +00:00
|
|
|
import { Routes as AppRoutes } from '../../lib/links';
|
2023-07-24 08:37:18 +00:00
|
|
|
|
|
|
|
jest.mock('../node-health', () => ({
|
|
|
|
NodeHealthContainer: () => <span data-testid="node-health" />,
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('@vegaprotocol/accounts', () => ({
|
|
|
|
TransferContainer: () => <div data-testid="transfer" />,
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('@vegaprotocol/deposits', () => ({
|
|
|
|
DepositContainer: () => <div data-testid="deposit" />,
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('../settings', () => ({
|
|
|
|
Settings: () => <div data-testid="settings" />,
|
|
|
|
}));
|
|
|
|
|
2023-08-02 15:34:04 +00:00
|
|
|
jest.mock('../welcome-dialog', () => ({
|
|
|
|
GetStarted: () => <div data-testid="get-started" />,
|
|
|
|
}));
|
|
|
|
|
2023-08-31 02:40:04 +00:00
|
|
|
const walletContext = {
|
|
|
|
pubKeys: [{ publicKey: 'pubkey' }],
|
|
|
|
} as VegaWalletContextShape;
|
|
|
|
|
2023-07-24 08:37:18 +00:00
|
|
|
describe('Sidebar', () => {
|
2023-10-16 18:18:26 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
useSidebar.setState({ views: {} });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders options prop', () => {
|
2023-07-24 08:37:18 +00:00
|
|
|
render(
|
2023-08-31 02:40:04 +00:00
|
|
|
<VegaWalletContext.Provider value={walletContext}>
|
2023-10-16 18:18:26 +00:00
|
|
|
<MemoryRouter>
|
|
|
|
<Sidebar options={<div data-testid="options" />} />
|
2023-08-03 11:14:35 +00:00
|
|
|
</MemoryRouter>
|
2023-08-31 02:40:04 +00:00
|
|
|
</VegaWalletContext.Provider>
|
2023-07-24 08:37:18 +00:00
|
|
|
);
|
|
|
|
|
2023-08-03 11:14:35 +00:00
|
|
|
expect(screen.getByTestId(ViewType.ViewAs)).toBeInTheDocument();
|
2023-07-24 08:37:18 +00:00
|
|
|
expect(screen.getByTestId(ViewType.Settings)).toBeInTheDocument();
|
|
|
|
expect(screen.getByTestId('node-health')).toBeInTheDocument();
|
2023-10-16 18:18:26 +00:00
|
|
|
expect(screen.getByTestId('options')).toBeInTheDocument();
|
2023-07-24 08:37:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders selected state', async () => {
|
2023-10-16 18:18:26 +00:00
|
|
|
const routeId = '/markets/ABC';
|
2023-07-24 08:37:18 +00:00
|
|
|
render(
|
2023-08-31 02:40:04 +00:00
|
|
|
<VegaWalletContext.Provider value={walletContext}>
|
2023-10-16 18:18:26 +00:00
|
|
|
<MemoryRouter initialEntries={[routeId]}>
|
|
|
|
<Sidebar
|
|
|
|
options={
|
|
|
|
<SidebarButton
|
|
|
|
view={ViewType.Deposit}
|
|
|
|
icon={VegaIconNames.DEPOSIT}
|
|
|
|
tooltip="Deposit"
|
|
|
|
routeId={'/markets/:marketId'}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2023-08-03 11:14:35 +00:00
|
|
|
</MemoryRouter>
|
2023-08-31 02:40:04 +00:00
|
|
|
</VegaWalletContext.Provider>
|
2023-07-24 08:37:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const settingsButton = screen.getByTestId(ViewType.Settings);
|
2023-10-16 18:18:26 +00:00
|
|
|
const depositButton = screen.getByTestId(ViewType.Deposit);
|
2023-07-24 08:37:18 +00:00
|
|
|
|
|
|
|
// select settings first
|
|
|
|
await userEvent.click(settingsButton);
|
|
|
|
expect(settingsButton).toHaveClass('bg-vega-yellow text-black');
|
|
|
|
|
2023-10-16 18:18:26 +00:00
|
|
|
// switch to deposit
|
|
|
|
await userEvent.click(depositButton);
|
|
|
|
|
2023-07-24 08:37:18 +00:00
|
|
|
expect(settingsButton).not.toHaveClass('bg-vega-yellow text-black');
|
2023-10-16 18:18:26 +00:00
|
|
|
expect(depositButton).toHaveClass('bg-vega-yellow text-black');
|
2023-07-24 08:37:18 +00:00
|
|
|
|
|
|
|
// close order
|
2023-10-16 18:18:26 +00:00
|
|
|
await userEvent.click(depositButton);
|
|
|
|
expect(depositButton).not.toHaveClass('bg-vega-yellow text-black');
|
2023-07-24 08:37:18 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SidebarContent', () => {
|
2023-10-16 18:18:26 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
useSidebar.setState({ views: {} });
|
|
|
|
});
|
|
|
|
|
2023-07-24 08:37:18 +00:00
|
|
|
it('renders the correct content', () => {
|
|
|
|
const { container } = render(
|
2023-08-31 02:40:04 +00:00
|
|
|
<VegaWalletContext.Provider value={walletContext}>
|
2023-08-03 11:14:35 +00:00
|
|
|
<MemoryRouter initialEntries={['/markets/ABC']}>
|
|
|
|
<Routes>
|
2023-09-15 12:36:08 +00:00
|
|
|
<Route
|
|
|
|
path="/markets/:marketId"
|
|
|
|
id={AppRoutes.MARKET}
|
|
|
|
element={<SidebarContent />}
|
|
|
|
/>
|
2023-08-03 11:14:35 +00:00
|
|
|
</Routes>
|
|
|
|
</MemoryRouter>
|
2023-08-31 02:40:04 +00:00
|
|
|
</VegaWalletContext.Provider>
|
2023-07-24 08:37:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(container).toBeEmptyDOMElement();
|
|
|
|
|
|
|
|
act(() => {
|
2023-09-15 12:36:08 +00:00
|
|
|
useSidebar.setState({
|
|
|
|
views: { [AppRoutes.MARKET]: { type: ViewType.Transfer } },
|
|
|
|
});
|
2023-07-24 08:37:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(screen.getByTestId('transfer')).toBeInTheDocument();
|
|
|
|
|
|
|
|
act(() => {
|
2023-09-15 12:36:08 +00:00
|
|
|
useSidebar.setState({
|
|
|
|
views: { [AppRoutes.MARKET]: { type: ViewType.Deposit } },
|
|
|
|
});
|
2023-07-24 08:37:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(screen.getByTestId('deposit')).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('closes sidebar if market id is required but not present', () => {
|
|
|
|
const { container } = render(
|
2023-08-31 02:40:04 +00:00
|
|
|
<VegaWalletContext.Provider value={walletContext}>
|
2023-08-03 11:14:35 +00:00
|
|
|
<MemoryRouter initialEntries={['/portfolio']}>
|
|
|
|
<Routes>
|
2023-09-15 12:36:08 +00:00
|
|
|
<Route
|
|
|
|
path="/portfolio"
|
|
|
|
id={AppRoutes.PORTFOLIO}
|
|
|
|
element={<SidebarContent />}
|
|
|
|
/>
|
2023-08-03 11:14:35 +00:00
|
|
|
</Routes>
|
|
|
|
</MemoryRouter>
|
2023-08-31 02:40:04 +00:00
|
|
|
</VegaWalletContext.Provider>
|
2023-07-24 08:37:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
act(() => {
|
2023-09-15 12:36:08 +00:00
|
|
|
useSidebar.setState({
|
|
|
|
views: { [AppRoutes.PORTFOLIO]: { type: ViewType.Order } },
|
|
|
|
});
|
2023-07-24 08:37:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(container).toBeEmptyDOMElement();
|
|
|
|
|
|
|
|
act(() => {
|
2023-09-15 12:36:08 +00:00
|
|
|
useSidebar.setState({
|
|
|
|
views: { [AppRoutes.PORTFOLIO]: { type: ViewType.Settings } },
|
|
|
|
});
|
2023-07-24 08:37:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(screen.getByTestId('settings')).toBeInTheDocument();
|
|
|
|
|
|
|
|
act(() => {
|
2023-09-15 12:36:08 +00:00
|
|
|
useSidebar.setState({
|
|
|
|
views: { [AppRoutes.PORTFOLIO]: { type: ViewType.Info } },
|
|
|
|
});
|
2023-07-24 08:37:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(container).toBeEmptyDOMElement();
|
|
|
|
});
|
|
|
|
});
|
2023-08-03 11:14:35 +00:00
|
|
|
|
|
|
|
describe('SidebarButton', () => {
|
|
|
|
it.each([ViewType.Info, ViewType.Deposit, ViewType.ViewAs])(
|
|
|
|
'runs given callback regardless of requested view (%s)',
|
|
|
|
async (view) => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
render(
|
|
|
|
<SidebarButton
|
|
|
|
icon={VegaIconNames.INFO}
|
|
|
|
tooltip="INFO"
|
|
|
|
onClick={onClick}
|
|
|
|
view={view}
|
2023-09-15 12:36:08 +00:00
|
|
|
routeId="current-route-id"
|
2023-08-03 11:14:35 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const btn = screen.getByTestId(view);
|
|
|
|
await userEvent.click(btn);
|
|
|
|
|
|
|
|
expect(onClick).toBeCalled();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|