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

describe('$xhr.bulk', function() {
  var scope, $browser, $browserXhr, $log, $xhrBulk, $xhrError, log;

  beforeEach(function(){
    scope = angular.scope(angular.service, {
      '$xhr.error': $xhrError = jasmine.createSpy('$xhr.error'),
      '$log': $log = {}
    });
    $browser = scope.$service('$browser');
    $browserXhr = $browser.xhr;
    $xhrBulk = scope.$service('$xhr.bulk');
    $log = scope.$service('$log');
    log = '';
  });


  afterEach(function(){
    dealoc(scope);
  });


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


  it('should collect requests', function(){
    $xhrBulk.urls["/"] = {match:/.*/};
    $xhrBulk('GET', '/req1', null, callback);
    $xhrBulk('POST', '/req2', {post:'data'}, callback);

    $browserXhr.expectPOST('/', {
      requests:[{method:'GET',  url:'/req1', data: null},
                {method:'POST', url:'/req2', data:{post:'data'} }]
    }).respond([
      {status:200, response:'first'},
      {status:200, response:'second'}
    ]);
    $xhrBulk.flush(function(){ log += 'DONE';});
    $browserXhr.flush();
    expect(log).toEqual('"first";"second";DONE');
  });


  it('should handle non 200 status code by forwarding to error handler', function(){
    $xhrBulk.urls['/'] = {match:/.*/};
    $xhrBulk('GET', '/req1', null, callback);
    $xhrBulk('POST', '/req2', {post:'data'}, callback);

    $browserXhr.expectPOST('/', {
      requests:[{method:'GET',  url:'/req1', data: null},
                {method:'POST', url:'/req2', data:{post:'data'} }]
    }).respond([
      {status:404, response:'NotFound'},
      {status:200, response:'second'}
    ]);
    $xhrBulk.flush(function(){ log += 'DONE';});
    $browserXhr.flush();

    expect($xhrError).toHaveBeenCalled();
    var cb = $xhrError.mostRecentCall.args[0].success;
    expect(typeof cb).toEqual('function');
    expect($xhrError).toHaveBeenCalledWith(
        {url: '/req1', method: 'GET', data: null, success: cb},
        {status: 404, response: 'NotFound'});

    expect(log).toEqual('"second";DONE');
  });

  it('should handle non 200 status code by calling error callback if provided', function() {
    var callback = jasmine.createSpy('error');

    $xhrBulk.urls['/'] = {match: /.*/};
    $xhrBulk('GET', '/req1', null, noop, callback);

    $browserXhr.expectPOST('/', {
      requests:[{method: 'GET',  url: '/req1', data: null}]
    }).respond([{status: 404, response: 'NotFound'}]);

    $xhrBulk.flush();
    $browserXhr.flush();

    expect($xhrError).not.toHaveBeenCalled();
    expect(callback).toHaveBeenCalledWith(404, 'NotFound');
  });
});