debian-mirror-gitlab/spec/frontend/confirm_modal_spec.js

109 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2020-10-24 23:57:45 +05:30
import initConfirmModal from '~/confirm_modal';
2020-04-08 14:13:33 +05:30
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');
2021-03-08 18:12:59 +05:30
buttons.forEach((x) => {
2020-04-08 14:13:33 +05:30
const button = document.createElement('button');
button.setAttribute('class', 'js-confirm-modal-button');
button.setAttribute('data-path', x.path);
button.setAttribute('data-method', x.method);
button.setAttribute('data-modal-attributes', JSON.stringify(x.modalAttributes));
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-23 00:09:42 +05:30
const modalIsHidden = () => findModal() === null;
2020-04-08 14:13:33 +05:30
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]
2021-03-08 18:12:59 +05:30
.find((x) => x.match('btn-'))
2020-04-08 14:13:33 +05:30
.replace('btn-', ''),
okTitle: findModalOkButton(modal, modalAttributes.okVariant).innerHTML,
},
};
};
it('starts with only JsHooks', () => {
expect(findJsHooks()).toHaveLength(buttons.length);
2021-12-11 22:18:48 +05:30
expect(findModal()).toBe(null);
2020-04-08 14:13:33 +05:30
});
describe('when button clicked', () => {
beforeEach(() => {
initConfirmModal();
2021-03-08 18:12:59 +05:30
findJsHooks().item(0).click();
2020-04-08 14:13:33 +05:30
});
it('does not replace JsHook with GlModal', () => {
expect(findJsHooks()).toHaveLength(buttons.length);
});
describe('GlModal', () => {
it('is rendered', () => {
2021-12-11 22:18:48 +05:30
expect(findModal()).not.toBe(null);
2020-04-08 14:13:33 +05:30
expect(modalIsHidden()).toBe(false);
});
});
});
describe.each`
index
${0}
${1}
`(`when multiple buttons exist`, ({ index }) => {
beforeEach(() => {
initConfirmModal();
2021-03-08 18:12:59 +05:30
findJsHooks().item(index).click();
2020-04-08 14:13:33 +05:30
});
it('correct props are passed to gl-modal', () => {
expect(serializeModal(findModal(), index)).toEqual(buttons[index]);
});
});
});