summaryrefslogtreecommitdiff
path: root/spec/frontend/jobs/components/job/stages_dropdown_spec.js
blob: 61dec585e8264c5083d604e15aff6dbf8c3aaca1 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { GlDropdown, GlDropdownItem, GlLink, GlSprintf } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Mousetrap from 'mousetrap';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import StagesDropdown from '~/jobs/components/job/sidebar/stages_dropdown.vue';
import CiIcon from '~/vue_shared/components/ci_icon.vue';
import * as copyToClipboard from '~/behaviors/copy_to_clipboard';
import {
  mockPipelineWithoutRef,
  mockPipelineWithoutMR,
  mockPipelineWithAttachedMR,
  mockPipelineDetached,
} from '../../mock_data';

describe('Stages Dropdown', () => {
  let wrapper;

  const findStatus = () => wrapper.findComponent(CiIcon);
  const findSelectedStageText = () => wrapper.findComponent(GlDropdown).props('text');
  const findStageItem = (index) => wrapper.findAllComponents(GlDropdownItem).at(index);

  const findPipelineInfoText = () => wrapper.findByTestId('pipeline-info').text();

  const createComponent = (props) => {
    wrapper = extendedWrapper(
      shallowMount(StagesDropdown, {
        propsData: {
          stages: [],
          selectedStage: 'deploy',
          ...props,
        },
        stubs: {
          GlSprintf,
          GlLink,
        },
      }),
    );
  };

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

  describe('without a merge request pipeline', () => {
    beforeEach(() => {
      createComponent({
        pipeline: mockPipelineWithoutMR,
        stages: [{ name: 'build' }, { name: 'test' }],
      });
    });

    it('renders pipeline status', () => {
      expect(findStatus().exists()).toBe(true);
    });

    it('renders dropdown with stages', () => {
      expect(findStageItem(0).text()).toBe('build');
    });

    it('rendes selected stage', () => {
      expect(findSelectedStageText()).toBe('deploy');
    });
  });

  describe('pipelineInfo', () => {
    const allElements = [
      'pipeline-path',
      'mr-link',
      'source-ref-link',
      'copy-source-ref-link',
      'source-branch-link',
      'copy-source-branch-link',
      'target-branch-link',
      'copy-target-branch-link',
    ];
    describe.each([
      [
        'does not have a ref',
        {
          pipeline: mockPipelineWithoutRef,
          text: `Pipeline #${mockPipelineWithoutRef.id}`,
          foundElements: [
            { testId: 'pipeline-path', props: [{ href: mockPipelineWithoutRef.path }] },
          ],
        },
      ],
      [
        'hasRef but not triggered by MR',
        {
          pipeline: mockPipelineWithoutMR,
          text: `Pipeline #${mockPipelineWithoutMR.id} for ${mockPipelineWithoutMR.ref.name}`,
          foundElements: [
            { testId: 'pipeline-path', props: [{ href: mockPipelineWithoutMR.path }] },
            { testId: 'source-ref-link', props: [{ href: mockPipelineWithoutMR.ref.path }] },
            { testId: 'copy-source-ref-link', props: [{ text: mockPipelineWithoutMR.ref.name }] },
          ],
        },
      ],
      [
        'hasRef and MR but not MR pipeline',
        {
          pipeline: mockPipelineDetached,
          text: `Pipeline #${mockPipelineDetached.id} for !${mockPipelineDetached.merge_request.iid} with ${mockPipelineDetached.merge_request.source_branch}`,
          foundElements: [
            { testId: 'pipeline-path', props: [{ href: mockPipelineDetached.path }] },
            { testId: 'mr-link', props: [{ href: mockPipelineDetached.merge_request.path }] },
            {
              testId: 'source-branch-link',
              props: [{ href: mockPipelineDetached.merge_request.source_branch_path }],
            },
            {
              testId: 'copy-source-branch-link',
              props: [{ text: mockPipelineDetached.merge_request.source_branch }],
            },
          ],
        },
      ],
      [
        'hasRef and MR and MR pipeline',
        {
          pipeline: mockPipelineWithAttachedMR,
          text: `Pipeline #${mockPipelineWithAttachedMR.id} for !${mockPipelineWithAttachedMR.merge_request.iid} with ${mockPipelineWithAttachedMR.merge_request.source_branch} into ${mockPipelineWithAttachedMR.merge_request.target_branch}`,
          foundElements: [
            { testId: 'pipeline-path', props: [{ href: mockPipelineWithAttachedMR.path }] },
            { testId: 'mr-link', props: [{ href: mockPipelineWithAttachedMR.merge_request.path }] },
            {
              testId: 'source-branch-link',
              props: [{ href: mockPipelineWithAttachedMR.merge_request.source_branch_path }],
            },
            {
              testId: 'copy-source-branch-link',
              props: [{ text: mockPipelineWithAttachedMR.merge_request.source_branch }],
            },
            {
              testId: 'target-branch-link',
              props: [{ href: mockPipelineWithAttachedMR.merge_request.target_branch_path }],
            },
            {
              testId: 'copy-target-branch-link',
              props: [{ text: mockPipelineWithAttachedMR.merge_request.target_branch }],
            },
          ],
        },
      ],
    ])('%s', (_, { pipeline, text, foundElements }) => {
      beforeEach(() => {
        createComponent({
          pipeline,
        });
      });

      it('should render the text', () => {
        expect(findPipelineInfoText()).toMatchInterpolatedText(text);
      });

      it('should find components with props', () => {
        foundElements.forEach((element) => {
          element.props.forEach((prop) => {
            const key = Object.keys(prop)[0];
            expect(wrapper.findByTestId(element.testId).attributes(key)).toBe(prop[key]);
          });
        });
      });

      it('should not find components', () => {
        const foundTestIds = foundElements.map((element) => element.testId);
        allElements
          .filter((testId) => !foundTestIds.includes(testId))
          .forEach((testId) => {
            expect(wrapper.findByTestId(testId).exists()).toBe(false);
          });
      });
    });
  });

  describe('mousetrap', () => {
    it.each([
      ['copy-source-ref-link', mockPipelineWithoutMR],
      ['copy-source-branch-link', mockPipelineWithAttachedMR],
    ])(
      'calls clickCopyToClipboardButton with `%s` button when `b` is pressed',
      (button, pipeline) => {
        const copyToClipboardMock = jest.spyOn(copyToClipboard, 'clickCopyToClipboardButton');
        createComponent({ pipeline });

        Mousetrap.trigger('b');

        expect(copyToClipboardMock).toHaveBeenCalledWith(wrapper.findByTestId(button).element);
      },
    );
  });
});