summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_shared/components/user_avatar/user_avatar_link_spec.js
blob: 50b8d49d4bdb6c25216b0c27fc38942d842486fa (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
import _ from 'underscore';
import Vue from 'vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';

describe('User Avatar Link Component', function() {
  beforeEach(function() {
    this.propsData = {
      linkHref: 'myavatarurl.com',
      imgSize: 99,
      imgSrc: 'myavatarurl.com',
      imgAlt: 'mydisplayname',
      imgCssClasses: 'myextraavatarclass',
      tooltipText: 'tooltip text',
      tooltipPlacement: 'bottom',
      username: 'username',
    };

    const UserAvatarLinkComponent = Vue.extend(UserAvatarLink);

    this.userAvatarLink = new UserAvatarLinkComponent({
      propsData: this.propsData,
    }).$mount();

    [this.userAvatarImage] = this.userAvatarLink.$children;
  });

  it('should return a defined Vue component', function() {
    expect(this.userAvatarLink).toBeDefined();
  });

  it('should have user-avatar-image registered as child component', function() {
    expect(this.userAvatarLink.$options.components.userAvatarImage).toBeDefined();
  });

  it('user-avatar-link should have user-avatar-image as child component', function() {
    expect(this.userAvatarImage).toBeDefined();
  });

  it('should render <a> as a child element', function() {
    expect(this.userAvatarLink.$el.tagName).toBe('A');
  });

  it('should have <img> as a child element', function() {
    expect(this.userAvatarLink.$el.querySelector('img')).not.toBeNull();
  });

  it('should return neccessary props as defined', function() {
    _.each(this.propsData, (val, key) => {
      expect(this.userAvatarLink[key]).toBeDefined();
    });
  });

  describe('no username', function() {
    beforeEach(function(done) {
      this.userAvatarLink.username = '';

      Vue.nextTick(done);
    });

    it('should only render image tag in link', function() {
      const childElements = this.userAvatarLink.$el.childNodes;

      expect(childElements[0].tagName).toBe('IMG');

      // Vue will render the hidden component as <!---->
      expect(childElements[1].tagName).toBeUndefined();
    });

    it('should render avatar image tooltip', function() {
      expect(this.userAvatarLink.$el.querySelector('img').dataset.originalTitle).toEqual(
        this.propsData.tooltipText,
      );
    });
  });

  describe('username', function() {
    it('should not render avatar image tooltip', function() {
      expect(this.userAvatarLink.$el.querySelector('img').dataset.originalTitle).toEqual('');
    });

    it('should render username prop in <span>', function() {
      expect(this.userAvatarLink.$el.querySelector('span').innerText.trim()).toEqual(
        this.propsData.username,
      );
    });

    it('should render text tooltip for <span>', function() {
      expect(this.userAvatarLink.$el.querySelector('span').dataset.originalTitle).toEqual(
        this.propsData.tooltipText,
      );
    });

    it('should render text tooltip placement for <span>', function() {
      expect(
        this.userAvatarLink.$el.querySelector('span').getAttribute('tooltip-placement'),
      ).toEqual(this.propsData.tooltipPlacement);
    });
  });
});