2020-02-25 07:08:48 -05:00
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2020-08-17 17:09:56 -04:00
|
|
|
import initConfirmModal from '~/confirm_modal';
|
2020-02-25 07:08:48 -05:00
|
|
|
|
|
|
|
describe('ConfirmModal', () => {
|
|
|
|
const buttons = [
|
|
|
|
{
|
|
|
|
path: `${TEST_HOST}/1`,
|
|
|
|
method: 'delete',
|
|
|
|
modalAttributes: {
|
|
|
|
title: 'Remove tracking database entry',
|
|
|
|
message: 'Tracking database entry will be removed. Are you sure?',
|
|
|
|
okVariant: 'danger',
|
|
|
|
okTitle: 'Remove entry',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: `${TEST_HOST}/1`,
|
|
|
|
method: 'post',
|
|
|
|
modalAttributes: {
|
|
|
|
title: 'Update tracking database entry',
|
|
|
|
message: 'Tracking database entry will be updated. Are you sure?',
|
|
|
|
okVariant: 'success',
|
|
|
|
okTitle: 'Update entry',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
const buttonContainer = document.createElement('div');
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
buttons.forEach((x) => {
|
2020-02-25 07:08:48 -05:00
|
|
|
const button = document.createElement('button');
|
|
|
|
button.setAttribute('class', 'js-confirm-modal-button');
|
2022-06-17 11:08:29 -04:00
|
|
|
button.dataset.path = x.path;
|
|
|
|
button.dataset.method = x.method;
|
|
|
|
button.dataset.modalAttributes = JSON.stringify(x.modalAttributes);
|
2020-02-25 07:08:48 -05:00
|
|
|
button.innerHTML = 'Action';
|
|
|
|
buttonContainer.appendChild(button);
|
|
|
|
});
|
|
|
|
|
|
|
|
document.body.appendChild(buttonContainer);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
document.body.innerHTML = '';
|
|
|
|
});
|
|
|
|
|
|
|
|
const findJsHooks = () => document.querySelectorAll('.js-confirm-modal-button');
|
|
|
|
const findModal = () => document.querySelector('.gl-modal');
|
|
|
|
const findModalOkButton = (modal, variant) =>
|
|
|
|
modal.querySelector(`.modal-footer .btn-${variant}`);
|
2020-06-04 20:08:38 -04:00
|
|
|
const modalIsHidden = () => findModal() === null;
|
2020-02-25 07:08:48 -05:00
|
|
|
|
|
|
|
const serializeModal = (modal, buttonIndex) => {
|
|
|
|
const { modalAttributes } = buttons[buttonIndex];
|
|
|
|
|
|
|
|
return {
|
|
|
|
path: modal.querySelector('form').action,
|
|
|
|
method: modal.querySelector('input[name="_method"]').value,
|
|
|
|
modalAttributes: {
|
|
|
|
title: modal.querySelector('.modal-title').innerHTML,
|
|
|
|
message: modal.querySelector('.modal-body div').innerHTML,
|
|
|
|
okVariant: [...findModalOkButton(modal, modalAttributes.okVariant).classList]
|
2020-12-23 16:10:24 -05:00
|
|
|
.find((x) => x.match('btn-'))
|
2020-02-25 07:08:48 -05:00
|
|
|
.replace('btn-', ''),
|
|
|
|
okTitle: findModalOkButton(modal, modalAttributes.okVariant).innerHTML,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
it('starts with only JsHooks', () => {
|
|
|
|
expect(findJsHooks()).toHaveLength(buttons.length);
|
2021-11-10 01:10:31 -05:00
|
|
|
expect(findModal()).toBe(null);
|
2020-02-25 07:08:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when button clicked', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
initConfirmModal();
|
2020-12-23 07:10:26 -05:00
|
|
|
findJsHooks().item(0).click();
|
2020-02-25 07:08:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not replace JsHook with GlModal', () => {
|
|
|
|
expect(findJsHooks()).toHaveLength(buttons.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GlModal', () => {
|
|
|
|
it('is rendered', () => {
|
2021-11-10 01:10:31 -05:00
|
|
|
expect(findModal()).not.toBe(null);
|
2020-03-10 17:09:21 -04:00
|
|
|
expect(modalIsHidden()).toBe(false);
|
2020-02-25 07:08:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each`
|
|
|
|
index
|
|
|
|
${0}
|
|
|
|
${1}
|
|
|
|
`(`when multiple buttons exist`, ({ index }) => {
|
|
|
|
beforeEach(() => {
|
|
|
|
initConfirmModal();
|
2020-12-23 07:10:26 -05:00
|
|
|
findJsHooks().item(index).click();
|
2020-02-25 07:08:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('correct props are passed to gl-modal', () => {
|
|
|
|
expect(serializeModal(findModal(), index)).toEqual(buttons[index]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|