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-08-02 10:01:44 +03:00
|
|
|
import type { Domain } from '../../../src/domains/data';
|
|
|
|
import { DomainDropdown } from '../../../src/domains/helpers/DomainDropdown';
|
2023-08-04 12:16:01 +03:00
|
|
|
import { FeaturesProvider } from '../../../src/utils/features';
|
|
|
|
import { RoutesPrefixProvider } from '../../../src/utils/routesPrefix';
|
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);
|
2023-08-04 12:16:01 +03:00
|
|
|
const setUp = ({ domain, withVisits = true }: { domain?: Domain; withVisits?: boolean } = {}) => renderWithEvents(
|
2022-07-10 00:03:21 +03:00
|
|
|
<MemoryRouter>
|
2023-08-04 12:16:01 +03:00
|
|
|
<RoutesPrefixProvider value="/server/123">
|
|
|
|
<FeaturesProvider value={fromPartial({ domainVisits: withVisits })}>
|
|
|
|
<DomainDropdown
|
|
|
|
domain={domain ?? fromPartial({})}
|
|
|
|
editDomainRedirects={editDomainRedirects}
|
|
|
|
/>
|
|
|
|
</FeaturesProvider>
|
|
|
|
</RoutesPrefixProvider>
|
2022-07-10 00:03:21 +03:00
|
|
|
</MemoryRouter>,
|
|
|
|
);
|
2022-04-30 10:05:12 +03:00
|
|
|
|
|
|
|
it('renders expected menu items', () => {
|
2023-08-04 12:16:01 +03:00
|
|
|
setUp({ withVisits: false });
|
2022-04-30 10:05:12 +03:00
|
|
|
|
|
|
|
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) => {
|
2023-08-04 12:16:01 +03:00
|
|
|
setUp({ domain: fromPartial({ domain: 'foo.com', isDefault }) });
|
2022-04-30 10:05:12 +03:00
|
|
|
|
|
|
|
expect(screen.getByText('Visit stats')).toHaveAttribute('href', `/server/123/domain/foo.com${expectedLink}/visits`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
['foo.com'],
|
|
|
|
['bar.org'],
|
|
|
|
['baz.net'],
|
|
|
|
])('displays modal when editing redirects', async (domain) => {
|
2023-08-04 12:16:01 +03:00
|
|
|
const { user } = setUp({ domain: 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();
|
|
|
|
});
|
|
|
|
});
|