2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2023-09-30 11:45:52 +03:00
|
|
|
import type { UserEvent } from '@testing-library/user-event';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-06-04 11:20:24 +03:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ServerWithId } from '../../src/servers/data';
|
2023-09-05 10:08:42 +03:00
|
|
|
import { ManageServersRowDropdownFactory } from '../../src/servers/ManageServersRowDropdown';
|
2023-09-30 11:45:52 +03:00
|
|
|
import { checkAccessibility } from '../__helpers__/accessibility';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2021-10-23 11:55:52 +03:00
|
|
|
|
|
|
|
describe('<ManageServersRowDropdown />', () => {
|
2023-09-05 10:08:42 +03:00
|
|
|
const ManageServersRowDropdown = ManageServersRowDropdownFactory(fromPartial({
|
|
|
|
DeleteServerModal: ({ isOpen }: { isOpen: boolean }) => (
|
|
|
|
<span>DeleteServerModal {isOpen ? '[OPEN]' : '[CLOSED]'}</span>
|
|
|
|
),
|
|
|
|
}));
|
2023-05-27 12:57:26 +03:00
|
|
|
const setAutoConnect = vi.fn();
|
2022-06-04 11:20:24 +03:00
|
|
|
const setUp = (autoConnect = false) => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const server = fromPartial<ServerWithId>({ id: 'abc123', autoConnect });
|
2022-07-10 00:03:21 +03:00
|
|
|
return renderWithEvents(
|
|
|
|
<MemoryRouter>
|
|
|
|
<ManageServersRowDropdown setAutoConnect={setAutoConnect} server={server} />
|
|
|
|
</MemoryRouter>,
|
|
|
|
);
|
2021-10-23 11:55:52 +03:00
|
|
|
};
|
2023-09-30 11:45:52 +03:00
|
|
|
const toggleDropdown = (user: UserEvent) => user.click(screen.getByRole('button'));
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[setUp],
|
|
|
|
[async () => {
|
|
|
|
const { user, container } = setUp();
|
|
|
|
await toggleDropdown(user);
|
|
|
|
|
|
|
|
return { container };
|
|
|
|
}],
|
|
|
|
])('passes a11y checks', (setUp) => checkAccessibility(setUp()));
|
2021-10-23 11:55:52 +03:00
|
|
|
|
2022-06-04 11:20:24 +03:00
|
|
|
it('renders expected amount of dropdown items', async () => {
|
|
|
|
const { user } = setUp();
|
2021-10-23 11:55:52 +03:00
|
|
|
|
2022-06-04 11:20:24 +03:00
|
|
|
expect(screen.queryByRole('menu')).not.toBeInTheDocument();
|
2023-09-30 11:45:52 +03:00
|
|
|
await toggleDropdown(user);
|
2022-06-04 11:20:24 +03:00
|
|
|
expect(screen.getByRole('menu')).toBeInTheDocument();
|
|
|
|
|
|
|
|
expect(screen.getAllByRole('menuitem')).toHaveLength(4);
|
|
|
|
expect(screen.getByRole('menuitem', { name: 'Connect' })).toHaveAttribute('href', '/server/abc123');
|
|
|
|
expect(screen.getByRole('menuitem', { name: 'Edit server' })).toHaveAttribute('href', '/server/abc123/edit');
|
2021-10-23 11:55:52 +03:00
|
|
|
});
|
|
|
|
|
2022-06-04 11:20:24 +03:00
|
|
|
it('allows toggling auto-connect', async () => {
|
|
|
|
const { user } = setUp();
|
2021-10-23 11:55:52 +03:00
|
|
|
|
|
|
|
expect(setAutoConnect).not.toHaveBeenCalled();
|
2023-09-30 11:45:52 +03:00
|
|
|
await toggleDropdown(user);
|
2022-06-04 11:20:24 +03:00
|
|
|
await user.click(screen.getByRole('menuitem', { name: 'Auto-connect' }));
|
2021-10-23 11:55:52 +03:00
|
|
|
expect(setAutoConnect).toHaveBeenCalledWith(expect.objectContaining({ id: 'abc123' }), true);
|
|
|
|
});
|
|
|
|
|
2022-06-04 11:20:24 +03:00
|
|
|
it('renders deletion modal', async () => {
|
|
|
|
const { user } = setUp();
|
2021-10-23 11:55:52 +03:00
|
|
|
|
2022-06-04 11:20:24 +03:00
|
|
|
expect(screen.queryByText('DeleteServerModal [OPEN]')).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByText('DeleteServerModal [CLOSED]')).toBeInTheDocument();
|
2021-10-23 11:55:52 +03:00
|
|
|
|
2023-09-30 11:45:52 +03:00
|
|
|
await toggleDropdown(user);
|
2022-06-04 11:20:24 +03:00
|
|
|
await user.click(screen.getByRole('menuitem', { name: 'Remove server' }));
|
2021-10-23 11:55:52 +03:00
|
|
|
|
2022-06-04 11:20:24 +03:00
|
|
|
expect(screen.getByText('DeleteServerModal [OPEN]')).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText('DeleteServerModal [CLOSED]')).not.toBeInTheDocument();
|
2021-10-23 11:55:52 +03:00
|
|
|
});
|
2024-03-17 14:21:45 +03:00
|
|
|
|
|
|
|
it.each([[true], [false]])('renders expected size and icon', (autoConnect) => {
|
|
|
|
const { container } = setUp(autoConnect);
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
2021-10-23 11:55:52 +03:00
|
|
|
});
|