summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/line_numbers_spec.js
blob: 5bedd0ccd0275ca0da0f6daf5617186760319536 (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
import { shallowMount } from '@vue/test-utils';
import { GlIcon, GlLink } from '@gitlab/ui';
import LineNumbers from '~/vue_shared/components/line_numbers.vue';

describe('Line Numbers component', () => {
  let wrapper;
  const lines = 10;

  const createComponent = () => {
    wrapper = shallowMount(LineNumbers, { propsData: { lines } });
  };

  const findGlIcon = () => wrapper.findComponent(GlIcon);
  const findLineNumbers = () => wrapper.findAllComponents(GlLink);
  const findFirstLineNumber = () => findLineNumbers().at(0);
  const findSecondLineNumber = () => findLineNumbers().at(1);

  beforeEach(() => createComponent());

  afterEach(() => wrapper.destroy());

  describe('rendering', () => {
    it('renders Line Numbers', () => {
      expect(findLineNumbers().length).toBe(lines);
      expect(findFirstLineNumber().attributes()).toMatchObject({
        id: 'L1',
        href: '#L1',
      });
    });

    it('renders a link icon', () => {
      expect(findGlIcon().props()).toMatchObject({
        size: 12,
        name: 'link',
      });
    });
  });

  describe('clicking a line number', () => {
    let firstLineNumber;
    let firstLineNumberElement;

    beforeEach(() => {
      firstLineNumber = findFirstLineNumber();
      firstLineNumberElement = firstLineNumber.element;

      jest.spyOn(firstLineNumberElement, 'scrollIntoView');
      jest.spyOn(firstLineNumberElement.classList, 'add');
      jest.spyOn(firstLineNumberElement.classList, 'remove');

      firstLineNumber.vm.$emit('click');
    });

    it('adds the highlight (hll) class', () => {
      expect(firstLineNumberElement.classList.add).toHaveBeenCalledWith('hll');
    });

    it('removes the highlight (hll) class from a previously highlighted line', () => {
      findSecondLineNumber().vm.$emit('click');

      expect(firstLineNumberElement.classList.remove).toHaveBeenCalledWith('hll');
    });

    it('scrolls the line into view', () => {
      expect(firstLineNumberElement.scrollIntoView).toHaveBeenCalledWith({
        behavior: 'smooth',
        block: 'center',
      });
    });
  });
});