Created DeleteTagConfirmModal test

This commit is contained in:
Alejandro Celaya 2019-01-13 23:26:06 +01:00
parent 056286636d
commit 11d49fb70f
3 changed files with 92 additions and 6 deletions

View file

@ -13,13 +13,12 @@ export default class DeleteTagConfirmModal extends React.Component {
tagDeleted: PropTypes.func,
};
doDelete = () => {
doDelete = async () => {
const { tag, toggle, deleteTag } = this.props;
return deleteTag(tag).then(() => {
this.tagWasDeleted = true;
toggle();
});
await deleteTag(tag);
this.tagWasDeleted = true;
toggle();
};
handleOnClosed = () => {
if (!this.tagWasDeleted) {

View file

@ -15,7 +15,7 @@ describe('<CreateShortUrl />', () => {
const createShortUrl = sinon.spy();
beforeEach(() => {
const CreateShortUrl = createShortUrlsCreator(TagsSelector);
const CreateShortUrl = createShortUrlsCreator(TagsSelector, () => '');
wrapper = shallow(
<CreateShortUrl shortUrlCreationResult={shortUrlCreationResult} createShortUrl={createShortUrl} />

View file

@ -0,0 +1,87 @@
import React from 'react';
import { shallow } from 'enzyme';
import * as sinon from 'sinon';
import { Modal, ModalBody, ModalFooter } from 'reactstrap';
import DeleteTagConfirmModal from '../../../src/tags/helpers/DeleteTagConfirmModal';
describe('<DeleteTagConfirmModal />', () => {
let wrapper;
const tag = 'nodejs';
const deleteTag = sinon.spy();
const tagDeleted = sinon.spy();
const createWrapper = (tagDelete) => {
wrapper = shallow(
<DeleteTagConfirmModal
tag={tag}
toggle={() => ''}
isOpen
deleteTag={deleteTag}
tagDeleted={tagDeleted}
tagDelete={tagDelete}
/>
);
return wrapper;
};
afterEach(() => {
wrapper && wrapper.unmount();
deleteTag.resetHistory();
tagDeleted.resetHistory();
});
it('asks confirmation for provided tag to be deleted', () => {
wrapper = createWrapper({ error: false, deleting: false });
const body = wrapper.find(ModalBody);
const footer = wrapper.find(ModalFooter);
const delBtn = footer.find('.btn-danger');
expect(body.html()).toContain(`Are you sure you want to delete tag <b>${tag}</b>?`);
expect(delBtn.prop('disabled')).toEqual(false);
expect(delBtn.text()).toEqual('Delete tag');
});
it('shows error message when deletion failed', () => {
wrapper = createWrapper({ error: true, deleting: false });
const body = wrapper.find(ModalBody);
expect(body.html()).toContain('Something went wrong while deleting the tag :(');
});
it('shows loading status while deleting', () => {
wrapper = createWrapper({ error: false, deleting: true });
const footer = wrapper.find(ModalFooter);
const delBtn = footer.find('.btn-danger');
expect(delBtn.prop('disabled')).toEqual(true);
expect(delBtn.text()).toEqual('Deleting tag...');
});
it('deletes tag modal when btn is clicked', () => {
wrapper = createWrapper({ error: false, deleting: true });
const footer = wrapper.find(ModalFooter);
const delBtn = footer.find('.btn-danger');
delBtn.simulate('click');
expect(deleteTag.calledOnce).toEqual(true);
expect(deleteTag.calledWith(tag)).toEqual(true);
});
it('does no further actions when modal is closed without deleting tag', () => {
wrapper = createWrapper({ error: false, deleting: false });
const modal = wrapper.find(Modal);
modal.simulate('closed');
expect(tagDeleted.called).toEqual(false);
});
it('notifies tag to be deleted when modal is closed after deleting tag', () => {
wrapper = createWrapper({ error: false, deleting: false });
const modal = wrapper.find(Modal);
wrapper.instance().tagWasDeleted = true;
modal.simulate('closed');
expect(tagDeleted.calledOnce).toEqual(true);
expect(tagDeleted.calledWith(tag)).toEqual(true);
});
});