shlink-web-client/test/domains/DomainSelector.test.tsx

66 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-11-28 14:36:40 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
import { DropdownItem, InputGroup } from 'reactstrap';
2020-11-28 14:36:40 +03:00
import { DomainSelector } from '../../src/domains/DomainSelector';
import { DomainsList } from '../../src/domains/reducers/domainsList';
import { ShlinkDomain } from '../../src/api/types';
import { DropdownBtn } from '../../src/utils/DropdownBtn';
2020-11-28 14:36:40 +03:00
describe('<DomainSelector />', () => {
let wrapper: ShallowWrapper;
const domainsList = Mock.of<DomainsList>({
domains: [
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' }),
],
});
const createWrapper = (value = '') => {
wrapper = shallow(
<DomainSelector value={value} domainsList={domainsList} listDomains={jest.fn()} onChange={jest.fn()} />,
);
2020-11-28 14:36:40 +03:00
return wrapper;
};
2020-11-28 14:36:40 +03:00
afterEach(jest.clearAllMocks);
afterEach(() => wrapper.unmount());
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'],
])('shows dropdown by default', (value, expectedText, expectedClassName) => {
const wrapper = createWrapper(value);
2020-11-28 14:36:40 +03:00
const input = wrapper.find(InputGroup);
const dropdown = wrapper.find(DropdownBtn);
2020-11-28 14:36:40 +03:00
expect(input).toHaveLength(0);
expect(dropdown).toHaveLength(1);
expect(dropdown.find(DropdownItem)).toHaveLength(5);
expect(dropdown.prop('text')).toEqual(expectedText);
expect(dropdown.prop('className')).toEqual(expectedClassName);
2020-11-28 14:36:40 +03:00
});
it('allows toggling between dropdown and input', () => {
const wrapper = createWrapper();
2020-11-28 14:36:40 +03:00
wrapper.find(DropdownItem).last().simulate('click');
expect(wrapper.find(InputGroup)).toHaveLength(1);
expect(wrapper.find(DropdownBtn)).toHaveLength(0);
2020-11-28 14:36:40 +03:00
wrapper.find('.domains-dropdown__back-btn').simulate('click');
expect(wrapper.find(InputGroup)).toHaveLength(0);
expect(wrapper.find(DropdownBtn)).toHaveLength(1);
2020-11-28 14:36:40 +03:00
});
it.each([
2022-03-26 14:17:42 +03:00
[0, 'default.com<span class="float-end text-muted">default</span>'],
[1, 'foo.com'],
[2, 'bar.com'],
])('shows expected content on every item', (index, expectedContent) => {
const item = createWrapper().find(DropdownItem).at(index);
expect(item.html()).toContain(expectedContent);
});
2020-11-28 14:36:40 +03:00
});