summaryrefslogtreecommitdiff
path: root/spec/frontend/jobs/components/stages_dropdown_spec.js
blob: b75d1707a8d490584d0d5395effddf63577d5d3c (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
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { trimText } from 'helpers/text_helper';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import StagesDropdown from '~/jobs/components/stages_dropdown.vue';
import CiIcon from '~/vue_shared/components/ci_icon.vue';
import {
  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 findPipelinePath = () => wrapper.findByTestId('pipeline-path').attributes('href');
  const findMRLinkPath = () => wrapper.findByTestId('mr-link').attributes('href');
  const findSourceBranchLinkPath = () =>
    wrapper.findByTestId('source-branch-link').attributes('href');
  const findTargetBranchLinkPath = () =>
    wrapper.findByTestId('target-branch-link').attributes('href');

  const createComponent = (props) => {
    wrapper = extendedWrapper(
      shallowMount(StagesDropdown, {
        propsData: {
          ...props,
        },
      }),
    );
  };

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

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

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

    it('renders pipeline link', () => {
      expect(findPipelinePath()).toBe('pipeline/28029444');
    });

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

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

    it(`renders the pipeline info text like "Pipeline #123 for source_branch"`, () => {
      const expected = `Pipeline #${mockPipelineWithoutMR.id} for ${mockPipelineWithoutMR.ref.name}`;
      const actual = trimText(findPipelineInfoText());

      expect(actual).toBe(expected);
    });
  });

  describe('with an "attached" merge request pipeline', () => {
    beforeEach(() => {
      createComponent({
        pipeline: mockPipelineWithAttachedMR,
        stages: [],
        selectedStage: 'deploy',
      });
    });

    it(`renders the pipeline info text like "Pipeline #123 for !456 with source_branch into target_branch"`, () => {
      const expected = `Pipeline #${mockPipelineWithAttachedMR.id} for !${mockPipelineWithAttachedMR.merge_request.iid} with ${mockPipelineWithAttachedMR.merge_request.source_branch} into ${mockPipelineWithAttachedMR.merge_request.target_branch}`;
      const actual = trimText(findPipelineInfoText());

      expect(actual).toBe(expected);
    });

    it(`renders the correct merge request link`, () => {
      expect(findMRLinkPath()).toBe(mockPipelineWithAttachedMR.merge_request.path);
    });

    it(`renders the correct source branch link`, () => {
      expect(findSourceBranchLinkPath()).toBe(
        mockPipelineWithAttachedMR.merge_request.source_branch_path,
      );
    });

    it(`renders the correct target branch link`, () => {
      expect(findTargetBranchLinkPath()).toBe(
        mockPipelineWithAttachedMR.merge_request.target_branch_path,
      );
    });
  });

  describe('with a detached merge request pipeline', () => {
    beforeEach(() => {
      createComponent({
        pipeline: mockPipelineDetached,
        stages: [],
        selectedStage: 'deploy',
      });
    });

    it(`renders the pipeline info like "Pipeline #123 for !456 with source_branch"`, () => {
      const expected = `Pipeline #${mockPipelineDetached.id} for !${mockPipelineDetached.merge_request.iid} with ${mockPipelineDetached.merge_request.source_branch}`;
      const actual = trimText(findPipelineInfoText());

      expect(actual).toBe(expected);
    });

    it(`renders the correct merge request link`, () => {
      expect(findMRLinkPath()).toBe(mockPipelineDetached.merge_request.path);
    });

    it(`renders the correct source branch link`, () => {
      expect(findSourceBranchLinkPath()).toBe(
        mockPipelineDetached.merge_request.source_branch_path,
      );
    });
  });
});