BigW Consortium Gitlab

commit_spec.js 4.45 KB
Newer Older
1
import Vue from 'vue';
2
import commitComp from '~/vue_shared/components/commit.vue';
Filipa Lacerda committed
3 4

describe('Commit component', () => {
5 6
  let props;
  let component;
7 8 9 10 11
  let CommitComponent;

  beforeEach(() => {
    CommitComponent = Vue.extend(commitComp);
  });
12 13

  it('should render a code-fork icon if it does not represent a tag', () => {
14
    component = new CommitComponent({
15 16
      propsData: {
        tag: false,
Filipa Lacerda committed
17
        commitRef: {
18 19 20
          name: 'master',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
        },
Filipa Lacerda committed
21 22
        commitUrl: 'https://gitlab.com/gitlab-org/gitlab-ce/commit/b7836eddf62d663c665769e1b0960197fd215067',
        shortSha: 'b7836edd',
23 24
        title: 'Commit message',
        author: {
25
          avatar_url: 'https://gitlab.com/uploads/-/system/user/avatar/300478/avatar.png',
26
          web_url: 'https://gitlab.com/jschatz1',
27
          path: '/jschatz1',
28 29
          username: 'jschatz1',
        },
Filipa Lacerda committed
30
      },
31
    }).$mount();
Filipa Lacerda committed
32

33 34
    expect(component.$el.querySelector('.icon-container i').classList).toContain('fa-code-fork');
  });
Filipa Lacerda committed
35

36 37 38 39
  describe('Given all the props', () => {
    beforeEach(() => {
      props = {
        tag: true,
Filipa Lacerda committed
40
        commitRef: {
41 42 43
          name: 'master',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
        },
Filipa Lacerda committed
44 45
        commitUrl: 'https://gitlab.com/gitlab-org/gitlab-ce/commit/b7836eddf62d663c665769e1b0960197fd215067',
        shortSha: 'b7836edd',
46 47
        title: 'Commit message',
        author: {
48
          avatar_url: 'https://gitlab.com/uploads/-/system/user/avatar/300478/avatar.png',
49
          web_url: 'https://gitlab.com/jschatz1',
50
          path: '/jschatz1',
51 52
          username: 'jschatz1',
        },
Filipa Lacerda committed
53
        commitIconSvg: '<svg></svg>',
54 55
      };

56
      component = new CommitComponent({
57
        propsData: props,
58
      }).$mount();
Filipa Lacerda committed
59 60
    });

61 62
    it('should render a tag icon if it represents a tag', () => {
      expect(component.$el.querySelector('.icon-container i').classList).toContain('fa-tag');
Filipa Lacerda committed
63 64 65
    });

    it('should render a link to the ref url', () => {
66
      expect(component.$el.querySelector('.ref-name').getAttribute('href')).toEqual(props.commitRef.ref_url);
Filipa Lacerda committed
67 68 69
    });

    it('should render the ref name', () => {
70
      expect(component.$el.querySelector('.ref-name').textContent).toContain(props.commitRef.name);
Filipa Lacerda committed
71 72
    });

73
    it('should render the commit short sha with a link to the commit url', () => {
74 75
      expect(component.$el.querySelector('.commit-sha').getAttribute('href')).toEqual(props.commitUrl);
      expect(component.$el.querySelector('.commit-sha').textContent).toContain(props.shortSha);
Filipa Lacerda committed
76 77 78 79
    });

    it('should render the given commitIconSvg', () => {
      expect(component.$el.querySelector('.js-commit-icon').children).toContain('svg');
80
    });
Filipa Lacerda committed
81

82
    describe('Given commit title and author props', () => {
Filipa Lacerda committed
83
      it('should render a link to the author profile', () => {
84
        expect(
85
          component.$el.querySelector('.commit-title .avatar-image-container').getAttribute('href'),
86
        ).toEqual(props.author.path);
87 88 89 90
      });

      it('Should render the author avatar with title and alt attributes', () => {
        expect(
91
          component.$el.querySelector('.commit-title .avatar-image-container img').getAttribute('data-original-title'),
92 93
        ).toContain(props.author.username);
        expect(
94
          component.$el.querySelector('.commit-title .avatar-image-container img').getAttribute('alt'),
95 96 97
        ).toContain(`${props.author.username}'s avatar`);
      });
    });
Filipa Lacerda committed
98

99 100
    it('should render the commit title', () => {
      expect(
101
        component.$el.querySelector('a.commit-row-message').getAttribute('href'),
Filipa Lacerda committed
102
      ).toEqual(props.commitUrl);
103
      expect(
104
        component.$el.querySelector('a.commit-row-message').textContent,
105 106
      ).toContain(props.title);
    });
Filipa Lacerda committed
107 108
  });

109
  describe('When commit title is not provided', () => {
Filipa Lacerda committed
110
    it('should render default message', () => {
111 112
      props = {
        tag: false,
Filipa Lacerda committed
113
        commitRef: {
114 115 116
          name: 'master',
          ref_url: 'http://localhost/namespace2/gitlabhq/tree/master',
        },
Filipa Lacerda committed
117 118
        commitUrl: 'https://gitlab.com/gitlab-org/gitlab-ce/commit/b7836eddf62d663c665769e1b0960197fd215067',
        shortSha: 'b7836edd',
119 120 121 122
        title: null,
        author: {},
      };

123
      component = new CommitComponent({
124
        propsData: props,
125
      }).$mount();
126 127

      expect(
128
        component.$el.querySelector('.commit-title span').textContent,
129 130
      ).toContain('Cant find HEAD commit for this branch');
    });
Filipa Lacerda committed
131 132
  });
});