All files / app/assets/javascripts/pages/projects/merge_requests/edit check_form_state.js

92.3% Statements 12/13
100% Branches 2/2
80% Functions 4/5
90.9% Lines 10/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25    7x 5x     2x   2x 3x   3x 1x   1x       2x   2x        
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),
  );
};