merge_request_tabs_spec.js 7.1 KB
Newer Older
1
/* eslint-disable no-var, comma-dangle, object-shorthand */
F
Fatih Acet 已提交
2

3 4 5
require('~/merge_request_tabs');
require('~/breakpoints');
require('~/lib/utils/common_utils');
6
require('vendor/jquery.scrollTo');
F
Fatih Acet 已提交
7

8
(function () {
9 10
  // TODO: remove this hack!
  // PhantomJS causes spyOn to panic because replaceState isn't "writable"
11 12 13 14 15 16
  var phantomjs;
  try {
    phantomjs = !Object.getOwnPropertyDescriptor(window.history, 'replaceState').writable;
  } catch (err) {
    phantomjs = false;
  }
17

18 19 20 21
  describe('MergeRequestTabs', function () {
    var stubLocation = {};
    var setLocation = function (stubs) {
      var defaults = {
F
Fatih Acet 已提交
22 23 24 25
        pathname: '',
        search: '',
        hash: ''
      };
26
      $.extend(stubLocation, defaults, stubs || {});
F
Fatih Acet 已提交
27
    };
28
    preloadFixtures('merge_requests/merge_request_with_task_list.html.raw');
29 30 31 32 33

    beforeEach(function () {
      this.class = new gl.MergeRequestTabs({ stubLocation: stubLocation });
      setLocation();

34 35 36 37 38
      if (!phantomjs) {
        this.spies = {
          history: spyOn(window.history, 'replaceState').and.callFake(function () {})
        };
      }
F
Fatih Acet 已提交
39
    });
40 41 42

    describe('#activateTab', function () {
      beforeEach(function () {
S
Steffen Rauh 已提交
43
        spyOn($, 'ajax').and.callFake(function () {});
44
        loadFixtures('merge_requests/merge_request_with_task_list.html.raw');
45
        this.subject = this.class.activateTab;
F
Fatih Acet 已提交
46
      });
47
      it('shows the first tab when action is show', function () {
F
Fatih Acet 已提交
48
        this.subject('show');
49
        expect($('#notes')).toHaveClass('active');
F
Fatih Acet 已提交
50
      });
51
      it('shows the notes tab when action is notes', function () {
F
Fatih Acet 已提交
52
        this.subject('notes');
53
        expect($('#notes')).toHaveClass('active');
F
Fatih Acet 已提交
54
      });
55
      it('shows the commits tab when action is commits', function () {
F
Fatih Acet 已提交
56
        this.subject('commits');
57
        expect($('#commits')).toHaveClass('active');
F
Fatih Acet 已提交
58
      });
59
      it('shows the diffs tab when action is diffs', function () {
F
Fatih Acet 已提交
60
        this.subject('diffs');
61
        expect($('#diffs')).toHaveClass('active');
F
Fatih Acet 已提交
62 63
      });
    });
64
    describe('#opensInNewTab', function () {
65
      var tabUrl;
66
      var windowTarget = '_blank';
67

68
      beforeEach(function () {
69 70 71
        loadFixtures('merge_requests/merge_request_with_task_list.html.raw');

        tabUrl = $('.commits-tab a').attr('href');
72 73

        spyOn($.fn, 'attr').and.returnValue(tabUrl);
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

      describe('meta click', () => {
        beforeEach(function () {
          spyOn(gl.utils, 'isMetaClick').and.returnValue(true);
        });

        it('opens page when commits link is clicked', function () {
          spyOn(window, 'open').and.callFake(function (url, name) {
            expect(url).toEqual(tabUrl);
            expect(name).toEqual(windowTarget);
          });

          this.class.bindEvents();
          document.querySelector('.merge-request-tabs .commits-tab a').click();
        });

        it('opens page when commits badge is clicked', function () {
          spyOn(window, 'open').and.callFake(function (url, name) {
            expect(url).toEqual(tabUrl);
            expect(name).toEqual(windowTarget);
          });

          this.class.bindEvents();
          document.querySelector('.merge-request-tabs .commits-tab a .badge').click();
        });
      });

102
      it('opens page tab in a new browser tab with Ctrl+Click - Windows/Linux', function () {
103 104
        spyOn(window, 'open').and.callFake(function (url, name) {
          expect(url).toEqual(tabUrl);
105
          expect(name).toEqual(windowTarget);
106
        });
107

108 109 110 111 112 113 114 115
        this.class.clickTab({
          metaKey: false,
          ctrlKey: true,
          which: 1,
          stopImmediatePropagation: function () {}
        });
      });
      it('opens page tab in a new browser tab with Cmd+Click - Mac', function () {
116 117
        spyOn(window, 'open').and.callFake(function (url, name) {
          expect(url).toEqual(tabUrl);
118
          expect(name).toEqual(windowTarget);
119 120
        });

121 122 123 124 125 126 127
        this.class.clickTab({
          metaKey: true,
          ctrlKey: false,
          which: 1,
          stopImmediatePropagation: function () {}
        });
      });
128
      it('opens page tab in a new browser tab with Middle-click - Mac/PC', function () {
129 130
        spyOn(window, 'open').and.callFake(function (url, name) {
          expect(url).toEqual(tabUrl);
131
          expect(name).toEqual(windowTarget);
132 133
        });

134 135 136 137 138 139 140
        this.class.clickTab({
          metaKey: false,
          ctrlKey: false,
          which: 2,
          stopImmediatePropagation: function () {}
        });
      });
141
    });
142 143 144

    describe('#setCurrentAction', function () {
      beforeEach(function () {
S
Steffen Rauh 已提交
145
        spyOn($, 'ajax').and.callFake(function () {});
146
        this.subject = this.class.setCurrentAction;
F
Fatih Acet 已提交
147
      });
148 149
      it('changes from commits', function () {
        setLocation({
F
Fatih Acet 已提交
150 151 152
          pathname: '/foo/bar/merge_requests/1/commits'
        });
        expect(this.subject('notes')).toBe('/foo/bar/merge_requests/1');
153
        expect(this.subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs');
F
Fatih Acet 已提交
154
      });
155 156
      it('changes from diffs', function () {
        setLocation({
F
Fatih Acet 已提交
157 158 159
          pathname: '/foo/bar/merge_requests/1/diffs'
        });
        expect(this.subject('notes')).toBe('/foo/bar/merge_requests/1');
160
        expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
F
Fatih Acet 已提交
161
      });
162 163
      it('changes from diffs.html', function () {
        setLocation({
F
Fatih Acet 已提交
164 165 166
          pathname: '/foo/bar/merge_requests/1/diffs.html'
        });
        expect(this.subject('notes')).toBe('/foo/bar/merge_requests/1');
167
        expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
F
Fatih Acet 已提交
168
      });
169 170
      it('changes from notes', function () {
        setLocation({
F
Fatih Acet 已提交
171 172 173
          pathname: '/foo/bar/merge_requests/1'
        });
        expect(this.subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs');
174
        expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
F
Fatih Acet 已提交
175
      });
176 177
      it('includes search parameters and hash string', function () {
        setLocation({
F
Fatih Acet 已提交
178 179 180 181
          pathname: '/foo/bar/merge_requests/1/diffs',
          search: '?view=parallel',
          hash: '#L15-35'
        });
182
        expect(this.subject('show')).toBe('/foo/bar/merge_requests/1?view=parallel#L15-35');
F
Fatih Acet 已提交
183
      });
184 185 186
      it('replaces the current history state', function () {
        var newState;
        setLocation({
F
Fatih Acet 已提交
187 188
          pathname: '/foo/bar/merge_requests/1'
        });
189
        newState = this.subject('commits');
190 191 192 193 194
        if (!phantomjs) {
          expect(this.spies.history).toHaveBeenCalledWith({
            url: newState
          }, document.title, newState);
        }
F
Fatih Acet 已提交
195
      });
196 197
      it('treats "show" like "notes"', function () {
        setLocation({
F
Fatih Acet 已提交
198 199
          pathname: '/foo/bar/merge_requests/1/commits'
        });
200
        expect(this.subject('show')).toBe('/foo/bar/merge_requests/1');
F
Fatih Acet 已提交
201 202
      });
    });
S
Steffen Rauh 已提交
203 204 205
    describe('#loadDiff', function () {
      it('requires an absolute pathname', function () {
        spyOn($, 'ajax').and.callFake(function (options) {
206 207 208 209 210
          expect(options.url).toEqual('/foo/bar/merge_requests/1/diffs.json');
        });
        this.class.loadDiff('/foo/bar/merge_requests/1/diffs');
      });
    });
F
Fatih Acet 已提交
211
  });
212
}).call(window);