Commit 3a6c2a41 authored by Simon Knox's avatar Simon Knox

Merge branch '290622-display-warning-mr-edit' into 'master'

Display unload warning if MR edit changed

See merge request gitlab-org/gitlab!53438
parents c0fb9238 dad1ada1
import { serializeForm } from '~/lib/utils/forms';
const findForm = () => document.querySelector('.merge-request-form');
const serializeFormData = () => JSON.stringify(serializeForm(findForm()));
export default () => {
const oldFormData = serializeFormData();
const compareFormData = (e) => {
const newFormData = serializeFormData();
if (oldFormData !== newFormData) {
e.preventDefault();
// eslint-disable-next-line no-param-reassign
e.returnValue = ''; // Chrome requires returnValue to be set
}
};
window.addEventListener('beforeunload', compareFormData);
findForm().addEventListener('submit', () =>
window.removeEventListener('beforeunload', compareFormData),
);
};
import initMergeRequest from '~/pages/projects/merge_requests/init_merge_request';
import initCheckFormState from './check_form_state';
document.addEventListener('DOMContentLoaded', initMergeRequest);
document.addEventListener('DOMContentLoaded', () => {
initMergeRequest();
initCheckFormState();
});
---
title: Display unload warning if MR edit changed
merge_request: 53438
author:
type: added
......@@ -23,6 +23,9 @@ export default {
fallbackApprovalsRequired: (state) => state.approvals.fallbackApprovalsRequired,
}),
},
mounted() {
this.$root.$emit('hidden-inputs-mounted');
},
INPUT_ID,
INPUT_SOURCE_ID,
INPUT_NAME,
......
import mountApprovals from 'ee/approvals/mount_mr_edit';
import mountBlockingMergeRequestsInput from 'ee/projects/merge_requests/blocking_mr_input';
import initCheckFormState from '~/pages/projects/merge_requests/edit/check_form_state';
export default () => {
mountApprovals(document.getElementById('js-mr-approvals-input'));
const editMrApp = mountApprovals(document.getElementById('js-mr-approvals-input'));
mountBlockingMergeRequestsInput(document.getElementById('js-blocking-merge-requests-input'));
editMrApp.$on('hidden-inputs-mounted', initCheckFormState);
};
import initCheckFormState from '~/pages/projects/merge_requests/edit/check_form_state';
describe('Check form state', () => {
const findInput = () => document.querySelector('#form-input');
let beforeUnloadEvent;
let setDialogContent;
beforeEach(() => {
setFixtures(`
<form class="merge-request-form">
<input type="text" name="test" id="form-input"/>
</form>`);
beforeUnloadEvent = new Event('beforeunload');
jest.spyOn(beforeUnloadEvent, 'preventDefault');
setDialogContent = jest.spyOn(beforeUnloadEvent, 'returnValue', 'set');
initCheckFormState();
});
afterEach(() => {
beforeUnloadEvent.preventDefault.mockRestore();
setDialogContent.mockRestore();
});
it('shows confirmation dialog when there are unsaved changes', () => {
findInput().value = 'value changed';
window.dispatchEvent(beforeUnloadEvent);
expect(beforeUnloadEvent.preventDefault).toHaveBeenCalled();
expect(setDialogContent).toHaveBeenCalledWith('');
});
it('does not show confirmation dialog when there are no unsaved changes', () => {
window.dispatchEvent(beforeUnloadEvent);
expect(beforeUnloadEvent.preventDefault).not.toHaveBeenCalled();
expect(setDialogContent).not.toHaveBeenCalled();
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment