2020-11-28 14:36:40 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
2020-12-25 12:29:25 +03:00
|
|
|
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';
|
2020-12-22 11:49:13 +03:00
|
|
|
import { ShlinkDomain } from '../../src/api/types';
|
2020-12-25 12:43:36 +03:00
|
|
|
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: [
|
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' }),
|
|
|
|
],
|
|
|
|
});
|
2021-09-20 22:51:51 +03:00
|
|
|
const createWrapper = (value = '') => {
|
|
|
|
wrapper = shallow(
|
|
|
|
<DomainSelector value={value} domainsList={domainsList} listDomains={jest.fn()} onChange={jest.fn()} />,
|
|
|
|
);
|
2020-11-28 14:36:40 +03:00
|
|
|
|
2021-09-20 22:51:51 +03:00
|
|
|
return wrapper;
|
|
|
|
};
|
2020-11-28 14:36:40 +03:00
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
afterEach(() => wrapper.unmount());
|
|
|
|
|
2021-09-20 22:51:51 +03:00
|
|
|
it.each([
|
|
|
|
[ '', '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);
|
2020-12-25 12:43:36 +03:00
|
|
|
const dropdown = wrapper.find(DropdownBtn);
|
2020-11-28 14:36:40 +03:00
|
|
|
|
|
|
|
expect(input).toHaveLength(0);
|
|
|
|
expect(dropdown).toHaveLength(1);
|
2021-09-20 22:51:51 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2021-09-20 22:51:51 +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);
|
2020-12-25 12:43:36 +03:00
|
|
|
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);
|
2020-12-25 12:43:36 +03:00
|
|
|
expect(wrapper.find(DropdownBtn)).toHaveLength(1);
|
2020-11-28 14:36:40 +03:00
|
|
|
});
|
2021-09-20 22:51:51 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-03-05 15:26:28 +03:00
|
|
|
[ 0, 'default.com<span class="float-end text-muted">default</span>' ],
|
2021-09-20 22:51:51 +03:00
|
|
|
[ 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
|
|
|
});
|