merge_request_widget_spec.js 3.94 KB
Newer Older
1
/* eslint-disable */
Fatih Acet's avatar
Fatih Acet committed
2
/*= require merge_request_widget */
3
/*= require jquery.timeago.js */
Fatih Acet's avatar
Fatih Acet committed
4 5 6 7 8 9 10 11

(function() {
  describe('MergeRequestWidget', function() {
    beforeEach(function() {
      window.notifyPermissions = function() {};
      window.notify = function() {};
      this.opts = {
        ci_status_url: "http://sampledomain.local/ci/getstatus",
12
        ci_environments_status_url: "http://sampledomain.local/ci/getenvironmentsstatus",
Fatih Acet's avatar
Fatih Acet committed
13 14 15 16 17 18 19 20 21 22 23 24
        ci_status: "",
        ci_message: {
          normal: "Build {{status}} for \"{{title}}\"",
          preparing: "{{status}} build for \"{{title}}\""
        },
        ci_title: {
          preparing: "{{status}} build",
          normal: "Build {{status}}"
        },
        gitlab_icon: "gitlab_logo.png",
        builds_path: "http://sampledomain.local/sampleBuildsPath"
      };
25
      this["class"] = new window.gl.MergeRequestWidget(this.opts);
Fatih Acet's avatar
Fatih Acet committed
26
    });
27 28 29

    describe('getCIEnvironmentsStatus', function() {
      beforeEach(function() {
30
        this.ciEnvironmentsStatusData = [{
31 32 33 34 35
          created_at: '2016-09-12T13:38:30.636Z',
          environment_id: 1,
          environment_name: 'env1',
          external_url: 'https://test-url.com',
          external_url_formatted: 'test-url.com'
36
        }];
37 38 39 40 41 42 43 44 45 46 47 48 49

        spyOn(jQuery, 'getJSON').and.callFake((req, cb) => {
          cb(this.ciEnvironmentsStatusData);
        });
      });

      it('should call renderEnvironments when the environments property is set', function() {
         const spy = spyOn(this.class, 'renderEnvironments').and.stub();
         this.class.getCIEnvironmentsStatus();
         expect(spy).toHaveBeenCalledWith(this.ciEnvironmentsStatusData);
       });

       it('should not call renderEnvironments when the environments property is not set', function() {
50
         this.ciEnvironmentsStatusData = null;
51 52 53 54 55 56
         const spy = spyOn(this.class, 'renderEnvironments').and.stub();
         this.class.getCIEnvironmentsStatus();
         expect(spy).not.toHaveBeenCalled();
       });
    });

Fatih Acet's avatar
Fatih Acet committed
57 58
    return describe('getCIStatus', function() {
      beforeEach(function() {
59 60 61 62 63 64 65
        this.ciStatusData = {
          "title": "Sample MR title",
          "sha": "12a34bc5",
          "status": "success",
          "coverage": 98
        };

66
        spyOn(jQuery, 'getJSON').and.callFake((function(_this) {
Fatih Acet's avatar
Fatih Acet committed
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
          return function(req, cb) {
            return cb(_this.ciStatusData);
          };
        })(this));
      });
      it('should call showCIStatus even if a notification should not be displayed', function() {
        var spy;
        spy = spyOn(this["class"], 'showCIStatus').and.stub();
        this["class"].getCIStatus(false);
        return expect(spy).toHaveBeenCalledWith(this.ciStatusData.status);
      });
      it('should call showCIStatus when a notification should be displayed', function() {
        var spy;
        spy = spyOn(this["class"], 'showCIStatus').and.stub();
        this["class"].getCIStatus(true);
        return expect(spy).toHaveBeenCalledWith(this.ciStatusData.status);
      });
      it('should call showCICoverage when the coverage rate is set', function() {
        var spy;
        spy = spyOn(this["class"], 'showCICoverage').and.stub();
        this["class"].getCIStatus(false);
        return expect(spy).toHaveBeenCalledWith(this.ciStatusData.coverage);
      });
      it('should not call showCICoverage when the coverage rate is not set', function() {
        var spy;
        this.ciStatusData.coverage = null;
        spy = spyOn(this["class"], 'showCICoverage').and.stub();
        this["class"].getCIStatus(false);
        return expect(spy).not.toHaveBeenCalled();
      });
97
      it('should not display a notification on the first check after the widget has been created', function() {
Fatih Acet's avatar
Fatih Acet committed
98 99
        var spy;
        spy = spyOn(window, 'notify');
100
        this["class"] = new window.gl.MergeRequestWidget(this.opts);
Fatih Acet's avatar
Fatih Acet committed
101 102 103 104 105 106 107
        this["class"].getCIStatus(true);
        return expect(spy).not.toHaveBeenCalled();
      });
    });
  });

}).call(this);