import { shallow, ShallowWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
import { Button, ModalHeader } from 'reactstrap';
import { DuplicatedServersModal } from '../../../src/servers/helpers/DuplicatedServersModal';
import { ServerData } from '../../../src/servers/data';
describe('', () => {
const onDiscard = jest.fn();
const onSave = jest.fn();
let wrapper: ShallowWrapper;
const createWrapper = (duplicatedServers: ServerData[] = []) => {
wrapper = shallow(
,
);
return wrapper;
};
beforeEach(jest.clearAllMocks);
afterEach(() => wrapper?.unmount());
it.each([
[[], 0 ],
[[ Mock.all() ], 2 ],
[[ Mock.all(), Mock.all() ], 2 ],
[[ Mock.all(), Mock.all(), Mock.all() ], 3 ],
[[ Mock.all(), Mock.all(), Mock.all(), Mock.all() ], 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() ],
{
header: 'Duplicated server',
firstParagraph: 'There is already a server with:',
lastParagraph: 'Do you want to save this server anyway?',
discardBtn: 'Discard',
},
],
[
[ Mock.all(), Mock.all() ],
{
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);
});
it.each([
[[]],
[[ Mock.of({ url: 'url', apiKey: 'apiKey' }) ]],
])('displays provided server data', (duplicatedServers) => {
const wrapper = createWrapper(duplicatedServers);
const li = wrapper.find('li');
if (duplicatedServers.length === 0) {
expect(li).toHaveLength(0);
} else if (duplicatedServers.length === 1) {
expect(li.first().find('b').html()).toEqual(`${duplicatedServers[0].url}`);
expect(li.last().find('b').html()).toEqual(`${duplicatedServers[0].apiKey}`);
} else {
expect.assertions(duplicatedServers.length);
li.forEach((item, index) => {
const server = duplicatedServers[index];
expect(item.html()).toContain(`${server.url} - ${server.apiKey}`);
});
}
});
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();
});
});