-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathangular-hyper-resource.spec.js
More file actions
319 lines (243 loc) · 7.9 KB
/
Copy pathangular-hyper-resource.spec.js
File metadata and controls
319 lines (243 loc) · 7.9 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
describe('The hal resource', function() {
beforeEach(module("hyperResource"));
var hResource, $httpBackend;
beforeEach(inject(function($injector) {
hResource = $injector.get('hResource');
$httpBackend = $injector.get('$httpBackend');
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("is very similar to the ngResource app's $resource service", function() {
var className = 'pet';
var Pet = hResource(className, '/pets/:id', {id: '@id'});
var fluffy = new Pet();
fluffy.species = 'cat';
var someId = 1234;
fluffy.$save(function(data){
fluffy.id = data.id;
});
$httpBackend.expectPOST('/pets').respond(200, {id: someId});
$httpBackend.flush();
expect(fluffy.id).toBe(someId);
fluffy.$delete();
$httpBackend.expectDELETE('/pets/' + someId).respond(200);
$httpBackend.flush();
});
describe('provides some abstractions for Hypertext Application Language:', function() {
var Book;
beforeEach(function() {
Book = hResource('book', '/books/:id', {id: '@id'});
});
it("resource instances have an extra `$rel` method", function() {
var book = new Book();
expect(book.$rel).toBeDefined();
});
it('the `$rel` method will follow HAL links', function() {
var title = 'Anna Karenina';
var annaKareninaData = {
id: 1,
title: title,
_links: {
next: {
href: '/books/2',
type: 'book' // used to resolve instance type
}
}
};
$httpBackend.expectGET('/books?title=Anna+Karenina').respond(200, annaKareninaData);
var book = Book.get({title: title});
$httpBackend.flush();
expect(book.id).toBe(1);
var theGoodPartsData = {
id: 2,
title: 'Javascript: The Good Parts',
author: 'Douglas Crockford'
};
$httpBackend.expectGET('/books/2').respond(200, theGoodPartsData);
book.$rel('next');
$httpBackend.flush();
});
it('the `$rel` method will also grab embedded resources', function() {
var Person = hResource('person', '/person/:id', {id: '@id'});
Person.prototype.name = function() {
return this.firstName + ' ' + this.lastName;
};
var bookData = {
title: 'Code Complete',
edition: 2,
_embedded: {
author: {
_links: {
self: {
href: '/authors/1',
type: 'person',
}
},
id: 1,
firstName: 'Steve',
lastName: 'McConnel'
}
}
};
var book = new Book(bookData);
var author = book.$rel('author');
author.then(function(author){
expect(author instanceof Person).toBe(true);
expect(author.name()).toBe('Steve McConnel');
});
});
var bookData = {
title: 'The Design of Everyday Things',
_embedded: {
related: [
{
title: "Don't make me think",
},
{
_links: {self: {name: "reference"}},
title: "The Elements of Typographic Style",
}
]
}
};
it('the `$rel` method will return an array of related resources if there are more than one links', function() {
var book = new Book(bookData);
book.$rel('related').then(function(result) {
expect(result.length).toBe(2);
expect(result[0] instanceof Book).toBe(false); // because there is no self link!
expect(result[1].title).toBe("The Elements of Typographic Style");
})
});
it("resource instances alse have an extra `$if` method", function() {
var book = new Book();
expect(book.$if).toBeDefined();
});
it("the `$if` method returns the number of related resource", function() {
var book = new Book(bookData);
expect(book.$if).toBeDefined();
expect(book.$if('related')).toBe(2);
expect(book.$if('related', 'reference')).toBe(1);
expect(book.$if('related', 'journals')).toBe(0);
});
});
var bookData = {
title: 'Transport Phenomena',
_links: {
self: {
href: '/books/0',
},
next: {
href: '/books/1',
title: 'Introduction to Fourier Optics',
}
},
_embedded: {
chapter: [
{
title: 'Viscosity and the Mechanism of Momentum Transport',
_links: {
self: {
href: '/books/0/chapter/1',
type: 'chapter',
}
},
_embedded: {
section: [
{title: "Newton's Law of Viscosity"},
{title: "Generalized Newton's Law"},
{title: "Pressure and Temperture Dependence"}
]
}
},
{
title: 'Shell Momentum Balances',
_links: {
self: {
href: '/books/0/chapter/2',
type: 'chapter',
}
},
}
]
}
};
describe("doesnt' require a declared resource type", function() {
var Book;
beforeEach(function() {
Book = hResource('/books/:id', {id: '@id'});
});
it('even so we still get the extra methods (`$rel`, `$if`, etc.).', function() {
var book = new Book(bookData);
expect(book instanceof Book).toBe(true);
book.$rel('chapter').then(function(chapters){
// because we don't register they type name, there is no way for the
// hyper resource to know what type it should be
expect(chapters[0] instanceof Book).toBe(false);
// but it will still have the `$rel` and `$if` methods because they are
// inhereted from the `HyperObject`
expect(chapters[0].$if('section')).toBe(3);
expect(chapters[1].$if('section')).toBe(0);
chapters[0].$rel('section').then(function(sections) {
expect(sections[0].title).toBe("Newton's Law of Viscosity");
});
});
});
});
it("the `$rel` method can't be called from an unresolved resource.", function() {
Book = hResource('/books/:id', {id: '@id'});
$httpBackend.expectGET('/books/1').respond(200, bookData);
var book = Book.get({id: 1});
function getChapters(){
book.$rel('chapter');
}
expect(getChapters).toThrow();
book.$promise.then(function(){
expect(getChapters).not.toThrow()
});
$httpBackend.flush();
});
it("the `$if` method returns `0` from unresolved resource.", function() {
var Book = hResource('/books/:id', {id: '@id'});
$httpBackend.expectGET('/books/1').respond(200, bookData);
var book = Book.get({id: 1});
function getChapterCount(){
book.$if('chapter');
}
expect(getChapterCount).not.toThrow();
book.$promise.then(function(){
expect(getChapterCount).not.toThrow()
expect(book.$if('chapter')).toBe(2);
});
$httpBackend.flush();
});
it('provide a `$link` and `$links` methods', function() {
var Book = hResource('/books/:id', {id: '@id'});
var book = new Book(bookData);
expect(book.$link('nonexistent')).toBe(undefined);
expect(book.$link('self').href).toBe('/books/0');
expect(book.$link('next').href).toBe('/books/1');
expect(book.$link('next').title).toBe('Introduction to Fourier Optics');
function getChapterLink() {
return book.$link('chapter');
}
function getChapterLinks() {
return book.$links('chapter');
}
expect(getChapterLink).toThrow();
expect(getChapterLinks).not.toThrow();
expect(angular.isArray(book.$links('chapter'))).toBe(true);
var expectedChapterLinks = [
{
href: '/books/0/chapter/1',
type: 'chapter',
},
{
href: '/books/0/chapter/2',
type: 'chapter',
}
];
expect(getChapterLinks()).toEqual(expectedChapterLinks);
});
});