summaryrefslogtreecommitdiff
path: root/spec/frontend/code_navigation/utils/index_spec.js
blob: 682c8bce8c5fe80a9bc3156eb4750b3e8efcddd2 (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
import {
  cachedData,
  getCurrentHoverElement,
  setCurrentHoverElement,
  addInteractionClass,
} from '~/code_navigation/utils';

afterEach(() => {
  if (cachedData.has('current')) {
    cachedData.delete('current');
  }
});

describe('getCurrentHoverElement', () => {
  it.each`
    value
    ${'test'}
    ${undefined}
  `('it returns cached current key', ({ value }) => {
    if (value) {
      cachedData.set('current', value);
    }

    expect(getCurrentHoverElement()).toEqual(value);
  });
});

describe('setCurrentHoverElement', () => {
  it('sets cached current key', () => {
    setCurrentHoverElement('test');

    expect(getCurrentHoverElement()).toEqual('test');
  });
});

describe('addInteractionClass', () => {
  beforeEach(() => {
    setFixtures(
      '<div data-path="index.js"><div class="blob-content"><div id="LC1" class="line"><span>console</span><span>.</span><span>log</span></div><div id="LC2" class="line"><span>function</span></div></div></div>',
    );
  });

  it.each`
    line | char | index
    ${0} | ${0} | ${0}
    ${0} | ${8} | ${2}
    ${1} | ${0} | ${0}
    ${1} | ${0} | ${0}
  `(
    'it sets code navigation attributes for line $line and character $char',
    ({ line, char, index }) => {
      addInteractionClass({ path: 'index.js', d: { start_line: line, start_char: char } });

      expect(document.querySelectorAll(`#LC${line + 1} span`)[index].classList).toContain(
        'js-code-navigation',
      );
    },
  );

  describe('wrapTextNodes', () => {
    beforeEach(() => {
      setFixtures(
        '<div data-path="index.js"><div class="blob-content"><div id="LC1" class="line"> Text </div></div></div>',
      );
    });

    const params = { path: 'index.js', d: { start_line: 0, start_char: 0 } };
    const findAllSpans = () => document.querySelectorAll('#LC1 span');

    it('does not wrap text nodes by default', () => {
      addInteractionClass(params);
      const spans = findAllSpans();
      expect(spans.length).toBe(0);
    });

    it('wraps text nodes if wrapTextNodes is true', () => {
      addInteractionClass({ ...params, wrapTextNodes: true });
      const spans = findAllSpans();

      expect(spans.length).toBe(3);
      expect(spans[0].textContent).toBe(' ');
      expect(spans[1].textContent).toBe('Text');
      expect(spans[2].textContent).toBe(' ');
    });
  });
});