summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/blob_viewers/simple_viewer_spec.js
blob: 46d4edad891ea5c6231f29b547d5a4fbb1c586a9 (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
import { shallowMount } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises';
import { HIGHLIGHT_CLASS_NAME } from '~/vue_shared/components/blob_viewers/constants';
import SimpleViewer from '~/vue_shared/components/blob_viewers/simple_viewer.vue';
import EditorLite from '~/vue_shared/components/editor_lite.vue';

describe('Blob Simple Viewer component', () => {
  let wrapper;
  const contentMock = `<span id="LC1">First</span>\n<span id="LC2">Second</span>\n<span id="LC3">Third</span>`;
  const blobHash = 'foo-bar';

  function createComponent(
    content = contentMock,
    isRawContent = false,
    isRefactorFlagEnabled = false,
  ) {
    wrapper = shallowMount(SimpleViewer, {
      provide: {
        blobHash,
        glFeatures: {
          refactorBlobViewer: isRefactorFlagEnabled,
        },
      },
      propsData: {
        content,
        type: 'text',
        fileName: 'test.js',
        isRawContent,
      },
    });
  }

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

  it('does not fail if content is empty', () => {
    const spy = jest.spyOn(window.console, 'error');
    createComponent('');
    expect(spy).not.toHaveBeenCalled();
  });

  describe('rendering', () => {
    beforeEach(() => {
      createComponent();
    });

    it('matches the snapshot', () => {
      expect(wrapper.element).toMatchSnapshot();
    });

    it('renders exactly three lines', () => {
      expect(wrapper.findAll('.js-line-number')).toHaveLength(3);
    });

    it('renders the content without transformations', () => {
      expect(wrapper.html()).toContain(contentMock);
    });
  });

  describe('functionality', () => {
    const scrollIntoViewMock = jest.fn();
    HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;

    beforeEach(() => {
      window.location.hash = '#LC2';
      createComponent();
    });

    afterEach(() => {
      window.location.hash = '';
    });

    it('scrolls to requested line when rendered', () => {
      const linetoBeHighlighted = wrapper.find('#LC2');
      expect(scrollIntoViewMock).toHaveBeenCalled();
      expect(wrapper.vm.highlightedLine).toBe(linetoBeHighlighted.element);
      expect(linetoBeHighlighted.classes()).toContain(HIGHLIGHT_CLASS_NAME);
    });

    it('switches highlighting when another line is selected', () => {
      const currentlyHighlighted = wrapper.find('#LC2');
      const hash = '#LC3';
      const linetoBeHighlighted = wrapper.find(hash);

      expect(wrapper.vm.highlightedLine).toBe(currentlyHighlighted.element);

      wrapper.vm.scrollToLine(hash);

      return wrapper.vm.$nextTick(() => {
        expect(wrapper.vm.highlightedLine).toBe(linetoBeHighlighted.element);
        expect(currentlyHighlighted.classes()).not.toContain(HIGHLIGHT_CLASS_NAME);
        expect(linetoBeHighlighted.classes()).toContain(HIGHLIGHT_CLASS_NAME);
      });
    });
  });

  describe('Vue refactoring to use Source Editor', () => {
    const findEditorLite = () => wrapper.find(EditorLite);

    it.each`
      doesRender    | condition                                          | isRawContent | isRefactorFlagEnabled
      ${'Does not'} | ${'rawContent is not specified'}                   | ${false}     | ${true}
      ${'Does not'} | ${'feature flag is disabled is not specified'}     | ${true}      | ${false}
      ${'Does not'} | ${'both, the FF and rawContent are not specified'} | ${false}     | ${false}
      ${'Does'}     | ${'both, the FF and rawContent are specified'}     | ${true}      | ${true}
    `(
      '$doesRender render Editor Lite component in readonly mode when $condition',
      async ({ isRawContent, isRefactorFlagEnabled } = {}) => {
        createComponent('raw content', isRawContent, isRefactorFlagEnabled);
        await waitForPromises();

        if (isRawContent && isRefactorFlagEnabled) {
          expect(findEditorLite().exists()).toBe(true);

          expect(findEditorLite().props('value')).toBe('raw content');
          expect(findEditorLite().props('fileName')).toBe('test.js');
          expect(findEditorLite().props('editorOptions')).toEqual({ readOnly: true });
        } else {
          expect(findEditorLite().exists()).toBe(false);
        }
      },
    );
  });
});