summaryrefslogtreecommitdiff
path: root/spec/frontend/pipelines/pipeline_url_spec.js
blob: c62898f0c834d282e9f5a2840b38d5523dad42a4 (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
import { merge } from 'lodash';
import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import PipelineUrlComponent from '~/pipelines/components/pipelines_list/pipeline_url.vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';
import { TRACKING_CATEGORIES } from '~/pipelines/constants';
import { mockPipeline, mockPipelineBranch, mockPipelineTag } from './mock_data';

const projectPath = 'test/test';

describe('Pipeline Url Component', () => {
  let wrapper;
  let trackingSpy;

  const findTableCell = () => wrapper.findByTestId('pipeline-url-table-cell');
  const findPipelineUrlLink = () => wrapper.findByTestId('pipeline-url-link');
  const findRefName = () => wrapper.findByTestId('merge-request-ref');
  const findCommitShortSha = () => wrapper.findByTestId('commit-short-sha');
  const findCommitIcon = () => wrapper.findByTestId('commit-icon');
  const findCommitIconType = () => wrapper.findByTestId('commit-icon-type');
  const findCommitRefName = () => wrapper.findByTestId('commit-ref-name');

  const findCommitTitleContainer = () => wrapper.findByTestId('commit-title-container');
  const findPipelineNameContainer = () => wrapper.findByTestId('pipeline-name-container');
  const findCommitTitle = (commitWrapper) => commitWrapper.find('[data-testid="commit-title"]');

  const defaultProps = mockPipeline(projectPath);

  const createComponent = (props) => {
    wrapper = shallowMountExtended(PipelineUrlComponent, {
      propsData: { ...defaultProps, ...props },
      provide: {
        targetProjectFullPath: projectPath,
      },
    });
  };

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

  it('should render pipeline url table cell', () => {
    createComponent();

    expect(findTableCell().exists()).toBe(true);
  });

  it('should render a link the provided path and id', () => {
    createComponent();

    expect(findPipelineUrlLink().attributes('href')).toBe('foo');

    expect(findPipelineUrlLink().text()).toBe('#1');
  });

  it('should render the pipeline name instead of commit title', () => {
    createComponent(merge(mockPipeline(projectPath), { pipeline: { name: 'Build pipeline' } }));

    expect(findCommitTitleContainer().exists()).toBe(false);
    expect(findPipelineNameContainer().exists()).toBe(true);
    expect(findRefName().exists()).toBe(true);
    expect(findCommitShortSha().exists()).toBe(true);
  });

  it('should render the commit title when pipeline has no name', () => {
    createComponent();

    const commitWrapper = findCommitTitleContainer();

    expect(findCommitTitle(commitWrapper).exists()).toBe(true);
    expect(findRefName().exists()).toBe(true);
    expect(findCommitShortSha().exists()).toBe(true);
    expect(findPipelineNameContainer().exists()).toBe(false);
  });

  describe('commit user avatar', () => {
    it('renders when commit author exists', () => {
      const pipelineBranch = mockPipelineBranch();
      const { avatar_url: imgSrc, name, path } = pipelineBranch.pipeline.commit.author;
      createComponent(pipelineBranch);

      const component = wrapper.findComponent(UserAvatarLink);
      expect(component.exists()).toBe(true);
      expect(component.props()).toMatchObject({
        imgSize: 16,
        imgSrc,
        imgAlt: name,
        linkHref: path,
        tooltipText: name,
      });
    });

    it('does not render when commit author does not exist', () => {
      createComponent();

      expect(wrapper.findComponent(UserAvatarLink).exists()).toBe(false);
    });
  });

  it('should render commit icon tooltip', () => {
    createComponent();

    expect(findCommitIcon().attributes('title')).toBe('Commit');
  });

  it.each`
    pipeline                | expectedTitle
    ${mockPipelineTag()}    | ${'Tag'}
    ${mockPipelineBranch()} | ${'Branch'}
    ${mockPipeline()}       | ${'Merge Request'}
  `('should render tooltip $expectedTitle for commit icon type', ({ pipeline, expectedTitle }) => {
    createComponent(pipeline);

    expect(findCommitIconType().attributes('title')).toBe(expectedTitle);
  });

  describe('tracking', () => {
    beforeEach(() => {
      trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);
    });

    afterEach(() => {
      unmockTracking();
    });

    it('tracks pipeline id click', () => {
      createComponent();

      findPipelineUrlLink().vm.$emit('click');

      expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_pipeline_id', {
        label: TRACKING_CATEGORIES.table,
      });
    });

    it('tracks merge request ref click', () => {
      createComponent();

      findRefName().vm.$emit('click');

      expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_mr_ref', {
        label: TRACKING_CATEGORIES.table,
      });
    });

    it('tracks commit ref name click', () => {
      createComponent(mockPipelineBranch());

      findCommitRefName().vm.$emit('click');

      expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_commit_name', {
        label: TRACKING_CATEGORIES.table,
      });
    });

    it('tracks commit title click', () => {
      createComponent(merge(mockPipelineBranch(), { pipeline: { name: null } }));

      findCommitTitle(findCommitTitleContainer()).vm.$emit('click');

      expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_commit_title', {
        label: TRACKING_CATEGORIES.table,
      });
    });

    it('tracks commit short sha click', () => {
      createComponent(mockPipelineBranch());

      findCommitShortSha().vm.$emit('click');

      expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_commit_sha', {
        label: TRACKING_CATEGORIES.table,
      });
    });
  });
});