2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-05-17 23:31:02 +03:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2023-04-13 22:48:29 +03:00
|
|
|
import type { ServersMap } from '../../src/servers/data';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { ServersDropdown } from '../../src/servers/ServersDropdown';
|
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';
|
2018-06-14 20:42:42 +03:00
|
|
|
|
2018-07-22 23:49:51 +03:00
|
|
|
describe('<ServersDropdown />', () => {
|
2022-05-17 23:31:02 +03:00
|
|
|
const fallbackServers: ServersMap = {
|
2023-04-13 22:48:29 +03:00
|
|
|
'1a': fromPartial({ name: 'foo', id: '1a' }),
|
|
|
|
'2b': fromPartial({ name: 'bar', id: '2b' }),
|
|
|
|
'3c': fromPartial({ name: 'baz', id: '3c' }),
|
2019-10-05 20:51:50 +03:00
|
|
|
};
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (servers: ServersMap = fallbackServers) => renderWithEvents(
|
2023-09-30 11:45:52 +03:00
|
|
|
<MemoryRouter>
|
|
|
|
<ul>
|
|
|
|
<ServersDropdown servers={servers} selectedServer={null} />
|
|
|
|
</ul>
|
|
|
|
</MemoryRouter>,
|
2022-07-10 00:03:21 +03:00
|
|
|
);
|
2018-06-14 20:42:42 +03:00
|
|
|
|
2023-09-30 11:45:52 +03:00
|
|
|
it('passes a11y checks', () => checkAccessibility(setUp()));
|
|
|
|
|
2022-05-17 23:31:02 +03:00
|
|
|
it('contains the list of servers and the "mange servers" button', async () => {
|
|
|
|
const { user } = setUp();
|
|
|
|
|
|
|
|
await user.click(screen.getByText('Servers'));
|
|
|
|
const items = screen.getAllByRole('menuitem');
|
2023-11-01 12:07:50 +03:00
|
|
|
expect(items).toHaveLength(Object.values(fallbackServers).length + 1);
|
2022-05-17 23:31:02 +03:00
|
|
|
expect(items[0]).toHaveTextContent('foo');
|
|
|
|
expect(items[1]).toHaveTextContent('bar');
|
|
|
|
expect(items[2]).toHaveTextContent('baz');
|
|
|
|
expect(items[3]).toHaveTextContent('Manage servers');
|
|
|
|
});
|
2018-08-20 17:53:48 +03:00
|
|
|
|
2022-05-17 23:31:02 +03:00
|
|
|
it('contains a toggle with proper text', () => {
|
|
|
|
setUp();
|
|
|
|
expect(screen.getByRole('link')).toHaveTextContent('Servers');
|
|
|
|
});
|
2018-08-20 17:53:48 +03:00
|
|
|
|
2022-05-17 23:31:02 +03:00
|
|
|
it('contains a button to manage servers', async () => {
|
|
|
|
const { user } = setUp();
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2022-05-17 23:31:02 +03:00
|
|
|
await user.click(screen.getByText('Servers'));
|
|
|
|
expect(screen.getByRole('menuitem', { name: 'Manage servers' })).toHaveAttribute('href', '/manage-servers');
|
2018-06-14 20:42:42 +03:00
|
|
|
});
|
|
|
|
|
2022-05-17 23:31:02 +03:00
|
|
|
it('shows only create link when no servers exist yet', async () => {
|
|
|
|
const { user } = setUp({});
|
2018-08-20 17:53:48 +03:00
|
|
|
|
2022-05-17 23:31:02 +03:00
|
|
|
await user.click(screen.getByText('Servers'));
|
|
|
|
expect(screen.getByRole('menuitem')).toHaveTextContent('Add a server');
|
2018-06-14 20:42:42 +03:00
|
|
|
});
|
|
|
|
});
|