2022-07-10 00:03:21 +03:00
|
|
|
import { screen, waitForElementToBeRemoved } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-04-30 10:05:12 +03:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2023-04-13 22:48:29 +03:00
|
|
|
import type { SelectedServer } from '../../../src/servers/data';
|
2023-07-16 09:47:10 +03:00
|
|
|
import type { Domain } from '../../../src/shlink-web-component/domains/data';
|
|
|
|
import { DomainDropdown } from '../../../src/shlink-web-component/domains/helpers/DomainDropdown';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { SemVer } from '../../../src/utils/helpers/version';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
2022-04-30 10:05:12 +03:00
|
|
|
|
|
|
|
describe('<DomainDropdown />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const editDomainRedirects = vi.fn().mockResolvedValue(undefined);
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (domain?: Domain, selectedServer?: SelectedServer) => renderWithEvents(
|
|
|
|
<MemoryRouter>
|
|
|
|
<DomainDropdown
|
2023-04-13 22:48:29 +03:00
|
|
|
domain={domain ?? fromPartial({})}
|
|
|
|
selectedServer={selectedServer ?? fromPartial({})}
|
2022-07-10 00:03:21 +03:00
|
|
|
editDomainRedirects={editDomainRedirects}
|
|
|
|
/>
|
|
|
|
</MemoryRouter>,
|
|
|
|
);
|
2022-04-30 10:05:12 +03:00
|
|
|
|
|
|
|
it('renders expected menu items', () => {
|
|
|
|
setUp();
|
|
|
|
|
|
|
|
expect(screen.queryByText('Visit stats')).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByText('Edit redirects')).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[true, '_DEFAULT'],
|
|
|
|
[false, ''],
|
|
|
|
])('points first link to the proper section', (isDefault, expectedLink) => {
|
|
|
|
setUp(
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial({ domain: 'foo.com', isDefault }),
|
|
|
|
fromPartial({ version: '3.1.0', id: '123' }),
|
2022-04-30 10:05:12 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(screen.getByText('Visit stats')).toHaveAttribute('href', `/server/123/domain/foo.com${expectedLink}/visits`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[true, '2.9.0' as SemVer, false],
|
|
|
|
[true, '2.10.0' as SemVer, true],
|
|
|
|
[false, '2.9.0' as SemVer, true],
|
|
|
|
])('allows editing certain the domains', (isDefault, serverVersion, canBeEdited) => {
|
|
|
|
setUp(
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial({ domain: 'foo.com', isDefault }),
|
|
|
|
fromPartial({ version: serverVersion, id: '123' }),
|
2022-04-30 10:05:12 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (canBeEdited) {
|
|
|
|
expect(screen.getByText('Edit redirects')).not.toHaveAttribute('disabled');
|
|
|
|
} else {
|
|
|
|
expect(screen.getByText('Edit redirects')).toHaveAttribute('disabled');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
['foo.com'],
|
|
|
|
['bar.org'],
|
|
|
|
['baz.net'],
|
|
|
|
])('displays modal when editing redirects', async (domain) => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const { user } = setUp(fromPartial({ domain, isDefault: false }));
|
2022-04-30 10:05:12 +03:00
|
|
|
|
|
|
|
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByRole('form')).not.toBeInTheDocument();
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByText('Edit redirects'));
|
2022-04-30 10:05:12 +03:00
|
|
|
expect(await screen.findByRole('dialog')).toBeInTheDocument();
|
|
|
|
|
|
|
|
expect(editDomainRedirects).not.toHaveBeenCalled();
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByText('Save'));
|
2022-11-04 19:10:02 +03:00
|
|
|
expect(editDomainRedirects).toHaveBeenCalledWith(expect.objectContaining({ domain }));
|
2022-04-30 10:05:12 +03:00
|
|
|
|
|
|
|
await waitForElementToBeRemoved(() => screen.queryByRole('dialog'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays dropdown when clicked', async () => {
|
2022-05-11 20:18:43 +03:00
|
|
|
const { user } = setUp();
|
2022-04-30 10:05:12 +03:00
|
|
|
|
|
|
|
expect(screen.queryByRole('menu')).not.toBeInTheDocument();
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByRole('button', { expanded: false }));
|
2022-04-30 10:05:12 +03:00
|
|
|
expect(await screen.findByRole('menu')).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|