summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_shared/components/icon_spec.js
blob: a22b6bd3a6723e077d122993a9074fcb380eddf3 (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
import Vue from 'vue';
import Icon from '~/vue_shared/components/icon.vue';
import mountComponent from '../../helpers/vue_mount_component_helper';

describe('Sprite Icon Component', function () {
  describe('Initialization', function () {
    let icon;

    beforeEach(function () {
      const IconComponent = Vue.extend(Icon);

      icon = mountComponent(IconComponent, {
        name: 'test',
        size: 32,
        cssClasses: 'extraclasses',
      });
    });

    afterEach(() => {
      icon.$destroy();
    });

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

    it('should have <svg> as a child element', function () {
      expect(icon.$el.tagName).toBe('svg');
    });

    it('should have <use> as a child element with the correct href', function () {
      expect(icon.$el.firstChild.tagName).toBe('use');
      expect(icon.$el.firstChild.getAttribute('xlink:href')).toBe(`${gon.sprite_icons}#test`);
    });

    it('should properly compute iconSizeClass', function () {
      expect(icon.iconSizeClass).toBe('s32');
    });

    it('forbids invalid size prop', () => {
      expect(icon.$options.props.size.validator(NaN)).toBeFalsy();
      expect(icon.$options.props.size.validator(0)).toBeFalsy();
      expect(icon.$options.props.size.validator(9001)).toBeFalsy();
    });

    it('should properly render img css', function () {
      const classList = icon.$el.classList;
      const containsSizeClass = classList.contains('s32');
      const containsCustomClass = classList.contains('extraclasses');
      expect(containsSizeClass).toBe(true);
      expect(containsCustomClass).toBe(true);
    });
  });
});