summaryrefslogtreecommitdiff
path: root/spec/javascripts/ide/components/commit_sidebar/actions_spec.js
blob: 23e6a055518ad6d6a5bb8a95d356e12544468e4f (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import Vue from 'vue';
import store from '~/ide/stores';
import consts from '~/ide/stores/modules/commit/constants';
import commitActions from '~/ide/components/commit_sidebar/actions.vue';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import { resetStore } from 'spec/ide/helpers';
import { projectData } from 'spec/ide/mock_data';

describe('IDE commit sidebar actions', () => {
  let vm;
  const createComponent = ({
    hasMR = false,
    commitAction = consts.COMMIT_TO_NEW_BRANCH,
    mergeRequestsEnabled = true,
    currentBranchId = 'master',
    shouldCreateMR = false,
  } = {}) => {
    const Component = Vue.extend(commitActions);

    vm = createComponentWithStore(Component, store);

    vm.$store.state.currentBranchId = currentBranchId;
    vm.$store.state.currentProjectId = 'abcproject';
    vm.$store.state.commit.commitAction = commitAction;
    Vue.set(vm.$store.state.projects, 'abcproject', { ...projectData });
    vm.$store.state.projects.abcproject.merge_requests_enabled = mergeRequestsEnabled;
    vm.$store.state.commit.shouldCreateMR = shouldCreateMR;

    if (hasMR) {
      vm.$store.state.currentMergeRequestId = '1';
      vm.$store.state.projects[store.state.currentProjectId].mergeRequests[
        store.state.currentMergeRequestId
      ] = { foo: 'bar' };
    }

    return vm.$mount();
  };

  afterEach(() => {
    vm.$destroy();

    resetStore(vm.$store);
  });

  it('renders 2 groups', () => {
    createComponent();

    expect(vm.$el.querySelectorAll('input[type="radio"]').length).toBe(2);
  });

  it('renders current branch text', () => {
    createComponent();

    expect(vm.$el.textContent).toContain('Commit to master branch');
  });

  it('hides merge request option when project merge requests are disabled', done => {
    createComponent({ mergeRequestsEnabled: false });

    vm.$nextTick(() => {
      expect(vm.$el.querySelectorAll('input[type="radio"]').length).toBe(2);
      expect(vm.$el.textContent).not.toContain('Create a new branch and merge request');

      done();
    });
  });

  describe('commitToCurrentBranchText', () => {
    it('escapes current branch', () => {
      const injectedSrc = '<img src="x" />';
      createComponent({ currentBranchId: injectedSrc });

      expect(vm.commitToCurrentBranchText).not.toContain(injectedSrc);
    });
  });

  describe('create new MR checkbox', () => {
    it('disables `createMR` button when an MR already exists and committing to current branch', () => {
      createComponent({ hasMR: true, commitAction: consts.COMMIT_TO_CURRENT_BRANCH });

      expect(vm.$el.querySelector('input[type="checkbox"]').disabled).toBe(true);
    });

    it('does not disable checkbox if MR does not exist', () => {
      createComponent({ hasMR: false });

      expect(vm.$el.querySelector('input[type="checkbox"]').disabled).toBe(false);
    });

    it('does not disable checkbox when creating a new branch', () => {
      createComponent({ commitAction: consts.COMMIT_TO_NEW_BRANCH });

      expect(vm.$el.querySelector('input[type="checkbox"]').disabled).toBe(false);
    });

    it('toggles off new MR when switching back to commit to current branch and MR exists', () => {
      createComponent({
        commitAction: consts.COMMIT_TO_NEW_BRANCH,
        shouldCreateMR: true,
      });
      const currentBranchRadio = vm.$el.querySelector(
        `input[value="${consts.COMMIT_TO_CURRENT_BRANCH}"`,
      );
      currentBranchRadio.click();

      vm.$nextTick(() => {
        expect(vm.$store.state.commit.shouldCreateMR).toBe(false);
      });
    });

    it('toggles `shouldCreateMR` when clicking checkbox', () => {
      createComponent();
      const el = vm.$el.querySelector('input[type="checkbox"]');
      el.dispatchEvent(new Event('change'));

      expect(vm.$store.state.commit.shouldCreateMR).toBe(true);
    });
  });
});