summaryrefslogtreecommitdiff
path: root/spec/javascripts/blob/viewer/index_spec.js
blob: 4ac15ca5aa27a34624097add578ffce56e5ed044 (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
/* eslint-disable no-new */

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

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

  preloadFixtures('snippets/show.html');

  beforeEach(() => {
    mock = new MockAdapter(axios);

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

    blob = new BlobViewer();

    mock.onGet('http://test.host/snippets/1.json?viewer=rich').reply(200, {
      html: '<div>testing</div>',
    });

    mock.onGet('http://test.host/snippets/1.json?viewer=simple').reply(200, {
      html: '<div>testing</div>',
    });

    spyOn(axios, 'get').and.callThrough();
  });

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

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

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

      done();
    });
  });

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

    new BlobViewer();

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

      done();
    });
  });

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

        setTimeout(resolve);
      });

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

        done();
      })
      .catch(() => {
        fail();
        done();
      });
  });

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

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

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

    it('has tooltip when disabled', () => {
      expect(copyButton.getAttribute('data-original-title')).toBe(
        'Switch to the source to copy it to the clipboard',
      );
    });

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

      copyButton.click();

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

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

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

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

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

      setTimeout(() => {
        expect(copyButton.classList.contains('disabled')).toBeFalsy();

        done();
      });
    });

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

      setTimeout(() => {
        expect(copyButton.getAttribute('data-original-title')).toBe('Copy source to clipboard');

        done();
      });
    });
  });

  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"]');

      spyOn(simpleBtn, 'blur');

      blob.switchToViewer('simple');

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

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

    it('sends AJAX request when switching to simple view', () => {
      blob.switchToViewer('simple');

      expect(axios.get).toHaveBeenCalled();
    });

    it('does not send AJAX request when switching to rich view', () => {
      blob.switchToViewer('simple');
      blob.switchToViewer('rich');

      expect(axios.get.calls.count()).toBe(1);
    });
  });
});