shlink-web-client/test/servers/helpers/DuplicatedServersModal.test.tsx

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-12-30 23:40:08 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
import { Button } from 'reactstrap';
import { DuplicatedServersModal } from '../../../src/servers/helpers/DuplicatedServersModal';
2021-12-30 23:40:08 +03:00
import { ServerData } from '../../../src/servers/data';
describe('<DuplicatedServersModal />', () => {
2021-12-30 23:40:08 +03:00
const onDiscard = jest.fn();
const onSave = jest.fn();
let wrapper: ShallowWrapper;
const createWrapper = (duplicatedServers: ServerData[] = []) => {
2021-12-30 23:40:08 +03:00
wrapper = shallow(
<DuplicatedServersModal isOpen duplicatedServers={duplicatedServers} onDiscard={onDiscard} onSave={onSave} />,
2021-12-30 23:40:08 +03:00
);
return wrapper;
};
beforeEach(jest.clearAllMocks);
afterEach(() => wrapper?.unmount());
it.each([
[[]],
[[ Mock.of<ServerData>({ url: 'url', apiKey: 'apiKey' }) ]],
])('displays provided server data', (duplicatedServers) => {
const wrapper = createWrapper(duplicatedServers);
2021-12-30 23:40:08 +03:00
const li = wrapper.find('li');
if (duplicatedServers.length === 0) {
expect(li).toHaveLength(0);
} else {
expect(li.first().find('b').html()).toEqual(`<b>${duplicatedServers[0].url}</b>`);
expect(li.last().find('b').html()).toEqual(`<b>${duplicatedServers[0].apiKey}</b>`);
}
2021-12-30 23:40:08 +03:00
});
it('invokes onDiscard when appropriate button is clicked', () => {
const wrapper = createWrapper();
const btn = wrapper.find(Button).first();
btn.simulate('click');
expect(onDiscard).toHaveBeenCalled();
});
it('invokes onSave when appropriate button is clicked', () => {
const wrapper = createWrapper();
const btn = wrapper.find(Button).last();
btn.simulate('click');
expect(onSave).toHaveBeenCalled();
});
});