forked from platanus/angular-restmod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_api_spec.js
More file actions
159 lines (123 loc) · 4.49 KB
/
Copy pathcommon_api_spec.js
File metadata and controls
159 lines (123 loc) · 4.49 KB
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
'use strict';
describe('Restmod model class:', function() {
var $httpBackend, $restmod, $rootScope, Bike, query;
beforeEach(module('plRestmod'));
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
$restmod = $injector.get('$restmod');
$rootScope = $injector.get('$rootScope');
Bike = $restmod.model('/api/bikes');
query = Bike.$collection();
}));
describe('$send', function() {
beforeEach(function() {
$httpBackend.when('GET','/api/bikes/1').respond(200, { last: false });
$httpBackend.when('GET','/api/bikes/2').respond(200, { last: true });
$httpBackend.when('GET','/api/bikes/3').respond(404);
});
it('should execute request in FIFO order', function() {
var bike = Bike.$new();
bike.$send({ method: 'GET', url: '/api/bikes/1' });
bike.$send({ method: 'GET', url: '/api/bikes/2' });
$httpBackend.flush();
expect(bike.$response.data.last).toEqual(true);
});
it('should properly update the $status property', function() {
var bike = Bike.$new();
expect(bike.$status).toBeUndefined();
bike.$send({ method: 'GET', url: '/api/bikes/1' });
expect(bike.$status).toEqual('pending');
$httpBackend.flush();
expect(bike.$status).toEqual('ok');
bike.$send({ method: 'GET', url: '/api/bikes/3' });
$rootScope.$digest(); // force digest, since this is a second request.
expect(bike.$status).toEqual('pending');
$httpBackend.flush();
expect(bike.$status).toEqual('error');
});
it('should properly update the $pending property', function() {
var bike = Bike.$new();
expect(bike.$pending).toBeUndefined();
bike.$send({ method: 'GET', url: '/api/bikes/1' });
bike.$send({ method: 'GET', url: '/api/bikes/2' });
expect(bike.$pending.length).toEqual(2);
$httpBackend.flush();
expect(bike.$pending.length).toEqual(0);
});
});
describe('$hasPendingRequests', function() {
it('should return true if there are pending requests', function() {
var bike = Bike.$new();
expect(bike.$hasPendingRequests()).toEqual(false);
bike.$send({ method: 'GET', url: '/api/bikes/1' });
expect(bike.$hasPendingRequests()).toEqual(true);
$httpBackend.when('GET','/api/bikes/1').respond(200, {});
$httpBackend.flush();
expect(bike.$hasPendingRequests()).toEqual(false);
});
});
describe('$cancel', function() {
it('should cancel every pending request', function() {
var bike = Bike.$new();
bike.$send({ method: 'GET', url: '/api/bikes/1' });
bike.$send({ method: 'GET', url: '/api/bikes/6' });
expect(bike.$hasPendingRequests()).toEqual(true);
bike.$cancel();
expect(bike.$hasPendingRequests()).toEqual(false);
});
});
describe('$then', function() {
// TODO!
});
describe('$finally', function() {
it('should be called on success', function() {
var spy = jasmine.createSpy('callback');
Bike.$find(1).$finally(spy);
$httpBackend.when('GET','/api/bikes/1').respond(200, {});
$httpBackend.flush();
expect(spy).toHaveBeenCalledWith();
});
it('should be called on error', function() {
var spy = jasmine.createSpy('callback');
Bike.$find(1).$finally(spy);
$httpBackend.when('GET','/api/bikes/1').respond(404);
$httpBackend.flush();
expect(spy).toHaveBeenCalledWith();
});
});
describe('$on', function() {
it('should register a callback at instance level', function() {
var bike1 = Bike.$build(),
bike2 = Bike.$build(),
spy = jasmine.createSpy('callback');
bike1.$on('poke', spy);
bike2.$dispatch('poke');
expect(spy).not.toHaveBeenCalled();
bike1.$dispatch('poke');
expect(spy).toHaveBeenCalled();
});
});
describe('$dispatch', function() {
// TODO!
});
describe('$decorate', function() {
it('should register a callback at decorated context level', function() {
var bike = Bike.$build(), arg;
bike.$decorate({
poke: function(_arg) { arg = _arg; }
}, function() {
bike.$dispatch('poke', [1]);
});
expect(arg).toEqual(1);
bike.$dispatch('poke', [2]);
expect(arg).toEqual(1);
});
it('should return the decorated function return value', function() {
var bike = Bike.$build();
var ret = bike.$decorate({ }, function() {
return 'hello';
});
expect(ret).toEqual('hello');
});
});
});