summaryrefslogtreecommitdiff
path: root/spec/javascripts/ide/components/commit_sidebar/new_merge_request_option_spec.js
blob: 7017bfcd6a6afc3c00bf5f9e07ec30396f3956a5 (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
import Vue from 'vue';
import store from '~/ide/stores';
import consts from '~/ide/stores/modules/commit/constants';
import NewMergeRequestOption from '~/ide/components/commit_sidebar/new_merge_request_option.vue';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import { projectData } from 'spec/ide/mock_data';
import { resetStore } from 'spec/ide/helpers';

describe('create new MR checkbox', () => {
  let vm;
  const createComponent = ({
    hasMR = false,
    commitAction = consts.COMMIT_TO_NEW_BRANCH,
    currentBranchId = 'master',
  } = {}) => {
    const Component = Vue.extend(NewMergeRequestOption);

    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 });

    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('is hidden when an MR already exists and committing to current branch', () => {
    createComponent({
      hasMR: true,
      commitAction: consts.COMMIT_TO_CURRENT_BRANCH,
      currentBranchId: 'feature',
    });

    expect(vm.$el.textContent).toBe('');
  });

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

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

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

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

  it('dispatches toggleShouldCreateMR when clicking checkbox', () => {
    createComponent();
    const el = vm.$el.querySelector('input[type="checkbox"]');
    spyOn(vm.$store, 'dispatch');
    el.dispatchEvent(new Event('change'));

    expect(vm.$store.dispatch.calls.allArgs()).toEqual(
      jasmine.arrayContaining([['commit/toggleShouldCreateMR', jasmine.any(Object)]]),
    );
  });
});