summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_shared/components/tooltip_on_truncate_spec.js
blob: 8465757deb6a12f70033cf402eb7345777bb0849 (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
import { mountComponentWithRender } from 'spec/helpers/vue_mount_component_helper';
import TooltipOnTruncate from '~/vue_shared/components/tooltip_on_truncate.vue';

const TEST_TITLE = 'lorem-ipsum-dolar-sit-amit-consectur-adipiscing-elit-sed-do';
const CLASS_SHOW_TOOLTIP = 'js-show-tooltip';
const STYLE_TRUNCATED = {
  display: 'inline-block',
  'max-width': '20px',
};
const STYLE_NORMAL = {
  display: 'inline-block',
  'max-width': '1000px',
};

function mountTooltipOnTruncate(options, createChildren) {
  return mountComponentWithRender(h => h(TooltipOnTruncate, options, createChildren(h)), '#app');
}

describe('TooltipOnTruncate component', () => {
  let vm;

  beforeEach(() => {
    const el = document.createElement('div');
    el.id = 'app';
    document.body.appendChild(el);
  });

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

  describe('with default target', () => {
    it('renders tooltip if truncated', done => {
      const options = {
        style: STYLE_TRUNCATED,
        props: {
          title: TEST_TITLE,
        },
      };

      vm = mountTooltipOnTruncate(options, () => [TEST_TITLE]);

      vm.$nextTick()
        .then(() => {
          expect(vm.$el).toHaveClass(CLASS_SHOW_TOOLTIP);
          expect(vm.$el).toHaveData('original-title', TEST_TITLE);
          expect(vm.$el).toHaveData('placement', 'top');
        })
        .then(done)
        .catch(done.fail);
    });

    it('does not render tooltip if normal', done => {
      const options = {
        style: STYLE_NORMAL,
        props: {
          title: TEST_TITLE,
        },
      };

      vm = mountTooltipOnTruncate(options, () => [TEST_TITLE]);

      vm.$nextTick()
        .then(() => {
          expect(vm.$el).not.toHaveClass(CLASS_SHOW_TOOLTIP);
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('with child target', () => {
    it('renders tooltip if truncated', done => {
      const options = {
        style: STYLE_NORMAL,
        props: {
          title: TEST_TITLE,
          truncateTarget: 'child',
        },
      };

      vm = mountTooltipOnTruncate(options, (h) => [
        h('a', { style: STYLE_TRUNCATED }, TEST_TITLE),
      ]);

      vm.$nextTick()
        .then(() => {
          expect(vm.$el).toHaveClass(CLASS_SHOW_TOOLTIP);
        })
        .then(done)
        .catch(done.fail);
    });

    it('does not render tooltip if normal', done => {
      const options = {
        props: {
          title: TEST_TITLE,
          truncateTarget: 'child',
        },
      };

      vm = mountTooltipOnTruncate(options, (h) => [
        h('a', { style: STYLE_NORMAL }, TEST_TITLE),
      ]);

      vm.$nextTick()
        .then(() => {
          expect(vm.$el).not.toHaveClass(CLASS_SHOW_TOOLTIP);
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('with fn target', () => {
    it('renders tooltip if truncated', done => {
      const options = {
        style: STYLE_NORMAL,
        props: {
          title: TEST_TITLE,
          truncateTarget: (el) => el.childNodes[1],
        },
      };

      vm = mountTooltipOnTruncate(options, (h) => [
        h('a', { style: STYLE_NORMAL }, TEST_TITLE),
        h('span', { style: STYLE_TRUNCATED }, TEST_TITLE),
      ]);

      vm.$nextTick()
        .then(() => {
          expect(vm.$el).toHaveClass(CLASS_SHOW_TOOLTIP);
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('placement', () => {
    it('sets data-placement when tooltip is rendered', done => {
      const options = {
        props: {
          title: TEST_TITLE,
          truncateTarget: 'child',
          placement: 'bottom',
        },
      };

      vm = mountTooltipOnTruncate(options, (h) => [
        h('a', { style: STYLE_TRUNCATED }, TEST_TITLE),
      ]);

      vm.$nextTick()
        .then(() => {
          expect(vm.$el).toHaveClass(CLASS_SHOW_TOOLTIP);
          expect(vm.$el).toHaveData('placement', options.props.placement);
        })
        .then(done)
        .catch(done.fail);
    });
  });
});