summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/components/commit_sidebar/editor_header_spec.js
blob: d77e8e3d04c2d5e0d3fc0765f5a1d8795e9b735e (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { mount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
import EditorHeader from '~/ide/components/commit_sidebar/editor_header.vue';
import { createStore } from '~/ide/stores';
import { file } from '../../helpers';

Vue.use(Vuex);

const TEST_FILE_PATH = 'test/file/path';

describe('IDE commit editor header', () => {
  let wrapper;
  let store;

  const createComponent = (fileProps = {}) => {
    wrapper = mount(EditorHeader, {
      store,
      propsData: {
        activeFile: {
          ...file(TEST_FILE_PATH),
          staged: true,
          ...fileProps,
        },
      },
    });
  };

  const findDiscardModal = () => wrapper.find({ ref: 'discardModal' });
  const findDiscardButton = () => wrapper.find({ ref: 'discardButton' });

  beforeEach(() => {
    store = createStore();
    jest.spyOn(store, 'dispatch').mockImplementation();
  });

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  it.each`
    fileProps                            | shouldExist
    ${{ staged: false, changed: false }} | ${false}
    ${{ staged: true, changed: false }}  | ${true}
    ${{ staged: false, changed: true }}  | ${true}
    ${{ staged: true, changed: true }}   | ${true}
  `('with $fileProps, show discard button is $shouldExist', ({ fileProps, shouldExist }) => {
    createComponent(fileProps);

    expect(findDiscardButton().exists()).toBe(shouldExist);
  });

  describe('discard button', () => {
    beforeEach(() => {
      createComponent();

      const modal = findDiscardModal();
      jest.spyOn(modal.vm, 'show');

      findDiscardButton().trigger('click');
    });

    it('opens a dialog confirming discard', () => {
      expect(findDiscardModal().vm.show).toHaveBeenCalled();
    });

    it('calls discardFileChanges if dialog result is confirmed', () => {
      expect(store.dispatch).not.toHaveBeenCalled();

      findDiscardModal().vm.$emit('primary');

      expect(store.dispatch).toHaveBeenCalledWith('discardFileChanges', TEST_FILE_PATH);
    });
  });
});