commit_spec.js 6.77 KB
Newer Older
1 2 3 4
import { shallowMount } from '@vue/test-utils';
import CommitComponent from '~/vue_shared/components/commit.vue';
import Icon from '~/vue_shared/components/icon.vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';
Filipa Lacerda's avatar
Filipa Lacerda committed
5 6

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

10 11 12 13 14
  const findIcon = name => {
    const icons = wrapper.findAll(Icon).filter(c => c.attributes('name') === name);
    return icons.length ? icons.at(0) : icons;
  };

15 16 17 18 19 20
  const findUserAvatar = () => wrapper.find(UserAvatarLink);

  const createComponent = propsData => {
    wrapper = shallowMount(CommitComponent, {
      propsData,
      sync: false,
21
      attachToDocument: true,
22 23
    });
  };
24

Filipa Lacerda's avatar
Filipa Lacerda committed
25
  afterEach(() => {
26
    wrapper.destroy();
Filipa Lacerda's avatar
Filipa Lacerda committed
27 28
  });

29
  it('should render a fork icon if it does not represent a tag', () => {
30
    createComponent({
Filipa Lacerda's avatar
Filipa Lacerda committed
31 32 33 34
      tag: false,
      commitRef: {
        name: 'master',
        ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
Filipa Lacerda's avatar
Filipa Lacerda committed
35
      },
Filipa Lacerda's avatar
Filipa Lacerda committed
36
      commitUrl:
37
        'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
Filipa Lacerda's avatar
Filipa Lacerda committed
38 39 40 41 42 43 44 45 46
      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',
      },
    });
Filipa Lacerda's avatar
Filipa Lacerda committed
47

48 49 50 51 52 53
    expect(
      wrapper
        .find('.icon-container')
        .find(Icon)
        .exists(),
    ).toBe(true);
54
  });
Filipa Lacerda's avatar
Filipa Lacerda committed
55

56 57 58 59
  describe('Given all the props', () => {
    beforeEach(() => {
      props = {
        tag: true,
Filipa Lacerda's avatar
Filipa Lacerda committed
60
        commitRef: {
61 62 63
          name: 'master',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
        },
Filipa Lacerda's avatar
Filipa Lacerda committed
64
        commitUrl:
65
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
Filipa Lacerda's avatar
Filipa Lacerda committed
66
        shortSha: 'b7836edd',
67 68
        title: 'Commit message',
        author: {
69
          avatar_url: 'https://gitlab.com/uploads/-/system/user/avatar/300478/avatar.png',
70
          web_url: 'https://gitlab.com/jschatz1',
71
          path: '/jschatz1',
72 73 74
          username: 'jschatz1',
        },
      };
75
      createComponent(props);
Filipa Lacerda's avatar
Filipa Lacerda committed
76 77
    });

78
    it('should render a tag icon if it represents a tag', () => {
79
      expect(findIcon('tag').exists()).toBe(true);
Filipa Lacerda's avatar
Filipa Lacerda committed
80 81 82
    });

    it('should render a link to the ref url', () => {
83
      expect(wrapper.find('.ref-name').attributes('href')).toBe(props.commitRef.ref_url);
Filipa Lacerda's avatar
Filipa Lacerda committed
84 85 86
    });

    it('should render the ref name', () => {
87
      expect(wrapper.find('.ref-name').text()).toContain(props.commitRef.name);
Filipa Lacerda's avatar
Filipa Lacerda committed
88 89
    });

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

93
      expect(wrapper.find('.commit-sha').text()).toContain(props.shortSha);
Filipa Lacerda's avatar
Filipa Lacerda committed
94 95
    });

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

100
    describe('Given commit title and author props', () => {
Filipa Lacerda's avatar
Filipa Lacerda committed
101
      it('should render a link to the author profile', () => {
102 103 104
        const userAvatar = findUserAvatar();

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

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

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

        expect(userAvatar.props('imgAlt')).toBe(`${props.author.username}'s avatar`);
113 114
      });
    });
Filipa Lacerda's avatar
Filipa Lacerda committed
115

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

119
      expect(wrapper.find('.commit-row-message').text()).toContain(props.title);
120
    });
Filipa Lacerda's avatar
Filipa Lacerda committed
121 122
  });

