aboutsummaryrefslogtreecommitdiffstats
path: root/test/service/xhr.cacheSpec.js
blob: f4654cd4f982576e2a750f8cb5b1ca4fe059db3d (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';

describe('$xhr.cache', function() {
  var scope, $browser, $browserXhr, $xhrErr, cache, log;

  beforeEach(function() {
    scope = angular.scope({}, null, {'$xhr.error': $xhrErr = jasmine.createSpy('$xhr.error')});
    $browser = scope.$service('$browser');
    $browserXhr = $browser.xhr;
    cache = scope.$service('$xhr.cache');
    log = '';
  });


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


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


  it('should cache requests', function(){
    $browserXhr.expectGET('/url').respond('first');
    cache('GET', '/url', null, callback);
    $browserXhr.flush();

    $browserXhr.expectGET('/url').respond('ERROR');
    cache('GET', '/url', null, callback);
    $browser.defer.flush();
    expect(log).toEqual('"first";"first";');

    cache('GET', '/url', null, callback, false);
    $browser.defer.flush();
    expect(log).toEqual('"first";"first";"first";');
  });


  it('should first return cache request, then return server request', function(){
    $browserXhr.expectGET('/url').respond('first');
    cache('GET', '/url', null, callback, true);
    $browserXhr.flush();

    $browserXhr.expectGET('/url').respond('ERROR');
    cache('GET', '/url', null, callback, true);
    $browser.defer.flush();
    expect(log).toEqual('"first";"first";');

    $browserXhr.flush();
    expect(log).toEqual('"first";"first";"ERROR";');
  });


  it('should serve requests from cache', function(){
    cache.data.url = {value:'123'};
    cache('GET', 'url', null, callback);
    $browser.defer.flush();
    expect(log).toEqual('"123";');

    cache('GET', 'url', null, callback, false);
    $browser.defer.flush();
    expect(log).toEqual('"123";"123";');
  });


  it('should keep track of in flight requests and request only once', function(){
    scope.$service('$xhr.bulk').urls['/bulk'] = {
      match:function(url){
        return url == '/url';
      }
    };
    $browserXhr.expectPOST('/bulk', {
      requests:[{method:'GET',  url:'/url', data: null}]
    }).respond([
      {status:200, response:'123'}
    ]);
    cache('GET', '/url', null, callback);
    cache('GET', '/url', null, callback);
    cache.delegate.flush();
    $browserXhr.flush();
    expect(log).toEqual('"123";"123";');
  });


  it('should clear cache on non GET', function(){
    $browserXhr.expectPOST('abc', {}).respond({});
    cache.data.url = {value:123};
    cache('POST', 'abc', {});
    expect(cache.data.url).toBeUndefined();
  });


  it('should call callback asynchronously for both cache hit and cache miss', function() {
    $browserXhr.expectGET('/url').respond('+');
    cache('GET', '/url', null, callback);
    expect(log).toEqual(''); //callback hasn't executed

    $browserXhr.flush();
    expect(log).toEqual('"+";'); //callback has executed

    cache('GET', '/url', null, callback);
    expect(log).toEqual('"+";'); //callback hasn't executed

    $browser.defer.flush();
    expect(log).toEqual('"+";"+";'); //callback has executed
  });


  it('should call callback synchronously when sync flag is on', function() {
    $browserXhr.expectGET('/url').respond('+');
    cache('GET', '/url', null, callback, false, true);
    expect(log).toEqual(''); //callback hasn't executed

    $browserXhr.flush();
    expect(log).toEqual('"+";'); //callback has executed

    cache('GET', '/url', null, callback, false, true);
    expect(log).toEqual('"+";"+";'); //callback has executed

    $browser.defer.flush();
    expect(log).toEqual('"+";"+";'); //callback was not called again any more
  });


  it('should call eval after callbacks for both cache hit and cache miss execute', function() {
    var evalSpy = this.spyOn(scope, '$eval').andCallThrough();

    $browserXhr.expectGET('/url').respond('+');
    cache('GET', '/url', null, callback);
    expect(evalSpy).not.toHaveBeenCalled();

    $browserXhr.flush();
    expect(evalSpy).toHaveBeenCalled();

    evalSpy.reset(); //reset the spy

    cache('GET', '/url', null, callback);
    expect(evalSpy).not.toHaveBeenCalled();

    $browser.defer.flush();
    expect(evalSpy).toHaveBeenCalled();
  });

  it('should call the error callback on error if provided', function() {
    var errorSpy = jasmine.createSpy('error'),
        successSpy = jasmine.createSpy('success');

    $browserXhr.expectGET('/url').respond(500, 'error');

    cache('GET', '/url', null, successSpy, errorSpy, false, true);
    $browserXhr.flush();
    expect(errorSpy).toHaveBeenCalledWith(500, 'error');
    expect(successSpy).not.toHaveBeenCalled();

    errorSpy.reset();
    cache('GET', '/url', successSpy, errorSpy, false, true);
    $browserXhr.flush();
    expect(errorSpy).toHaveBeenCalledWith(500, 'error');
    expect(successSpy).not.toHaveBeenCalled();
  });

  it('should call the $xhr.error on error if error callback not provided', function() {
    var errorSpy = jasmine.createSpy('error'),
        successSpy = jasmine.createSpy('success');

    $browserXhr.expectGET('/url').respond(500, 'error');
    cache('GET', '/url', null, successSpy, false, true);
    $browserXhr.flush();

    expect(successSpy).not.toHaveBeenCalled();
    expect($xhrErr).toHaveBeenCalledWith(
      {method: 'GET', url: '/url', data: null, success: successSpy},
      {status: 500, body: 'error'});
  });
});