summaryrefslogtreecommitdiff
path: root/web-ui/test/spec/helpers/monitored_ajax_call.spec.js
blob: 4c48d32081137f6dd0f0f1a4365f084454fc9bd4 (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
define(['helpers/monitored_ajax'], function (monitoredAjax) {
  'use strict';
  describe('monitoredAjaxCall', function () {

    describe('default configs', function () {

     it('should always attach the xsrf token in the header', function () {
       var component = { trigger: function () {}};
       var d = $.Deferred();
       spyOn($, 'ajax').and.returnValue(d);
       document.cookie = 'XSRF-TOKEN=ff895ffc45a4ce140bfc5dda6c61d232; i18next=en-us';
       var anyUrl = '/';

       monitoredAjax(component, anyUrl, {});

       expect($.ajax.calls.mostRecent().args[1].headers).toEqual({ 'X-XSRF-TOKEN' : 'ff895ffc45a4ce140bfc5dda6c61d232' });

     });

     });

    describe('when dealing with errors', function () {

      _.each(
        {
         timeout: 'A timeout occurred',
         error: 'Problems talking to server',
         parseerror: 'Got invalid response from server'
        }, function (errorMessage, errorType) {
        it('shows message for a server ' + errorType, function () {
          var component = { trigger: function () {}};
          spyOn(component, 'trigger');
          var d = $.Deferred();
          spyOn($, 'ajax').and.returnValue(d);

          monitoredAjax(component, '/', {});
          d.reject({ responseJSON: {}}, errorType, '');

          expect(component.trigger).toHaveBeenCalledWith(document, Pixelated.events.ui.userAlerts.displayMessage,
              { message: errorMessage, class: 'error' });
        });
      });

      it('shows the error message sent by the server if it exists', function () {
        var component = { trigger: function () {}};
        spyOn(component, 'trigger');
        var d = $.Deferred();
        spyOn($, 'ajax').and.returnValue(d);

        monitoredAjax(component, '/', {});
        d.reject({ responseJSON: { message: 'Server Message'}}, 'error', '');

        expect(component.trigger).toHaveBeenCalledWith(document, Pixelated.events.ui.userAlerts.displayMessage,
            { message: 'Server Message', class: 'error' });
      });
    });

    describe('when user seems to be logged out', function () {
      var component, browser;

      beforeEach(function () {
        component = { trigger: function () {}};
        browser = require('helpers/browser');

      });

      it('will redirect the browser to the location specified', function () {
        var redirectUrl = '/some/redirect/url';
        var deferred = $.Deferred();
        spyOn($, 'ajax').and.returnValue(deferred);
        var spyRedirect = spyOn(browser, 'redirect').and.returnValue($.Deferred());

        monitoredAjax(component, '/some/url', {});

        deferred.reject({status: 302, getResponseHeader: function (_) {return redirectUrl;}}, '', '');

        expect(spyRedirect).toHaveBeenCalled();
        expect(spyRedirect.calls.mostRecent().args[0]).toEqual(redirectUrl);
      });

      it ('will redirect the browser to root if authentication is required', function () {
        var redirectUrl = '/';
        var deferred = $.Deferred();
        spyOn($, 'ajax').and.returnValue(deferred);
        var spyRedirect = spyOn(browser, 'redirect').and.returnValue($.Deferred());

        monitoredAjax(component, '/some/url', {});

        deferred.reject({status: 401}, '', '');

        expect(spyRedirect).toHaveBeenCalled();
        expect(spyRedirect.calls.mostRecent().args[0]).toEqual(redirectUrl);
      });

    });
  });
});