123
  describe('When commit title is not provided', () => {
Filipa Lacerda's avatar
Filipa Lacerda committed
124
    it('should render default message', () => {
125 126
      props = {
        tag: false,
Filipa Lacerda's avatar
Filipa Lacerda committed
127
        commitRef: {
128 129 130
          name: 'master',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
        },
Filipa Lacerda's avatar
Filipa Lacerda committed
131
        commitUrl:
132
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
Filipa Lacerda's avatar
Filipa Lacerda committed
133
        shortSha: 'b7836edd',
134 135 136 137
        title: null,
        author: {},
      };

138
      createComponent(props);
139

140
      expect(wrapper.find('.commit-title span').text()).toContain(
Filipa Lacerda's avatar
Filipa Lacerda committed
141
        "Can't find HEAD commit for this branch",
Filipa Lacerda's avatar
Filipa Lacerda committed
142
      );
143
    });
Filipa Lacerda's avatar
Filipa Lacerda committed
144
  });
Nathan Friend's avatar
Nathan Friend committed
145 146 147 148 149 150 151 152 153 154

  describe('When commit ref is provided, but merge ref is not', () => {
    it('should render the commit ref', () => {
      props = {
        tag: false,
        commitRef: {
          name: 'master',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
        },
        commitUrl:
155
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
Nathan Friend's avatar
Nathan Friend committed
156 157 158 159 160
        shortSha: 'b7836edd',
        title: null,
        author: {},
      };

161 162
      createComponent(props);
      const refEl = wrapper.find('.ref-name');
Nathan Friend's avatar
Nathan Friend committed
163

164
      expect(refEl.text()).toContain('master');
Nathan Friend's avatar
Nathan Friend committed
165

166
      expect(refEl.attributes('href')).toBe(props.commitRef.ref_url);
Nathan Friend's avatar
Nathan Friend committed
167

168
      expect(refEl.attributes('title')).toBe(props.commitRef.name);
Nathan Friend's avatar
Nathan Friend committed
169

170
      expect(findIcon('branch').exists()).toBe(true);
Nathan Friend's avatar
Nathan Friend committed
171 172 173 174 175 176 177 178 179 180 181 182
    });
  });

  describe('When both commit and merge ref are provided', () => {
    it('should render the merge ref', () => {
      props = {
        tag: false,
        commitRef: {
          name: 'master',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
        },
        commitUrl:
183
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
Nathan Friend's avatar
Nathan Friend committed
184 185 186 187 188 189 190 191 192 193
        mergeRequestRef: {
          iid: 1234,
          path: 'https://example.com/path/to/mr',
          title: 'Test MR',
        },
        shortSha: 'b7836edd',
        title: null,
        author: {},
      };

194 195
      createComponent(props);
      const refEl = wrapper.find('.ref-name');
Nathan Friend's avatar
Nathan Friend committed
196

197
      expect(refEl.text()).toContain('1234');
Nathan Friend's avatar
Nathan Friend committed
198

199
      expect(refEl.attributes('href')).toBe(props.mergeRequestRef.path);
Nathan Friend's avatar
Nathan Friend committed
200

201
      expect(refEl.attributes('title')).toBe(props.mergeRequestRef.title);
Nathan Friend's avatar
Nathan Friend committed
202

203
      expect(findIcon('git-merge').exists()).toBe(true);
Nathan Friend's avatar
Nathan Friend committed
204 205 206 207 208 209 210 211 212 213 214 215
    });
  });

  describe('When showRefInfo === false', () => {
    it('should not render any ref info', () => {
      props = {
        tag: false,
        commitRef: {
          name: 'master',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
        },
        commitUrl:
216
          'https://gitlab.com/gitlab-org/gitlab-foss/commit/b7836eddf62d663c665769e1b0960197fd215067',
Nathan Friend's avatar
Nathan Friend committed
217 218 219 220 221 222 223 224 225 226 227
        mergeRequestRef: {
          iid: 1234,
          path: '/path/to/mr',
          title: 'Test MR',
        },
        shortSha: 'b7836edd',
        title: null,
        author: {},
        showRefInfo: false,
      };

228
      createComponent(props);
Nathan Friend's avatar
Nathan Friend committed
229

230
      expect(wrapper.find('.ref-name').exists()).toBe(false);
Nathan Friend's avatar
Nathan Friend committed
231 232
    });
  });
Filipa Lacerda's avatar
Filipa Lacerda committed
233
});