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

index_spec.js « viewer « blob « javascripts « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c95a40689c7697fcf9f17ab1e7703a2ae6ac1925 (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
/* eslint-disable no-new */
import BlobViewer from '~/blob/viewer/index';

describe('Blob viewer', () => {
  preloadFixtures('blob/show.html.raw');

  beforeEach(() => {
    loadFixtures('blob/show.html.raw');
    $('#modal-upload-blob').remove();

    new BlobViewer();

    spyOn($, 'ajax').and.callFake(() => {
      const d = $.Deferred();

      d.resolve({
        html: '<div>testing</div>',
      });

      return d.promise();
    });
  });

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

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

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

      done();
    });
  });

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

    new BlobViewer();

    setTimeout(() => {
      expect($.ajax).toHaveBeenCalled();
      expect(
        document.querySelector('.js-blob-viewer-switcher[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-switcher[data-viewer="simple"]').click();

      setTimeout(resolve);
    });

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

        done();
      });
  });

  describe('copy blob button', () => {
    it('disabled on load', () => {
      expect(
        document.querySelector('.js-copy-blob-source-btn').classList.contains('disabled'),
      ).toBeTruthy();
    });

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

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

      setTimeout(() => {
        expect($.ajax).toHaveBeenCalled();
        expect(
          document.querySelector('.js-copy-blob-source-btn').classList.contains('disabled'),
        ).toBeFalsy();

        done();
      });
    });

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

      setTimeout(() => {
        expect($.ajax).toHaveBeenCalled();

        expect(
          document.querySelector('.js-copy-blob-source-btn').getAttribute('data-original-title'),
        ).toBe('Copy to clipboard');

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