Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index_spec.js « viewer « blob « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe55a537b890cd702c6856e2a5759e15b62a579a (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* eslint-disable no-new */

import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
import { setTestTimeout } from 'helpers/timeout';
import { BlobViewer } from '~/blob/viewer/index';
import axios from '~/lib/utils/axios_utils';

const execImmediately = (callback) => {
  callback();
};

describe('Blob viewer', () => {
  let blob;
  let mock;

  const jQueryMock = {
    tooltip: jest.fn(),
  };

  setTestTimeout(2000);

  beforeEach(() => {
    window.gon.features = { refactorBlobViewer: false }; // This file is based on the old (non-refactored) blob viewer
    jest.spyOn(window, 'requestIdleCallback').mockImplementation(execImmediately);
    $.fn.extend(jQueryMock);
    mock = new MockAdapter(axios);

    loadFixtures('blob/show_readme.html');
    $('#modal-upload-blob').remove();

    mock.onGet(/blob\/.+\/README\.md/).reply(200, {
      html: '<div>testing</div>',
    });

    blob = new BlobViewer();
  });

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

  it('loads source file after switching views', async () => {
    document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();

    await axios.waitForAll();

    expect(
      document
        .querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
        .classList.contains('hidden'),
    ).toBeFalsy();
  });

  it('loads source file when line number is in hash', async () => {
    window.location.hash = '#L1';

    new BlobViewer();

    await axios.waitForAll();

    expect(
      document
        .querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
        .classList.contains('hidden'),
    ).toBeFalsy();
  });

  it('doesnt reload file if already loaded', () => {
    const asyncClick = async () => {
      document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();

      await axios.waitForAll();
    };

    return asyncClick()
      .then(() => asyncClick())
      .then(() => {
        expect(
          document.querySelector('.blob-viewer[data-type="simple"]').getAttribute('data-loaded'),
        ).toBe('true');
      });
  });

  describe('copy blob button', () => {
    let copyButton;
    let copyButtonTooltip;

    beforeEach(() => {
      copyButton = document.querySelector('.js-copy-blob-source-btn');
      copyButtonTooltip = document.querySelector('.js-copy-blob-source-btn-tooltip');
    });

    it('disabled on load', () => {
      expect(copyButton.classList.contains('disabled')).toBeTruthy();
    });

    it('has tooltip when disabled', () => {
      expect(copyButtonTooltip.getAttribute('title')).toBe(
        'Switch to the source to copy the file contents',
      );
    });

    it('is blurred when clicked and disabled', () => {
      jest.spyOn(copyButton, 'blur').mockImplementation(() => {});

      copyButton.click();

      expect(copyButton.blur).toHaveBeenCalled();
    });

    it('is not blurred when clicked and not disabled', () => {
      jest.spyOn(copyButton, 'blur').mockImplementation(() => {});

      copyButton.classList.remove('disabled');
      copyButton.click();

      expect(copyButton.blur).not.toHaveBeenCalled();
    });

    it('enables after switching to simple view', async () => {
      document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();

      await axios.waitForAll();

      expect(copyButton.classList.contains('disabled')).toBeFalsy();
    });

    it('updates tooltip after switching to simple view', async () => {
      document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();

      await axios.waitForAll();

      expect(copyButtonTooltip.getAttribute('title')).toBe('Copy file contents');
    });
  });

  describe('switchToViewer', () => {
    it('removes active class from old viewer button', () => {
      blob.switchToViewer('simple');

      expect(
        document.querySelector('.js-blob-viewer-switch-btn.active[data-viewer="rich"]'),
      ).toBeNull();
    });

    it('adds active class to new viewer button', () => {
      const simpleBtn = document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]');

      jest.spyOn(simpleBtn, 'blur').mockImplementation(() => {});

      blob.switchToViewer('simple');

      expect(simpleBtn.classList.contains('selected')).toBeTruthy();

      expect(simpleBtn.blur).toHaveBeenCalled();
    });

    it('makes request for initial view', () => {
      expect(mock.history).toMatchObject({
        get: [{ url: expect.stringMatching(/README\.md\?.*viewer=rich/) }],
      });
    });

    describe.each`
      views
      ${['simple']}
      ${['simple', 'rich']}
    `('when view switches to $views', ({ views }) => {
      beforeEach(async () => {
        views.forEach((view) => blob.switchToViewer(view));
        await axios.waitForAll();
      });

      it('sends 1 AJAX request for new view', async () => {
        expect(mock.history).toMatchObject({
          get: [
            { url: expect.stringMatching(/README\.md\?.*viewer=rich/) },
            { url: expect.stringMatching(/README\.md\?.*viewer=simple/) },
          ],
        });
      });
    });
  });
});