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

107 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-12-30 23:40:08 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
2022-01-01 14:35:06 +03:00
import { Button, ModalHeader } 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());
2022-01-01 14:35:06 +03:00
it.each([
[[], 0 ],
[[ Mock.all<ServerData>() ], 2 ],
[[ Mock.all<ServerData>(), Mock.all<ServerData>() ], 2 ],
[[ Mock.all<ServerData>(), Mock.all<ServerData>(), Mock.all<ServerData>() ], 3 ],
[[ Mock.all<ServerData>(), Mock.all<ServerData>(), Mock.all<ServerData>(), Mock.all<ServerData>() ], 4 ],
])('renders expected amount of items', (duplicatedServers, expectedItems) => {
const wrapper = createWrapper(duplicatedServers);
const li = wrapper.find('li');
expect(li).toHaveLength(expectedItems);
});
it.each([
[
[ Mock.all<ServerData>() ],
{
header: 'Duplicated server',
firstParagraph: 'There is already a server with:',
lastParagraph: 'Do you want to save this server anyway?',
discardBtn: 'Discard',
},
],
[
[ Mock.all<ServerData>(), Mock.all<ServerData>() ],
{
header: 'Duplicated servers',
firstParagraph: 'The next servers already exist:',
lastParagraph: 'Do you want to ignore duplicated servers?',
discardBtn: 'Ignore duplicated',
},
],
])('renders expected texts based on amount of servers', (duplicatedServers, assertions) => {
const wrapper = createWrapper(duplicatedServers);
const header = wrapper.find(ModalHeader);
const p = wrapper.find('p');
const span = wrapper.find('span');
const discardBtn = wrapper.find(Button).first();
expect(header.html()).toContain(assertions.header);
expect(p.html()).toContain(assertions.firstParagraph);
expect(span.html()).toContain(assertions.lastParagraph);
expect(discardBtn.html()).toContain(assertions.discardBtn);
});
2021-12-30 23:40:08 +03:00
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);
2022-01-01 14:35:06 +03:00
} else if (duplicatedServers.length === 1) {
expect(li.first().find('b').html()).toEqual(`<b>${duplicatedServers[0].url}</b>`);
expect(li.last().find('b').html()).toEqual(`<b>${duplicatedServers[0].apiKey}</b>`);
2022-01-01 14:35:06 +03:00
} else {
expect.assertions(duplicatedServers.length);
li.forEach((item, index) => {
const server = duplicatedServers[index];
expect(item.html()).toContain(`<b>${server.url}</b> - <b>${server.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();
});
});