summaryrefslogtreecommitdiff
path: root/spec/frontend/pipeline_editor/components/ui/confirm_unsaved_changes_dialog_spec.js
blob: 44fda2812d86765d945e59b0ca905ed249690c3f (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { shallowMount } from '@vue/test-utils';
import ConfirmDialog from '~/pipeline_editor/components/ui/confirm_unsaved_changes_dialog.vue';

describe('pipeline_editor/components/ui/confirm_unsaved_changes_dialog', () => {
  let beforeUnloadEvent;
  let setDialogContent;
  let wrapper;

  const createComponent = (propsData = {}) => {
    wrapper = shallowMount(ConfirmDialog, {
      propsData,
    });
  };

  beforeEach(() => {
    beforeUnloadEvent = new Event('beforeunload');
    jest.spyOn(beforeUnloadEvent, 'preventDefault');
    setDialogContent = jest.spyOn(beforeUnloadEvent, 'returnValue', 'set');
  });

  afterEach(() => {
    beforeUnloadEvent.preventDefault.mockRestore();
    setDialogContent.mockRestore();
    wrapper.destroy();
  });

  it('shows confirmation dialog when there are unsaved changes', () => {
    createComponent({ hasUnsavedChanges: true });
    window.dispatchEvent(beforeUnloadEvent);

    expect(beforeUnloadEvent.preventDefault).toHaveBeenCalled();
    expect(setDialogContent).toHaveBeenCalledWith('');
  });

  it('does not show confirmation dialog when there are no unsaved changes', () => {
    createComponent({ hasUnsavedChanges: false });
    window.dispatchEvent(beforeUnloadEvent);

    expect(beforeUnloadEvent.preventDefault).not.toHaveBeenCalled();
    expect(setDialogContent).not.toHaveBeenCalled();
  });
});