summaryrefslogtreecommitdiff
path: root/spec/frontend/labels/components/promote_label_modal_spec.js
blob: 8953e3cbcd8ab5dbe81c7b8ca603f832e86c9c42 (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
import { shallowMount } from '@vue/test-utils';
import { GlModal, GlSprintf } from '@gitlab/ui';
import AxiosMockAdapter from 'axios-mock-adapter';

import { TEST_HOST } from 'helpers/test_constants';
import { stubComponent } from 'helpers/stub_component';

import axios from '~/lib/utils/axios_utils';
import PromoteLabelModal from '~/labels/components/promote_label_modal.vue';
import eventHub from '~/labels/event_hub';

describe('Promote label modal', () => {
  let wrapper;
  let axiosMock;

  const labelMockData = {
    labelTitle: 'Documentation',
    labelColor: 'rgb(92, 184, 92)',
    labelTextColor: 'rgb(255, 255, 255)',
    url: `${TEST_HOST}/dummy/promote/labels`,
    groupName: 'group',
  };

  const createComponent = () => {
    wrapper = shallowMount(PromoteLabelModal, {
      propsData: labelMockData,
      stubs: {
        GlSprintf,
        GlModal: stubComponent(GlModal, {
          template: `<div><slot name="modal-title"></slot><slot></slot></div>`,
        }),
      },
    });
  };

  beforeEach(() => {
    axiosMock = new AxiosMockAdapter(axios);
    createComponent();
  });

  afterEach(() => {
    axiosMock.reset();
    wrapper.destroy();
  });

  describe('Modal title and description', () => {
    it('contains the proper description', () => {
      expect(wrapper.text()).toContain(
        `Promoting ${labelMockData.labelTitle} will make it available for all projects inside ${labelMockData.groupName}`,
      );
    });

    it('contains a label span with the color', () => {
      const label = wrapper.find('.modal-title-with-label .label');

      expect(label.element.style.backgroundColor).toBe(labelMockData.labelColor);
      expect(label.element.style.color).toBe(labelMockData.labelTextColor);
      expect(label.text()).toBe(labelMockData.labelTitle);
    });
  });

  describe('When requesting a label promotion', () => {
    beforeEach(() => {
      jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
    });

    it('redirects when a label is promoted', async () => {
      const responseURL = `${TEST_HOST}/dummy/endpoint`;
      axiosMock.onPost(labelMockData.url).reply(200, { url: responseURL });

      wrapper.findComponent(GlModal).vm.$emit('primary');

      expect(eventHub.$emit).toHaveBeenCalledWith(
        'promoteLabelModal.requestStarted',
        labelMockData.url,
      );

      await axios.waitForAll();

      expect(eventHub.$emit).toHaveBeenCalledWith('promoteLabelModal.requestFinished', {
        labelUrl: labelMockData.url,
        successful: true,
      });
    });

    it('displays an error if promoting a label failed', async () => {
      const dummyError = new Error('promoting label failed');
      dummyError.response = { status: 500 };
      axiosMock.onPost(labelMockData.url).reply(500, { error: dummyError });

      wrapper.findComponent(GlModal).vm.$emit('primary');

      await axios.waitForAll();

      expect(eventHub.$emit).toHaveBeenCalledWith('promoteLabelModal.requestFinished', {
        labelUrl: labelMockData.url,
        successful: false,
      });
    });
  });
});