aboutsummaryrefslogtreecommitdiffstats
path: root/test/service/xhr.errorSpec.js
blob: f9ce2b72e72985e68f9a4cea041860150879bd4e (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
'use strict';

describe('$xhr.error', function() {
  var log;

  beforeEach(inject(function($provide) {
    $provide.value('$xhr.error', jasmine.createSpy('$xhr.error'));
    $provide.factory('$xhrError', ['$xhr.error', identity]);
    log = '';
  }));


  function callback(code, response) {
    expect(code).toEqual(200);
    log = log + toJson(response) + ';';
  }


  it('should handle non 200 status codes by forwarding to error handler', inject(function($browser, $xhr, $xhrError) {
    $browser.xhr.expectPOST('/req', 'MyData').respond(500, 'MyError');
    $xhr('POST', '/req', 'MyData', callback);
    $browser.xhr.flush();
    var cb = $xhrError.mostRecentCall.args[0].success;
    expect(typeof cb).toEqual('function');
    expect($xhrError).toHaveBeenCalledWith(
        {url: '/req', method: 'POST', data: 'MyData', success: cb},
        {status: 500, body: 'MyError'});
  }));
});