summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/components/commit_sidebar/editor_header_spec.js
blob: 054e7492429f5150cdbde2ed5c480264448aaf3d (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
77
78
79
80
81
82
import Vuex from 'vuex';
import { mount, createLocalVue } from '@vue/test-utils';
import { createStore } from '~/ide/stores';
import EditorHeader from '~/ide/components/commit_sidebar/editor_header.vue';
import { file } from '../../helpers';

const localVue = createLocalVue();
localVue.use(Vuex);

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

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

  beforeEach(() => {
    f = file('file');
    store = createStore();

    wrapper = mount(EditorHeader, {
      store,
      localVue,
      propsData: {
        activeFile: f,
      },
    });

    jest.spyOn(wrapper.vm, 'stageChange').mockImplementation();
    jest.spyOn(wrapper.vm, 'unstageChange').mockImplementation();
    jest.spyOn(wrapper.vm, 'discardFileChanges').mockImplementation();
  });

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

  it('renders button to discard & stage', () => {
    expect(wrapper.vm.$el.querySelectorAll('.btn').length).toBe(2);
  });

  describe('discard button', () => {
    let modal;

    beforeEach(() => {
      modal = findDiscardModal();

      jest.spyOn(modal.vm, 'show');

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

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

    it('calls discardFileChanges if dialog result is confirmed', () => {
      modal.vm.$emit('ok');

      expect(wrapper.vm.discardFileChanges).toHaveBeenCalledWith(f.path);
    });
  });

  describe('stage/unstage button', () => {
    it('unstages the file if it was already staged', () => {
      f.staged = true;

      findActionButton().trigger('click');

      expect(wrapper.vm.unstageChange).toHaveBeenCalledWith(f.path);
    });

    it('stages the file if it was not staged', () => {
      findActionButton().trigger('click');

      expect(wrapper.vm.stageChange).toHaveBeenCalledWith(f.path);
    });
  });
});