Welcome to mirror list, hosted at ThFree Co, Russian Federation.

new_merge_request_option_spec.js « commit_sidebar « components « ide « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce26519abc98de98367f2b98a7774a5f9abf838c (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
import Vue from 'vue';
import Vuex from 'vuex';
import { GlFormCheckbox } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import NewMergeRequestOption from '~/ide/components/commit_sidebar/new_merge_request_option.vue';
import { createStore } from '~/ide/stores';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';

Vue.use(Vuex);

describe('NewMergeRequestOption component', () => {
  let store;
  let wrapper;

  const findCheckbox = () => wrapper.findComponent(GlFormCheckbox);
  const findFieldset = () => wrapper.findByTestId('new-merge-request-fieldset');
  const findTooltip = () => getBinding(findFieldset().element, 'gl-tooltip');

  const createComponent = ({
    shouldHideNewMrOption = false,
    shouldDisableNewMrOption = false,
    shouldCreateMR = false,
  } = {}) => {
    store = createStore();

    wrapper = shallowMountExtended(NewMergeRequestOption, {
      store: {
        ...store,
        getters: {
          'commit/shouldHideNewMrOption': shouldHideNewMrOption,
          'commit/shouldDisableNewMrOption': shouldDisableNewMrOption,
          'commit/shouldCreateMR': shouldCreateMR,
        },
      },
      directives: {
        GlTooltip: createMockDirective('gl-tooltip'),
      },
    });
  };

  describe('when the `shouldHideNewMrOption` getter returns false', () => {
    beforeEach(() => {
      createComponent();
      jest.spyOn(store, 'dispatch').mockImplementation();
    });

    it('renders an enabled new MR checkbox', () => {
      expect(findCheckbox().attributes('disabled')).toBeUndefined();
    });

    it("doesn't add `is-disabled` class to the fieldset", () => {
      expect(findFieldset().classes()).not.toContain('is-disabled');
    });

    it('dispatches toggleShouldCreateMR when clicking checkbox', () => {
      findCheckbox().vm.$emit('change');

      expect(store.dispatch).toHaveBeenCalledWith('commit/toggleShouldCreateMR', undefined);
    });

    describe('when user cannot create an MR', () => {
      beforeEach(() => {
        createComponent({
          shouldDisableNewMrOption: true,
        });
      });

      it('disables the new MR checkbox', () => {
        expect(findCheckbox().attributes('disabled')).toBeDefined();
      });

      it('adds `is-disabled` class to the fieldset', () => {
        expect(findFieldset().classes()).toContain('is-disabled');
      });

      it('shows a tooltip', () => {
        expect(findTooltip().value).toBe(wrapper.vm.$options.i18n.tooltipText);
      });
    });
  });

  describe('when the `shouldHideNewMrOption` getter returns true', () => {
    beforeEach(() => {
      createComponent({
        shouldHideNewMrOption: true,
      });
    });

    it("doesn't render the new MR checkbox", () => {
      expect(findCheckbox().exists()).toBe(false);
    });
  });
});