forked from robsontenorio/vue-api-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.test.js
550 lines (392 loc) · 15.3 KB
/
model.test.js
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import Post from './dummy/models/Post'
import User from './dummy/models/User'
import Comment from './dummy/models/Comment'
import { Model } from '../src'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter';
import { Posts as postsResponse } from './dummy/data/posts'
import { Posts as postsEmbedResponse } from './dummy/data/postsEmbed'
import { Post as postResponse } from './dummy/data/post'
import { Post as postEmbedResponse } from './dummy/data/postEmbed'
import { Comments as commentsResponse } from './dummy/data/comments'
describe('Model methods', () => {
let errorModel = {}
Model.$http = axios
let axiosMock = new MockAdapter(axios)
beforeEach(() => {
axiosMock.reset()
Post.prototype['primaryKey'] = () => {
return 'id'
}
})
test('it throws a error when find() has no parameters', () => {
errorModel = () => {
const post = Post.find()
}
expect(errorModel).toThrow('You must specify the param on find() method.')
})
test('it throws a error when $find() has no parameters', () => {
errorModel = () => {
const post = Post.$find()
}
expect(errorModel).toThrow('You must specify the param on $find() method.')
})
test('first() returns first object in array as instance of such Model', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, {
data: postsResponse
})
const post = await Post.first()
expect(post).toEqual(postsResponse[0])
expect(post).toBeInstanceOf(Post)
})
test('$first() returns first object in array as instance of such Model', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, postsEmbedResponse)
const post = await Post.$first()
expect(post).toEqual(postsEmbedResponse.data[0])
expect(post).toBeInstanceOf(Post)
})
test('first() method returns a empty object when no items have found', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, [])
const post = await Post.first()
expect(post).toEqual({})
})
test('find() method returns a object as instance of such Model', async () => {
axiosMock.onGet('http://localhost/posts/1').reply(200, postResponse)
const post = await Post.find(1)
expect(post).toEqual(postResponse)
expect(post).toBeInstanceOf(Post)
})
test('$find() handles request with "data" wrapper', async () => {
axiosMock.onGet('http://localhost/posts/1').reply(200, postEmbedResponse)
const post = await Post.$find(1)
expect(post).toEqual(postEmbedResponse.data)
})
test('$find() handles request without "data" wrapper', async () => {
axiosMock.onGet('http://localhost/posts/1').reply(200, postResponse)
const post = await Post.$find(1)
expect(post).toEqual(postResponse)
})
test('get() method returns a array of objects as instance of suchModel', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, postsResponse)
const posts = await Post.get()
posts.forEach(post => {
expect(post).toBeInstanceOf(Post)
});
})
test('get() hits right resource (nested object)', async () => {
axiosMock.onGet().reply((config) => {
expect(config.method).toEqual('get')
expect(config.url).toEqual('http://localhost/posts/1/comments')
return [200, {}]
})
const post = new Post({ id: 1 })
await post.comments().get()
})
test('get() hits right resource (nested object, custom PK)', async () => {
Post.prototype['primaryKey'] = () => {
return 'someId'
}
let post = new Post({ id: 1, someId: 'po996-9dd18' })
axiosMock.onGet().reply((config) => {
expect(config.method).toEqual('get')
expect(config.url).toEqual(`http://localhost/posts/${post.someId}/comments`)
return [200, {}]
})
post.comments().get()
})
test('$get() fetch style request with "data" wrapper', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, postsEmbedResponse)
const posts = await Post.$get()
expect(posts).toEqual(postsEmbedResponse.data)
})
test('$get() fetch style request without "data" wrapper', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, postsEmbedResponse.data)
const posts = await Post.$get()
expect(posts).toEqual(postsEmbedResponse.data)
})
test('$get() hits right resource (nested object, custom PK)', async () => {
Post.prototype['primaryKey'] = () => {
return 'someId'
}
let post = new Post({ id: 1, someId: 'po996-9dd18' })
axiosMock.onGet().reply((config) => {
expect(config.method).toEqual('get')
expect(config.url).toEqual(`http://localhost/posts/${post.someId}/comments`)
return [200, {}]
})
post.comments().$get()
})
test('save() method makes a POST request when ID of object does not exists', async () => {
let post
axiosMock.onAny().reply((config) => {
expect(config.method).toEqual('post')
expect(config.data).toEqual(JSON.stringify(post))
expect(config.url).toEqual('http://localhost/posts')
return [200, {}]
})
post = new Post({ title: 'Cool!' })
await post.save()
})
test('save() method makes a PUT request when ID of object exists', async () => {
let post
axiosMock.onAny().reply((config) => {
expect(config.method).toEqual('put')
expect(config.data).toEqual(JSON.stringify(post))
expect(config.url).toEqual('http://localhost/posts/1')
return [200, {}]
})
post = new Post({ id: 1, title: 'Cool!' })
await post.save()
})
test('save() method makes a PUT request when ID of object exists (custom PK)', async () => {
Post.prototype['primaryKey'] = () => {
return 'someId'
}
let post = new Post({ id: 1, someId: 'xs911-8cf12', title: 'Cool!' })
axiosMock.onAny().reply((config) => {
expect(config.method).toEqual('put')
expect(config.url).toEqual(`http://localhost/posts/${post.someId}`)
expect(config.data).toEqual(JSON.stringify(post))
return [200, {}]
})
await post.save()
})
test('save() method makes a PUT request when ID of object exists (nested object)', async () => {
let comment
axiosMock.onGet('http://localhost/posts/1/comments').reply(200, commentsResponse)
axiosMock.onPut().reply((config) => {
expect(config.method).toEqual('put')
expect(config.data).toEqual(JSON.stringify(comment))
expect(config.url).toEqual('http://localhost/posts/1/comments/1')
return [200, {}]
})
const post = new Post({ id: 1 })
comment = await post.comments().first()
comment.text = 'Owh!'
comment.save()
})
test('save() method makes a PUT request when ID of object exists (nested object, customPK)', async () => {
let comment, post
Post.prototype['primaryKey'] = () => {
return 'someId'
}
post = new Post({ id: 1, someId: 'xs911-8cf12', title: 'Cool!' })
axiosMock.onGet(`http://localhost/posts/${post.someId}/comments`).reply(200, commentsResponse)
axiosMock.onPut().reply((config) => {
expect(config.method).toEqual('put')
expect(config.data).toEqual(JSON.stringify(comment))
expect(config.url).toEqual(`http://localhost/posts/${post.someId}/comments/1`)
return [200, {}]
})
comment = await post.comments().first()
comment.text = 'Owh!'
comment.save()
})
test('a request from delete() method hits the right resource', async () => {
axiosMock.onAny().reply((config) => {
expect(config.method).toEqual('delete')
expect(config.url).toBe('http://localhost/posts/1')
return [200, {}]
})
const post = new Post({ id: 1 })
post.delete()
})
test('a request from delete() method hits the right resource (custom PK)', async () => {
Post.prototype['primaryKey'] = () => {
return 'someId'
}
let post = new Post({ id: 1, someId: 'xs911-8cf12', title: 'Cool!' })
axiosMock.onAny().reply((config) => {
expect(config.method).toEqual('delete')
expect(config.url).toEqual(`http://localhost/posts/${post.someId}`)
return [200, {}]
})
await post.delete()
})
test('a request from delete() method when model has not ID throws a exception', async () => {
errorModel = () => {
let post = new Post()
post.delete()
}
expect(errorModel).toThrow('This model has a empty ID.')
})
test('a request from delete() method hits the right resource (nested object)', async () => {
axiosMock.onGet('http://localhost/posts/1/comments').reply(200, commentsResponse)
axiosMock.onDelete().reply((config) => {
expect(config.method).toEqual('delete')
expect(config.url).toBe('http://localhost/posts/1/comments/1')
return [200, {}]
})
const post = new Post({ id: 1 })
const comment = await post.comments().first()
comment.delete()
})
test('a request from delete() method hits the right resource (nested object) (nested object, customPK)', async () => {
let comment, post
Post.prototype['primaryKey'] = () => {
return 'someId'
}
post = new Post({ id: 1, someId: 'xs911-8cf12', title: 'Cool!' })
axiosMock.onGet(`http://localhost/posts/${post.someId}/comments`).reply(200, commentsResponse)
axiosMock.onDelete().reply((config) => {
expect(config.method).toEqual('delete')
expect(config.url).toEqual(`http://localhost/posts/${post.someId}/comments/1`)
return [200, {}]
})
comment = await post.comments().first()
comment.delete()
})
test('a request with custom() method hits the right resource', async () => {
axiosMock.onAny().reply((config) => {
expect(config.url).toEqual(`http://localhost/postz`)
return [200, {}]
})
const post = await Post.custom('postz').first()
})
test('custom() gracefully handles accidental / for string arguments', async () => {
axiosMock.onAny().reply((config) => {
expect(config.url).toBe('http://localhost/postz/recent')
return [200, {}]
})
const post = await Post.custom('/postz', 'recent').first()
})
test('custom() called with multiple objects/strings gets the correct resource', async () => {
let user
let comment
axiosMock.onAny().reply((config) => {
expect(config.method).toEqual('get')
expect(config.url).toEqual('http://localhost/users/1/postz/comments')
return [200, {}]
})
user = new User({ id: 1 })
comment = new Comment()
const result = await Comment.custom(user, 'postz', comment).get()
})
test('a request from hasMany() method hits right resource', async () => {
let user
let posts
axiosMock.onAny().reply((config) => {
expect(config.method).toEqual('get')
expect(config.url).toEqual('http://localhost/users/1/posts')
return [200, {}]
})
user = new User({ id: 1 })
posts = await user.posts().get()
})
test('a request from hasMany() with a find() hits right resource', async () => {
let user
let post
axiosMock.onAny().reply((config) => {
expect(config.method).toEqual('get')
expect(config.url).toEqual('http://localhost/users/1/posts/1')
return [200, {}]
})
user = new User({ id: 1 })
post = await user.posts().find(1)
})
test('a request hasMany() method returns a array of Models', async () => {
axiosMock.onGet('http://localhost/users/1/posts').reply(200, postsResponse)
const user = new User({ id: 1 })
const posts = await user.posts().get()
posts.forEach(post => {
expect(post).toBeInstanceOf(Post)
});
})
test('attach() method hits right endpoint with a POST request', async () => {
let comment
axiosMock.onPost().reply((config) => {
expect(config.method).toEqual('post')
expect(config.data).toEqual(JSON.stringify(comment))
expect(config.url).toEqual('http://localhost/posts/1/comments')
return [200, {}]
})
const post = new Post({ id: 1 })
comment = { text: 'hi!' }
let response = post.comments().attach(comment)
})
test('attach() method hits right endpoint with a POST request (custom PK)', async () => {
Post.prototype['primaryKey'] = () => {
return 'someId'
}
let comment, post
post = new Post({ id: 1, someId: 'gt123-9gh23' })
axiosMock.onAny().reply(config => {
console.log(config)
})
axiosMock.onPost().reply((config) => {
expect(config.method).toEqual('post')
expect(config.data).toEqual(JSON.stringify(comment))
expect(config.url).toEqual(`http://localhost/posts/${post.someId}/comments`)
return [200, {}]
})
comment = { text: 'hi!' }
post.comments().attach(comment)
})
test('sync() method hits right endpoint with a PUT request', async () => {
let comment
axiosMock.onPut().reply((config) => {
expect(config.method).toEqual('put')
expect(config.data).toEqual(JSON.stringify(comment))
expect(config.url).toEqual('http://localhost/posts/1/comments')
return [200, {}]
})
const post = new Post({ id: 1 })
comment = { text: 'hi!' }
let response = post.comments().sync(comment)
})
test('for() method setup the right resource', async () => {
axiosMock.onPost().reply((config) => {
expect(config.method).toEqual('post')
expect(config.url).toEqual('http://localhost/users/1/posts')
return [200, {}]
})
const user = new User({ id: 1 })
const post = new Post({ text: 'Hello' }).for(user)
await post.save()
})
test('Calling for() with multiple arguments productes the correct URL', () => {
const user = new User({ id: 1 })
const post = new Post({ id: 2 })
const comment = new Comment({
post_id: 2,
text: 'for() takes more than one argument now!'
}).for(user, post)
expect(comment.endpoint()).toEqual(`http://localhost/users/${user.id}/posts/${post.id}/comments`)
})
test('it throws a error when for() method does not recieve a instance of Model', () => {
errorModel = () => {
const post = new Post({ text: 'Hello' }).for()
}
expect(errorModel).toThrow('The for() method takes a minimum of one argument.')
errorModel = () => {
const post = new Post({ text: 'Hello' }).for({})
}
expect(errorModel).toThrow('The object referenced on for() method is not a valid Model.')
errorModel = () => {
const post = new Post({ text: 'Hello' }).for('')
}
expect(errorModel).toThrow('The object referenced on for() method is not a valid Model.')
errorModel = () => {
const post = new Post({ text: 'Hello' }).for(1)
}
expect(errorModel).toThrow('The object referenced on for() method is not a valid Model.')
})
test('it throws a error when for() when referenced object has not a valid id', () => {
errorModel = () => {
const user = new User({ name: 'Mary' })
const post = new Post({ text: 'Hello' }).for(user)
}
expect(errorModel).toThrow('The object referenced on for() method has a invalid id.')
})
test('it throws a error when a custom() parameter is not a valid Model or a string', () => {
errorModel = () => {
const post = new Post({ text: 'Hello' }).custom()
}
expect(errorModel).toThrow('The custom() method takes a minimum of one argument.')
errorModel = () => {
const user = new User({ name: 'Mary' })
const post = new Post({ text: 'Hello' }).custom(user, 'a-string', 42)
}
expect(errorModel).toThrow('Arguments to custom() must be strings or instances of Model.')
})
})