2023-08-24 08:03:56 +00:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2023-08-07 10:35:55 +00:00
|
|
|
import type { VegaWalletContextShape } from '@vegaprotocol/wallet';
|
|
|
|
import { VegaWalletContext } from '@vegaprotocol/wallet';
|
|
|
|
import { GetStarted } from './get-started';
|
2023-09-14 19:10:33 +00:00
|
|
|
import { render, screen, fireEvent } from '@testing-library/react';
|
|
|
|
import { useOnboardingStore } from './use-get-onboarding-step';
|
2023-08-07 10:35:55 +00:00
|
|
|
|
2023-08-24 08:03:56 +00:00
|
|
|
let mockStep = 1;
|
|
|
|
jest.mock('./use-get-onboarding-step', () => ({
|
|
|
|
...jest.requireActual('./use-get-onboarding-step'),
|
|
|
|
useGetOnboardingStep: jest.fn(() => mockStep),
|
|
|
|
}));
|
|
|
|
|
2023-08-07 10:35:55 +00:00
|
|
|
describe('GetStarted', () => {
|
|
|
|
const renderComponent = (context: Partial<VegaWalletContextShape> = {}) => {
|
|
|
|
return render(
|
2023-08-24 08:03:56 +00:00
|
|
|
<MemoryRouter>
|
|
|
|
<VegaWalletContext.Provider value={context as VegaWalletContextShape}>
|
|
|
|
<GetStarted />
|
|
|
|
</VegaWalletContext.Provider>
|
|
|
|
</MemoryRouter>
|
2023-08-07 10:35:55 +00:00
|
|
|
);
|
|
|
|
};
|
2023-08-24 08:03:56 +00:00
|
|
|
const checkTicks = (elements: Element[]) => {
|
|
|
|
elements.forEach((item, i) => {
|
2023-09-12 15:07:37 +00:00
|
|
|
if (i + 1 < mockStep - 1) {
|
2023-08-24 08:03:56 +00:00
|
|
|
expect(item.querySelector('[data-testid="icon-tick"]')).toBeTruthy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
2023-08-07 10:35:55 +00:00
|
|
|
|
|
|
|
it('renders full get started content if not connected and no browser wallet detected', () => {
|
|
|
|
renderComponent();
|
|
|
|
expect(screen.getByTestId('get-started-banner')).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders connect prompt if no pubKey but wallet installed', () => {
|
|
|
|
globalThis.window.vega = {} as Vega;
|
|
|
|
renderComponent();
|
2023-08-24 08:03:56 +00:00
|
|
|
expect(screen.getByTestId('get-started-banner')).toBeInTheDocument();
|
2023-08-07 10:35:55 +00:00
|
|
|
globalThis.window.vega = undefined as unknown as Vega;
|
|
|
|
});
|
|
|
|
|
2023-09-14 19:10:33 +00:00
|
|
|
it('renders nothing if dismissed', () => {
|
|
|
|
useOnboardingStore.setState({ dismissed: true });
|
2023-08-24 08:03:56 +00:00
|
|
|
mockStep = 0;
|
2023-08-07 10:35:55 +00:00
|
|
|
const { container } = renderComponent({ pubKey: 'my-pubkey' });
|
|
|
|
expect(container).toBeEmptyDOMElement();
|
|
|
|
});
|
2023-08-24 08:03:56 +00:00
|
|
|
|
|
|
|
it('steps should be ticked', () => {
|
2023-09-14 19:10:33 +00:00
|
|
|
useOnboardingStore.setState({ dismissed: false });
|
2023-08-24 08:03:56 +00:00
|
|
|
const navigatorGetter: jest.SpyInstance = jest.spyOn(
|
|
|
|
window.navigator,
|
|
|
|
'userAgent',
|
|
|
|
'get'
|
|
|
|
);
|
|
|
|
navigatorGetter.mockReturnValue('Chrome');
|
|
|
|
|
|
|
|
mockStep = 2;
|
2023-09-12 15:07:37 +00:00
|
|
|
const { rerender, container } = renderComponent();
|
2023-08-24 08:03:56 +00:00
|
|
|
checkTicks(screen.getAllByRole('listitem'));
|
|
|
|
expect(screen.getByRole('button', { name: 'Connect' })).toBeInTheDocument();
|
|
|
|
|
|
|
|
mockStep = 3;
|
|
|
|
rerender(
|
|
|
|
<MemoryRouter>
|
|
|
|
<VegaWalletContext.Provider value={{} as VegaWalletContextShape}>
|
|
|
|
<GetStarted />
|
|
|
|
</VegaWalletContext.Provider>
|
|
|
|
</MemoryRouter>
|
|
|
|
);
|
|
|
|
checkTicks(screen.getAllByRole('listitem'));
|
2023-09-15 14:10:38 +00:00
|
|
|
expect(screen.getByRole('link', { name: 'Deposit' })).toBeInTheDocument();
|
2023-08-24 08:03:56 +00:00
|
|
|
|
|
|
|
mockStep = 4;
|
|
|
|
rerender(
|
|
|
|
<MemoryRouter>
|
|
|
|
<VegaWalletContext.Provider value={{} as VegaWalletContextShape}>
|
|
|
|
<GetStarted />
|
|
|
|
</VegaWalletContext.Provider>
|
|
|
|
</MemoryRouter>
|
|
|
|
);
|
|
|
|
checkTicks(screen.getAllByRole('listitem'));
|
2023-09-12 15:07:37 +00:00
|
|
|
expect(
|
2023-09-15 14:10:38 +00:00
|
|
|
screen.getByRole('link', { name: 'Ready to trade' })
|
2023-09-12 15:07:37 +00:00
|
|
|
).toBeInTheDocument();
|
2023-08-24 08:03:56 +00:00
|
|
|
|
2023-09-15 14:10:38 +00:00
|
|
|
fireEvent.click(screen.getByRole('link', { name: 'Ready to trade' }));
|
2023-09-14 19:10:33 +00:00
|
|
|
|
2023-08-24 08:03:56 +00:00
|
|
|
mockStep = 5;
|
|
|
|
rerender(
|
|
|
|
<MemoryRouter>
|
|
|
|
<VegaWalletContext.Provider
|
|
|
|
value={{ pubKey: 'my-pubkey' } as VegaWalletContextShape}
|
|
|
|
>
|
|
|
|
<GetStarted />
|
|
|
|
</VegaWalletContext.Provider>
|
|
|
|
</MemoryRouter>
|
|
|
|
);
|
|
|
|
expect(container).toBeEmptyDOMElement();
|
|
|
|
});
|
2023-08-07 10:35:55 +00:00
|
|
|
});
|