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

/*= require merge_request_tabs */
P
Phil Hughes 已提交
4
//= require breakpoints
5 6
//= require lib/utils/common_utils
//= require jquery.scrollTo
F
Fatih Acet 已提交
7

8 9 10 11 12
(function () {
  describe('MergeRequestTabs', function () {
    var stubLocation = {};
    var setLocation = function (stubs) {
      var defaults = {
F
Fatih Acet 已提交
13 14 15 16
        pathname: '',
        search: '',
        hash: ''
      };
17
      $.extend(stubLocation, defaults, stubs || {});
F
Fatih Acet 已提交
18
    };
19
    preloadFixtures('static/merge_request_tabs.html.raw');
20 21 22 23 24 25 26

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

      this.spies = {
        history: spyOn(window.history, 'replaceState').and.callFake(function () {})
F
Fatih Acet 已提交
27 28
      };
    });
29 30 31

    describe('#activateTab', function () {
      beforeEach(function () {
S
Steffen Rauh 已提交
32
        spyOn($, 'ajax').and.callFake(function () {});
33
        loadFixtures('static/merge_request_tabs.html.raw');
34
        this.subject = this.class.activateTab;
F
Fatih Acet 已提交
35
      });
36
      it('shows the first tab when action is show', function () {
F
Fatih Acet 已提交
37
        this.subject('show');
38
        expect($('#notes')).toHaveClass('active');
F
Fatih Acet 已提交
39
      });
40
      it('shows the notes tab when action is notes', function () {
F
Fatih Acet 已提交
41
        this.subject('notes');
42
        expect($('#notes')).toHaveClass('active');
F
Fatih Acet 已提交
43
      });
44
      it('shows the commits tab when action is commits', function () {
F
Fatih Acet 已提交
45
        this.subject('commits');
46
        expect($('#commits')).toHaveClass('active');
F
Fatih Acet 已提交
47
      });
48
      it('shows the diffs tab when action is diffs', function () {
F
Fatih Acet 已提交
49
        this.subject('diffs');
50
        expect($('#diffs')).toHaveClass('active');
F
Fatih Acet 已提交
51 52
      });
    });
53 54 55

    describe('#setCurrentAction', function () {
      beforeEach(function () {
S
Steffen Rauh 已提交
56
        spyOn($, 'ajax').and.callFake(function () {});
57
        this.subject = this.class.setCurrentAction;
F
Fatih Acet 已提交
58
      });
59 60
      it('changes from commits', function () {
        setLocation({
F
Fatih Acet 已提交
61 62 63
          pathname: '/foo/bar/merge_requests/1/commits'
        });
        expect(this.subject('notes')).toBe('/foo/bar/merge_requests/1');
64
        expect(this.subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs');
F
Fatih Acet 已提交
65
      });
66 67
      it('changes from diffs', function () {
        setLocation({
F
Fatih Acet 已提交
68 69 70
          pathname: '/foo/bar/merge_requests/1/diffs'
        });
        expect(this.subject('notes')).toBe('/foo/bar/merge_requests/1');
71
        expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
F
Fatih Acet 已提交
72
      });
73 74
      it('changes from diffs.html', function () {
        setLocation({
F
Fatih Acet 已提交
75 76 77
          pathname: '/foo/bar/merge_requests/1/diffs.html'
        });
        expect(this.subject('notes')).toBe('/foo/bar/merge_requests/1');
78
        expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
F
Fatih Acet 已提交
79
      });
80 81
      it('changes from notes', function () {
        setLocation({
F
Fatih Acet 已提交
82 83 84
          pathname: '/foo/bar/merge_requests/1'
        });
        expect(this.subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs');
85
        expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
F
Fatih Acet 已提交
86
      });
87 88
      it('includes search parameters and hash string', function () {
        setLocation({
F
Fatih Acet 已提交
89 90 91 92
          pathname: '/foo/bar/merge_requests/1/diffs',
          search: '?view=parallel',
          hash: '#L15-35'
        });
93
        expect(this.subject('show')).toBe('/foo/bar/merge_requests/1?view=parallel#L15-35');
F
Fatih Acet 已提交
94
      });
95 96 97
      it('replaces the current history state', function () {
        var newState;
        setLocation({
F
Fatih Acet 已提交
98 99
          pathname: '/foo/bar/merge_requests/1'
        });
100 101
        newState = this.subject('commits');
        expect(this.spies.history).toHaveBeenCalledWith({
F
Fatih Acet 已提交
102
          turbolinks: true,
103 104
          url: newState
        }, document.title, newState);
F
Fatih Acet 已提交
105
      });
106 107
      it('treats "show" like "notes"', function () {
        setLocation({
F
Fatih Acet 已提交
108 109
          pathname: '/foo/bar/merge_requests/1/commits'
        });
110
        expect(this.subject('show')).toBe('/foo/bar/merge_requests/1');
F
Fatih Acet 已提交
111 112
      });
    });
S
Steffen Rauh 已提交
113 114 115
    describe('#loadDiff', function () {
      it('requires an absolute pathname', function () {
        spyOn($, 'ajax').and.callFake(function (options) {
116 117 118 119 120
          expect(options.url).toEqual('/foo/bar/merge_requests/1/diffs.json');
        });
        this.class.loadDiff('/foo/bar/merge_requests/1/diffs');
      });
    });
F
Fatih Acet 已提交
121 122
  });
}).call(this);