summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/directives/tooltip_spec.js
blob: 99e8b5b552b8febb9a674eed3378342b230ce38e (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
import { mount } from '@vue/test-utils';
import $ from 'jquery';
import { escape } from 'lodash';
import tooltip from '~/vue_shared/directives/tooltip';

const DEFAULT_TOOLTIP_TEMPLATE = '<div v-tooltip :title="tooltip"></div>';
const HTML_TOOLTIP_TEMPLATE = '<div v-tooltip data-html="true" :title="tooltip"></div>';

describe('Tooltip directive', () => {
  let wrapper;

  function createTooltipContainer({
    template = DEFAULT_TOOLTIP_TEMPLATE,
    text = 'some text',
  } = {}) {
    wrapper = mount(
      {
        directives: { tooltip },
        data: () => ({ tooltip: text }),
        template,
      },
      { attachTo: document.body },
    );
  }

  async function showTooltip() {
    $(wrapper.vm.$el).tooltip('show');
    jest.runOnlyPendingTimers();
    await wrapper.vm.$nextTick();
  }

  function findTooltipInnerHtml() {
    return document.querySelector('.tooltip-inner').innerHTML;
  }

  function findTooltipHtml() {
    return document.querySelector('.tooltip').innerHTML;
  }

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('with a single tooltip', () => {
    it('should have tooltip plugin applied', () => {
      createTooltipContainer();

      expect($(wrapper.vm.$el).data('bs.tooltip')).toBeDefined();
    });

    it('displays the title as tooltip', () => {
      createTooltipContainer();

      $(wrapper.vm.$el).tooltip('show');

      jest.runOnlyPendingTimers();

      const tooltipElement = document.querySelector('.tooltip-inner');

      expect(tooltipElement.textContent).toContain('some text');
    });

    it.each`
      condition                      | template                    | sanitize
      ${'does not contain any html'} | ${DEFAULT_TOOLTIP_TEMPLATE} | ${false}
      ${'contains html'}             | ${HTML_TOOLTIP_TEMPLATE}    | ${true}
    `('passes sanitize=$sanitize if the tooltip $condition', ({ template, sanitize }) => {
      createTooltipContainer({ template });

      expect($(wrapper.vm.$el).data('bs.tooltip').config.sanitize).toEqual(sanitize);
    });

    it('updates a visible tooltip', async () => {
      createTooltipContainer();

      $(wrapper.vm.$el).tooltip('show');
      jest.runOnlyPendingTimers();

      const tooltipElement = document.querySelector('.tooltip-inner');

      wrapper.vm.tooltip = 'other text';

      jest.runOnlyPendingTimers();
      await wrapper.vm.$nextTick();

      expect(tooltipElement.textContent).toContain('other text');
    });

    describe('tooltip sanitization', () => {
      it('reads tooltip content as text if data-html is not passed', async () => {
        createTooltipContainer({ text: 'sample text<script>alert("XSS!!")</script>' });

        await showTooltip();

        const result = findTooltipInnerHtml();
        expect(result).toEqual('sample text&lt;script&gt;alert("XSS!!")&lt;/script&gt;');
      });

      it('sanitizes tooltip if data-html is passed', async () => {
        createTooltipContainer({
          template: HTML_TOOLTIP_TEMPLATE,
          text: 'sample text<script>alert("XSS!!")</script>',
        });

        await showTooltip();

        const result = findTooltipInnerHtml();
        expect(result).toEqual('sample text');
        expect(result).not.toContain('XSS!!');
      });

      it('sanitizes tooltip if data-template is passed', async () => {
        const tooltipTemplate = escape(
          '<div class="tooltip" role="tooltip"><div onclick="alert(\'XSS!\')" class="arrow"></div><div class="tooltip-inner"></div></div>',
        );

        createTooltipContainer({
          template: `<div v-tooltip :title="tooltip" data-html="false" data-template="${tooltipTemplate}"></div>`,
        });

        await showTooltip();

        const result = findTooltipHtml();
        expect(result).toEqual(
          // objectionable element is removed
          '<div class="arrow"></div><div class="tooltip-inner">some text</div>',
        );
        expect(result).not.toContain('XSS!!');
      });
    });
  });

  describe('with multiple tooltips', () => {
    beforeEach(() => {
      createTooltipContainer({
        template: `
          <div>
            <div
              v-tooltip
              class="js-look-for-tooltip"
              title="foo">
            </div>
            <div
              v-tooltip
              title="bar">
            </div>
          </div>
        `,
      });
    });

    it('should have tooltip plugin applied to all instances', () => {
      expect($(wrapper.vm.$el).find('.js-look-for-tooltip').data('bs.tooltip')).toBeDefined();
    });
  });
});