2022-07-10 00:03:21 +03:00
|
|
|
import { screen, waitFor } from '@testing-library/react';
|
2020-11-28 14:36:40 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { DomainSelector } from '../../src/domains/DomainSelector';
|
|
|
|
import { DomainsList } from '../../src/domains/reducers/domainsList';
|
2020-12-22 11:49:13 +03:00
|
|
|
import { ShlinkDomain } from '../../src/api/types';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2020-11-28 14:36:40 +03:00
|
|
|
|
|
|
|
describe('<DomainSelector />', () => {
|
|
|
|
const domainsList = Mock.of<DomainsList>({
|
|
|
|
domains: [
|
2021-09-20 22:51:51 +03:00
|
|
|
Mock.of<ShlinkDomain>({ domain: 'default.com', isDefault: true }),
|
2020-11-28 14:36:40 +03:00
|
|
|
Mock.of<ShlinkDomain>({ domain: 'foo.com' }),
|
|
|
|
Mock.of<ShlinkDomain>({ domain: 'bar.com' }),
|
|
|
|
],
|
|
|
|
});
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (value = '') => renderWithEvents(
|
|
|
|
<DomainSelector value={value} domainsList={domainsList} listDomains={jest.fn()} onChange={jest.fn()} />,
|
|
|
|
);
|
2020-11-28 14:36:40 +03:00
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
2021-09-20 22:51:51 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
['', 'Domain', 'domains-dropdown__toggle-btn'],
|
|
|
|
['my-domain.com', 'Domain: my-domain.com', 'domains-dropdown__toggle-btn--active'],
|
2022-05-09 20:03:19 +03:00
|
|
|
])('shows dropdown by default', async (value, expectedText, expectedClassName) => {
|
2022-05-11 20:18:43 +03:00
|
|
|
const { user } = setUp(value);
|
2022-05-09 20:03:19 +03:00
|
|
|
const btn = screen.getByRole('button', { name: expectedText });
|
|
|
|
|
|
|
|
expect(screen.queryByPlaceholderText('Domain')).not.toBeInTheDocument();
|
|
|
|
expect(btn).toHaveAttribute(
|
|
|
|
'class',
|
|
|
|
`dropdown-btn__toggle btn-block ${expectedClassName} dropdown-toggle btn btn-primary`,
|
|
|
|
);
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(btn);
|
2020-11-28 14:36:40 +03:00
|
|
|
|
2022-05-09 20:03:19 +03:00
|
|
|
await waitFor(() => expect(screen.getByRole('menu')).toBeInTheDocument());
|
|
|
|
expect(screen.getAllByRole('menuitem')).toHaveLength(4);
|
2020-11-28 14:36:40 +03:00
|
|
|
});
|
|
|
|
|
2022-05-09 20:03:19 +03:00
|
|
|
it('allows toggling between dropdown and input', async () => {
|
2022-05-11 20:18:43 +03:00
|
|
|
const { user } = setUp();
|
2021-09-20 22:51:51 +03:00
|
|
|
|
2022-05-09 20:03:19 +03:00
|
|
|
expect(screen.queryByPlaceholderText('Domain')).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByRole('button', { name: 'Domain' })).toBeInTheDocument();
|
2020-11-28 14:36:40 +03:00
|
|
|
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByRole('button', { name: 'Domain' }));
|
|
|
|
await user.click(await screen.findByText('New domain'));
|
2022-05-09 20:03:19 +03:00
|
|
|
|
|
|
|
expect(screen.getByPlaceholderText('Domain')).toBeInTheDocument();
|
|
|
|
expect(screen.queryByRole('button', { name: 'Domain' })).not.toBeInTheDocument();
|
|
|
|
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByRole('button', { name: 'Back to domains list' }));
|
2022-05-09 20:03:19 +03:00
|
|
|
|
|
|
|
expect(screen.queryByPlaceholderText('Domain')).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByRole('button', { name: 'Domain' })).toBeInTheDocument();
|
2020-11-28 14:36:40 +03:00
|
|
|
});
|
2021-09-20 22:51:51 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-05-09 20:03:19 +03:00
|
|
|
[0, 'default.comdefault'],
|
2022-03-26 14:17:42 +03:00
|
|
|
[1, 'foo.com'],
|
|
|
|
[2, 'bar.com'],
|
2022-05-09 20:03:19 +03:00
|
|
|
])('shows expected content on every item', async (index, expectedContent) => {
|
2022-05-11 20:18:43 +03:00
|
|
|
const { user } = setUp();
|
2022-05-09 20:03:19 +03:00
|
|
|
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByRole('button', { name: 'Domain' }));
|
2022-05-09 20:03:19 +03:00
|
|
|
const items = await screen.findAllByRole('menuitem');
|
2021-09-20 22:51:51 +03:00
|
|
|
|
2022-05-09 20:03:19 +03:00
|
|
|
expect(items[index]).toHaveTextContent(expectedContent);
|
2021-09-20 22:51:51 +03:00
|
|
|
});
|
2020-11-28 14:36:40 +03:00
|
|
|
});
|