summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/source_viewer/components/chunk_spec.js
blob: 42c4f2eacb838fa2d01d65349cd8d9042a6ebd35 (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
import { GlIntersectionObserver } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import Chunk from '~/vue_shared/components/source_viewer/components/chunk.vue';
import ChunkLine from '~/vue_shared/components/source_viewer/components/chunk_line.vue';

const DEFAULT_PROPS = {
  chunkIndex: 2,
  isHighlighted: false,
  content: '// Line 1 content \n // Line 2 content',
  startingFrom: 140,
  totalLines: 50,
  language: 'javascript',
};

describe('Chunk component', () => {
  let wrapper;

  const createComponent = (props = {}) => {
    wrapper = shallowMountExtended(Chunk, { propsData: { ...DEFAULT_PROPS, ...props } });
  };

  const findIntersectionObserver = () => wrapper.findComponent(GlIntersectionObserver);
  const findChunkLines = () => wrapper.findAllComponents(ChunkLine);
  const findLineNumbers = () => wrapper.findAllByTestId('line-number');
  const findContent = () => wrapper.findByTestId('content');

  beforeEach(() => {
    createComponent();
  });

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

  describe('Intersection observer', () => {
    it('renders an Intersection observer component', () => {
      expect(findIntersectionObserver().exists()).toBe(true);
    });

    it('emits an appear event when intersection-observer appears', () => {
      findIntersectionObserver().vm.$emit('appear');

      expect(wrapper.emitted('appear')).toEqual([[DEFAULT_PROPS.chunkIndex]]);
    });

    it('does not emit an appear event is isHighlighted is true', () => {
      createComponent({ isHighlighted: true });
      findIntersectionObserver().vm.$emit('appear');

      expect(wrapper.emitted('appear')).toEqual(undefined);
    });
  });

  describe('rendering', () => {
    it('does not render a Chunk Line component if isHighlighted is false', () => {
      expect(findChunkLines().length).toBe(0);
    });

    it('renders simplified line numbers and content if isHighlighted is false', () => {
      expect(findLineNumbers().length).toBe(DEFAULT_PROPS.totalLines);

      expect(findLineNumbers().at(0).attributes()).toMatchObject({
        'data-line-number': `${DEFAULT_PROPS.startingFrom + 1}`,
        href: `#L${DEFAULT_PROPS.startingFrom + 1}`,
        id: `L${DEFAULT_PROPS.startingFrom + 1}`,
      });

      expect(findContent().text()).toBe(DEFAULT_PROPS.content);
    });

    it('renders Chunk Line components if isHighlighted is true', () => {
      const splitContent = DEFAULT_PROPS.content.split('\n');
      createComponent({ isHighlighted: true });

      expect(findChunkLines().length).toBe(splitContent.length);

      expect(findChunkLines().at(0).props()).toMatchObject({
        number: DEFAULT_PROPS.startingFrom + 1,
        content: splitContent[0],
        language: DEFAULT_PROPS.language,
      });
    });
  });
});