2020-08-29 10:19:15 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2022-03-26 14:46:32 +03:00
|
|
|
import { Button, Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
2020-08-29 10:19:15 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2018-12-18 00:32:51 +03:00
|
|
|
import DeleteServerModal from '../../src/servers/DeleteServerModal';
|
2020-08-29 10:19:15 +03:00
|
|
|
import { ServerWithId } from '../../src/servers/data';
|
2018-08-24 13:36:14 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() }));
|
|
|
|
|
2018-08-24 13:36:14 +03:00
|
|
|
describe('<DeleteServerModal />', () => {
|
2020-08-29 10:19:15 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2019-04-19 11:29:49 +03:00
|
|
|
const deleteServerMock = jest.fn();
|
2022-02-08 00:17:57 +03:00
|
|
|
const navigate = jest.fn();
|
2019-04-19 11:29:49 +03:00
|
|
|
const toggleMock = jest.fn();
|
2018-08-24 13:36:14 +03:00
|
|
|
const serverName = 'the_server_name';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useNavigate as any).mockReturnValue(navigate);
|
|
|
|
|
2018-08-24 13:36:14 +03:00
|
|
|
wrapper = shallow(
|
2018-12-18 00:32:51 +03:00
|
|
|
<DeleteServerModal
|
2020-08-29 10:19:15 +03:00
|
|
|
server={Mock.of<ServerWithId>({ name: serverName })}
|
2018-08-24 13:36:14 +03:00
|
|
|
toggle={toggleMock}
|
2022-03-26 14:17:42 +03:00
|
|
|
isOpen
|
2018-08-24 13:36:14 +03:00
|
|
|
deleteServer={deleteServerMock}
|
2020-08-22 09:10:31 +03:00
|
|
|
/>,
|
2018-08-24 13:36:14 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
afterEach(() => wrapper.unmount());
|
2020-08-29 10:19:15 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2018-08-24 13:36:14 +03:00
|
|
|
|
|
|
|
it('renders a modal window', () => {
|
|
|
|
expect(wrapper.find(Modal)).toHaveLength(1);
|
|
|
|
expect(wrapper.find(ModalHeader)).toHaveLength(1);
|
|
|
|
expect(wrapper.find(ModalBody)).toHaveLength(1);
|
|
|
|
expect(wrapper.find(ModalFooter)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the name of the server as part of the content', () => {
|
|
|
|
const modalBody = wrapper.find(ModalBody);
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-08-24 13:36:14 +03:00
|
|
|
expect(modalBody.find('p').first().text()).toEqual(
|
2020-08-22 09:10:31 +03:00
|
|
|
`Are you sure you want to remove ${serverName}?`,
|
2018-08-24 13:36:14 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggles when clicking cancel button', () => {
|
2022-03-26 14:46:32 +03:00
|
|
|
const cancelBtn = wrapper.find(Button).first();
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-08-24 13:36:14 +03:00
|
|
|
cancelBtn.simulate('click');
|
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(toggleMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(deleteServerMock).not.toHaveBeenCalled();
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).not.toHaveBeenCalled();
|
2018-08-24 13:36:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('deletes server when clicking accept button', () => {
|
2022-03-26 14:46:32 +03:00
|
|
|
const acceptBtn = wrapper.find(Button).last();
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-08-24 13:36:14 +03:00
|
|
|
acceptBtn.simulate('click');
|
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(toggleMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(deleteServerMock).toHaveBeenCalledTimes(1);
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledTimes(1);
|
2018-08-24 13:36:14 +03:00
|
|
|
});
|
|
|
|
});
|