summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/commit_spec.js
blob: d91853e7b7928b62f326415014e8376d64a64938 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { GlIcon } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import CommitComponent from '~/vue_shared/components/commit.vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';

describe('Commit component', () => {
  let props;
  let wrapper;

  const findIcon = (name) => {
    const icons = wrapper.findAll(GlIcon).filter((c) => c.attributes('name') === name);
    return icons.length ? icons.at(0) : icons;
  };

  const findUserAvatar = () => wrapper.find(UserAvatarLink);
  const findRefName = () => wrapper.findByTestId('ref-name');

  const createComponent = (propsData) => {
    wrapper = extendedWrapper(
      shallowMount(CommitComponent, {
        propsData,
      }),
    );
  };

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

  it('should render a fork icon if it does not represent a tag', () => {
    createComponent({
      tag: false,
      commitRef: {
        name: 'main',
        ref_url: 'http://localhost/namespace2/gitlabhq/tree/main',
      },
      commitUrl:
        'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
      shortSha: 'b7836edd',
      title: 'Commit message',
      author: {
        avatar_url: 'https://gitlab.com/uploads/-/system/user/avatar/300478/avatar.png',
        web_url: 'https://gitlab.com/jschatz1',
        path: '/jschatz1',
        username: 'jschatz1',
      },
    });

    expect(wrapper.find('.icon-container').find(GlIcon).exists()).toBe(true);
  });

  describe('Given all the props', () => {
    beforeEach(() => {
      props = {
        tag: true,
        commitRef: {
          name: 'main',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/main',
        },
        commitUrl:
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
        shortSha: 'b7836edd',
        title: 'Commit message',
        author: {
          avatar_url: 'https://gitlab.com/uploads/-/system/user/avatar/300478/avatar.png',
          web_url: 'https://gitlab.com/jschatz1',
          path: '/jschatz1',
          username: 'jschatz1',
        },
      };
      createComponent(props);
    });

    it('should render a tag icon if it represents a tag', () => {
      expect(findIcon('tag').exists()).toBe(true);
    });

    it('should render a link to the ref url', () => {
      expect(wrapper.find('.ref-name').attributes('href')).toBe(props.commitRef.ref_url);
    });

    it('should render the ref name', () => {
      expect(wrapper.find('.ref-name').text()).toContain(props.commitRef.name);
    });

    it('should render the commit short sha with a link to the commit url', () => {
      expect(wrapper.find('.commit-sha').attributes('href')).toEqual(props.commitUrl);

      expect(wrapper.find('.commit-sha').text()).toContain(props.shortSha);
    });

    it('should render icon for commit', () => {
      expect(findIcon('commit').exists()).toBe(true);
    });

    describe('Given commit title and author props', () => {
      it('should render a link to the author profile', () => {
        const userAvatar = findUserAvatar();

        expect(userAvatar.props('linkHref')).toBe(props.author.path);
      });

      it('Should render the author avatar with title and alt attributes', () => {
        const userAvatar = findUserAvatar();

        expect(userAvatar.exists()).toBe(true);

        expect(userAvatar.props('imgAlt')).toBe(`${props.author.username}'s avatar`);
      });
    });

    it('should render the commit title', () => {
      expect(wrapper.find('.commit-row-message').attributes('href')).toEqual(props.commitUrl);

      expect(wrapper.find('.commit-row-message').text()).toContain(props.title);
    });
  });

  describe('When commit title is not provided', () => {
    it('should render default message', () => {
      props = {
        tag: false,
        commitRef: {
          name: 'main',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/main',
        },
        commitUrl:
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
        shortSha: 'b7836edd',
        title: null,
        author: {},
      };

      createComponent(props);

      expect(wrapper.find('.commit-title span').text()).toContain(
        "Can't find HEAD commit for this branch",
      );
    });
  });

  describe('When commit ref is provided, but merge ref is not', () => {
    it('should render the commit ref', () => {
      props = {
        tag: false,
        commitRef: {
          name: 'main',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/main',
        },
        commitUrl:
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
        shortSha: 'b7836edd',
        title: null,
        author: {},
      };

      createComponent(props);
      const refEl = wrapper.find('.ref-name');

      expect(refEl.text()).toContain('main');

      expect(refEl.attributes('href')).toBe(props.commitRef.ref_url);

      expect(findIcon('branch').exists()).toBe(true);
    });
  });

  describe('When both commit and merge ref are provided', () => {
    it('should render the merge ref', () => {
      props = {
        tag: false,
        commitRef: {
          name: 'main',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/main',
        },
        commitUrl:
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
        mergeRequestRef: {
          iid: 1234,
          path: 'https://example.com/path/to/mr',
          title: 'Test MR',
        },
        shortSha: 'b7836edd',
        title: null,
        author: {},
      };

      createComponent(props);
      const refEl = wrapper.find('.ref-name');

      expect(refEl.text()).toContain('1234');

      expect(refEl.attributes('href')).toBe(props.mergeRequestRef.path);

      expect(findIcon('git-merge').exists()).toBe(true);
    });
  });

  describe('When showRefInfo === false', () => {
    it('should not render any ref info', () => {
      props = {
        tag: false,
        commitRef: {
          name: 'main',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/main',
        },
        commitUrl:
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
        mergeRequestRef: {
          iid: 1234,
          path: '/path/to/mr',
          title: 'Test MR',
        },
        shortSha: 'b7836edd',
        title: null,
        author: {},
        showRefInfo: false,
      };

      createComponent(props);

      expect(wrapper.find('.ref-name').exists()).toBe(false);
    });
  });

  describe('When commitRef has a path property instead of ref_url property', () => {
    it('should render path as href attribute', () => {
      props = {
        commitRef: {
          name: 'main',
          path: 'http://localhost/namespace2/gitlabhq/tree/main',
        },
      };

      createComponent(props);

      expect(findRefName().exists()).toBe(true);
      expect(findRefName().attributes('href')).toBe(props.commitRef.path);
    });
  });
});