forked from emberjs/data
/
store_adapter_test.js
802 lines (642 loc) · 25.9 KB
/
store_adapter_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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
This is an integration test that tests the communication between a store
and its adapter.
Typically, when a method is invoked on the store, it calls a related
method on its adapter. The adapter notifies the store that it has
completed the assigned task, either synchronously or asynchronously,
by calling a method on the store.
These tests ensure that the proper methods get called, and, if applicable,
the given record or record array changes state appropriately.
*/
var get = Ember.get, set = Ember.set;
var Person, Dog, env, store, adapter;
var run = Ember.run;
module("integration/adapter/store_adapter - DS.Store and DS.Adapter integration test", {
setup: function() {
Person = DS.Model.extend({
updatedAt: DS.attr('string'),
name: DS.attr('string'),
firstName: DS.attr('string'),
lastName: DS.attr('string')
});
Dog = DS.Model.extend({
name: DS.attr('string')
});
env = setupStore({ person: Person, dog: Dog });
store = env.store;
adapter = env.adapter;
},
teardown: function() {
run(env.container, 'destroy');
}
});
test("Records loaded multiple times and retrieved in recordArray are ready to send state events", function() {
adapter.findQuery = function(store, type, query, recordArray) {
return Ember.RSVP.resolve([{
id: 1,
name: "Mickael Ramírez"
}, {
id: 2,
name: "Johny Fontana"
}]);
};
run(store, 'findQuery', 'person', {q: 'bla'}).then(async(function(people) {
var people2 = store.findQuery('person', { q: 'bla2' });
return Ember.RSVP.hash({ people: people, people2: people2 });
})).then(async(function(results) {
equal(results.people2.get('length'), 2, 'return the elements' );
ok( results.people2.get('isLoaded'), 'array is loaded' );
var person = results.people.objectAt(0);
ok(person.get('isLoaded'), 'record is loaded');
// delete record will not throw exception
person.deleteRecord();
}));
});
test("by default, createRecords calls createRecord once per record", function() {
var count = 1;
adapter.createRecord = function(store, type, record) {
equal(type, Person, "the type is correct");
if (count === 1) {
equal(get(record, 'name'), "Tom Dale");
} else if (count === 2) {
equal(get(record, 'name'), "Yehuda Katz");
} else {
ok(false, "should not have invoked more than 2 times");
}
var hash = get(record, 'data');
hash.id = count;
hash.updatedAt = "now";
count++;
return Ember.RSVP.resolve(hash);
};
var tom, yehuda;
run(function(){
tom = store.createRecord('person', { name: "Tom Dale" });
yehuda = store.createRecord('person', { name: "Yehuda Katz" });
});
var promise = run(function(){
return Ember.RSVP.hash({
tom: tom.save(),
yehuda: yehuda.save()
});
});
promise.then(async(function(records) {
tom = records.tom;
yehuda = records.yehuda;
asyncEqual(tom, store.find('person', 1), "Once an ID is in, find returns the same object");
asyncEqual(yehuda, store.find('person', 2), "Once an ID is in, find returns the same object");
equal(get(tom, 'updatedAt'), "now", "The new information is received");
equal(get(yehuda, 'updatedAt'), "now", "The new information is received");
}));
});
test("by default, updateRecords calls updateRecord once per record", function() {
var count = 0;
adapter.updateRecord = function(store, type, record) {
equal(type, Person, "the type is correct");
if (count === 0) {
equal(get(record, 'name'), "Tom Dale");
} else if (count === 1) {
equal(get(record, 'name'), "Yehuda Katz");
} else {
ok(false, "should not get here");
}
count++;
equal(record.get('isSaving'), true, "record is saving");
return Ember.RSVP.resolve();
};
run(function(){
store.push('person', { id: 1, name: "Braaaahm Dale" });
store.push('person', { id: 2, name: "Brohuda Katz" });
});
var promise = run(function(){
return Ember.RSVP.hash({
tom: store.find('person', 1),
yehuda: store.find('person', 2)
});
});
promise.then(async(function(records) {
var tom = records.tom, yehuda = records.yehuda;
set(tom, "name", "Tom Dale");
set(yehuda, "name", "Yehuda Katz");
return Ember.RSVP.hash({ tom: tom.save(), yehuda: yehuda.save() });
})).then(async(function(records) {
var tom = records.tom, yehuda = records.yehuda;
equal(tom.get('isSaving'), false, "record is no longer saving");
equal(tom.get('isLoaded'), true, "record is loaded");
equal(yehuda.get('isSaving'), false, "record is no longer saving");
equal(yehuda.get('isLoaded'), true, "record is loaded");
}));
});
test("calling store.didSaveRecord can provide an optional hash", function() {
var count = 0;
adapter.updateRecord = function(store, type, record) {
equal(type, Person, "the type is correct");
count++;
if (count === 1) {
equal(get(record, 'name'), "Tom Dale");
return Ember.RSVP.resolve({ id: 1, name: "Tom Dale", updatedAt: "now" });
} else if (count === 2) {
equal(get(record, 'name'), "Yehuda Katz");
return Ember.RSVP.resolve({ id: 2, name: "Yehuda Katz", updatedAt: "now!" });
} else {
ok(false, "should not get here");
}
};
run(function(){
store.push('person', { id: 1, name: "Braaaahm Dale" });
store.push('person', { id: 2, name: "Brohuda Katz" });
});
var promise = run(function(){
return Ember.RSVP.hash({
tom: store.find('person', 1),
yehuda: store.find('person', 2)
});
});
promise.then(async(function(records) {
var tom = records.tom, yehuda = records.yehuda;
set(tom, "name", "Tom Dale");
set(yehuda, "name", "Yehuda Katz");
return Ember.RSVP.hash({ tom: tom.save(), yehuda: yehuda.save() });
})).then(async(function(records) {
var tom = records.tom, yehuda = records.yehuda;
equal(get(tom, 'isDirty'), false, "the record should not be dirty");
equal(get(tom, 'updatedAt'), "now", "the hash was updated");
equal(get(yehuda, 'isDirty'), false, "the record should not be dirty");
equal(get(yehuda, 'updatedAt'), "now!", "the hash was updated");
}));
});
test("by default, deleteRecord calls deleteRecord once per record", function() {
expect(4);
var count = 0;
adapter.deleteRecord = function(store, type, record) {
equal(type, Person, "the type is correct");
if (count === 0) {
equal(get(record, 'name'), "Tom Dale");
} else if (count === 1) {
equal(get(record, 'name'), "Yehuda Katz");
} else {
ok(false, "should not get here");
}
count++;
return Ember.RSVP.resolve();
};
run(function(){
store.push('person', { id: 1, name: "Tom Dale" });
store.push('person', { id: 2, name: "Yehuda Katz" });
});
var promise = run(function(){
return Ember.RSVP.hash({
tom: store.find('person', 1),
yehuda: store.find('person', 2)
});
});
promise.then(async(function(records) {
var tom = records.tom, yehuda = records.yehuda;
tom.deleteRecord();
yehuda.deleteRecord();
tom.save();
yehuda.save();
}));
});
test("by default, destroyRecord calls deleteRecord once per record without requiring .save", function() {
expect(4);
var count = 0;
adapter.deleteRecord = function(store, type, record) {
equal(type, Person, "the type is correct");
if (count === 0) {
equal(get(record, 'name'), "Tom Dale");
} else if (count === 1) {
equal(get(record, 'name'), "Yehuda Katz");
} else {
ok(false, "should not get here");
}
count++;
return Ember.RSVP.resolve();
};
run(function(){
store.push('person', { id: 1, name: "Tom Dale" });
store.push('person', { id: 2, name: "Yehuda Katz" });
});
var promise = run(function(){
return Ember.RSVP.hash({
tom: store.find('person', 1),
yehuda: store.find('person', 2)
});
});
promise.then(async(function(records) {
var tom = records.tom, yehuda = records.yehuda;
tom.destroyRecord();
yehuda.destroyRecord();
}));
});
test("if an existing model is edited then deleted, deleteRecord is called on the adapter", function() {
expect(5);
var count = 0;
adapter.deleteRecord = function(store, type, record) {
count++;
equal(get(record, 'id'), 'deleted-record', "should pass correct record to deleteRecord");
equal(count, 1, "should only call deleteRecord method of adapter once");
return Ember.RSVP.resolve();
};
adapter.updateRecord = function() {
ok(false, "should not have called updateRecord method of adapter");
};
// Load data for a record into the store.
run(function(){
store.push('person', { id: 'deleted-record', name: "Tom Dale" });
});
// Retrieve that loaded record and edit it so it becomes dirty
run(store, 'find', 'person', 'deleted-record').then(async(function(tom) {
tom.set('name', "Tom Mothereffin' Dale");
equal(get(tom, 'isDirty'), true, "precond - record should be dirty after editing");
tom.deleteRecord();
return tom.save();
})).then(async(function(tom) {
equal(get(tom, 'isDirty'), false, "record should not be dirty");
equal(get(tom, 'isDeleted'), true, "record should be considered deleted");
}));
});
test("if a deleted record errors, it enters the error state", function() {
var count = 0;
adapter.deleteRecord = function(store, type, record) {
if (count++ === 0) {
return Ember.RSVP.reject();
} else {
return Ember.RSVP.resolve();
}
};
run(function(){
store.push('person', { id: 'deleted-record', name: "Tom Dale" });
});
var tom;
run(function(){
store.find('person', 'deleted-record').then(async(function(person) {
tom = person;
person.deleteRecord();
return person.save();
})).then(null, async(function() {
equal(tom.get('isError'), true, "Tom is now errored");
// this time it succeeds
return tom.save();
})).then(async(function() {
equal(tom.get('isError'), false, "Tom is not errored anymore");
}));
});
});
test("if a created record is marked as invalid by the server, it enters an error state", function() {
adapter.createRecord = function(store, type, record) {
equal(type, Person, "the type is correct");
if (get(record, 'name').indexOf('Bro') === -1) {
return Ember.RSVP.reject(new DS.InvalidError({ name: ['common... name requires a "bro"'] }));
} else {
return Ember.RSVP.resolve();
}
};
var yehuda = run(function(){
return store.createRecord('person', { id: 1, name: "Yehuda Katz" });
});
// Wrap this in an Ember.run so that all chained async behavior is set up
// before flushing any scheduled behavior.
Ember.run(function() {
yehuda.save().then(null, async(function(error) {
equal(get(yehuda, 'isValid'), false, "the record is invalid");
ok(get(yehuda, 'errors.name'), "The errors.name property exists");
set(yehuda, 'updatedAt', true);
equal(get(yehuda, 'isValid'), false, "the record is still invalid");
set(yehuda, 'name', "Brohuda Brokatz");
equal(get(yehuda, 'isValid'), true, "the record is no longer invalid after changing");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
equal(get(yehuda, 'isNew'), true, "precond - record is still new");
return yehuda.save();
})).then(async(function(person) {
strictEqual(person, yehuda, "The promise resolves with the saved record");
equal(get(yehuda, 'isValid'), true, "record remains valid after committing");
equal(get(yehuda, 'isNew'), false, "record is no longer new");
}));
});
});
test("allows errors on arbitrary properties on create", function() {
adapter.createRecord = function(store, type, record) {
if (get(record, 'name').indexOf('Bro') === -1) {
return Ember.RSVP.reject(new DS.InvalidError({ base: ['is a generally unsavoury character'] }));
} else {
return Ember.RSVP.resolve();
}
};
var yehuda = run(function () {
return store.createRecord('person', { id: 1, name: "Yehuda Katz" });
});
// Wrap this in an Ember.run so that all chained async behavior is set up
// before flushing any scheduled behavior.
run(function() {
yehuda.save().then(null, async(function(error) {
equal(get(yehuda, 'isValid'), false, "the record is invalid");
ok(get(yehuda, 'errors.base'), "The errors.base property exists");
deepEqual(get(yehuda, 'errors').errorsFor('base'), [{attribute: 'base', message: "is a generally unsavoury character"}]);
set(yehuda, 'updatedAt', true);
equal(get(yehuda, 'isValid'), false, "the record is still invalid");
set(yehuda, 'name', "Brohuda Brokatz");
equal(get(yehuda, 'isValid'), false, "the record is still invalid as far as we know");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
equal(get(yehuda, 'isNew'), true, "precond - record is still new");
return yehuda.save();
})).then(async(function(person) {
strictEqual(person, yehuda, "The promise resolves with the saved record");
ok(!get(yehuda, 'errors.base'), "The errors.base property does not exist");
deepEqual(get(yehuda, 'errors').errorsFor('base'), []);
equal(get(yehuda, 'isValid'), true, "record remains valid after committing");
equal(get(yehuda, 'isNew'), false, "record is no longer new");
}));
});
});
test("if a created record is marked as invalid by the server, you can attempt the save again", function() {
var saveCount = 0;
adapter.createRecord = function(store, type, record) {
equal(type, Person, "the type is correct");
saveCount++;
if (get(record, 'name').indexOf('Bro') === -1) {
return Ember.RSVP.reject(new DS.InvalidError({ name: ['common... name requires a "bro"'] }));
} else {
return Ember.RSVP.resolve();
}
};
var yehuda = run(function(){
return store.createRecord('person', { id: 1, name: "Yehuda Katz" });
});
// Wrap this in an Ember.run so that all chained async behavior is set up
// before flushing any scheduled behavior.
Ember.run(function() {
yehuda.save().then(null, async(function(reason) {
equal(saveCount, 1, "The record has been saved once");
ok(reason.message.match("The backend rejected the commit because it was invalid"), "It should fail due to being invalid");
equal(get(yehuda, 'isValid'), false, "the record is invalid");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
ok(get(yehuda, 'errors.name'), "The errors.name property exists");
equal(get(yehuda, 'isNew'), true, "precond - record is still new");
return yehuda.save();
})).then(null, async(function(reason) {
equal(saveCount, 2, "The record has been saved twice");
ok(reason.message.match("The backend rejected the commit because it was invalid"), "It should fail due to being invalid");
equal(get(yehuda, 'isValid'), false, "the record is still invalid");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
ok(get(yehuda, 'errors.name'), "The errors.name property exists");
equal(get(yehuda, 'isNew'), true, "precond - record is still new");
set(yehuda, 'name', 'Brohuda Brokatz');
return yehuda.save();
})).then(async(function(person) {
equal(saveCount, 3, "The record has been saved thrice");
equal(get(yehuda, 'isValid'), true, "record is valid");
equal(get(yehuda, 'isDirty'), false, "record is not dirty");
equal(get(yehuda, 'errors.isEmpty'), true, "record has no errors");
}));
});
});
test("if a created record is marked as erred by the server, it enters an error state", function() {
adapter.createRecord = function(store, type, record) {
return Ember.RSVP.reject();
};
Ember.run(function() {
var person = store.createRecord('person', { id: 1, name: "John Doe" });
person.save().then(null, async(function() {
ok(get(person, 'isError'), "the record is in the error state");
}));
});
});
test("if an updated record is marked as invalid by the server, it enters an error state", function() {
adapter.updateRecord = function(store, type, record) {
equal(type, Person, "the type is correct");
if (get(record, 'name').indexOf('Bro') === -1) {
return Ember.RSVP.reject(new DS.InvalidError({ name: ['common... name requires a "bro"'] }));
} else {
return Ember.RSVP.resolve();
}
};
var yehuda = run(function(){
return store.push('person', { id: 1, name: "Brohuda Brokatz" });
});
Ember.run(function() {
store.find('person', 1).then(async(function(person) {
equal(person, yehuda, "The same object is passed through");
equal(get(yehuda, 'isValid'), true, "precond - the record is valid");
set(yehuda, 'name', "Yehuda Katz");
equal(get(yehuda, 'isValid'), true, "precond - the record is still valid as far as we know");
equal(get(yehuda, 'isDirty'), true, "the record is dirty");
return yehuda.save();
})).then(null, async(function(reason) {
equal(get(yehuda, 'isDirty'), true, "the record is still dirty");
equal(get(yehuda, 'isValid'), false, "the record is invalid");
set(yehuda, 'updatedAt', true);
equal(get(yehuda, 'isValid'), false, "the record is still invalid");
set(yehuda, 'name', "Brohuda Brokatz");
equal(get(yehuda, 'isValid'), true, "the record is no longer invalid after changing");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
return yehuda.save();
})).then(async(function(yehuda) {
equal(get(yehuda, 'isValid'), true, "record remains valid after committing");
equal(get(yehuda, 'isDirty'), false, "record is no longer new");
}));
});
});
test("records can have errors on arbitrary properties after update", function() {
adapter.updateRecord = function(store, type, record) {
if (get(record, 'name').indexOf('Bro') === -1) {
return Ember.RSVP.reject(new DS.InvalidError({ base: ['is a generally unsavoury character'] }));
} else {
return Ember.RSVP.resolve();
}
};
var yehuda = run(function() {
return store.push('person', { id: 1, name: "Brohuda Brokatz" });
});
run(function() {
store.find('person', 1).then(async(function(person) {
equal(person, yehuda, "The same object is passed through");
equal(get(yehuda, 'isValid'), true, "precond - the record is valid");
set(yehuda, 'name', "Yehuda Katz");
equal(get(yehuda, 'isValid'), true, "precond - the record is still valid as far as we know");
equal(get(yehuda, 'isDirty'), true, "the record is dirty");
return yehuda.save();
})).then(null, async(function(reason) {
equal(get(yehuda, 'isDirty'), true, "the record is still dirty");
equal(get(yehuda, 'isValid'), false, "the record is invalid");
ok(get(yehuda, 'errors.base'), "The errors.base property exists");
deepEqual(get(yehuda, 'errors').errorsFor('base'), [{attribute: 'base', message: "is a generally unsavoury character"}]);
set(yehuda, 'updatedAt', true);
equal(get(yehuda, 'isValid'), false, "the record is still invalid");
set(yehuda, 'name', "Brohuda Brokatz");
equal(get(yehuda, 'isValid'), false, "the record is still invalid after changing (only server can know if it's now valid)");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
return yehuda.save();
})).then(async(function(yehuda) {
equal(get(yehuda, 'isValid'), true, "record remains valid after committing");
equal(get(yehuda, 'isDirty'), false, "record is no longer new");
ok(!get(yehuda, 'errors.base'), "The errors.base property does not exist");
deepEqual(get(yehuda, 'errors').errorsFor('base'), []);
}));
});
});
test("if an updated record is marked as invalid by the server, you can attempt the save again", function() {
var saveCount = 0;
adapter.updateRecord = function(store, type, record) {
equal(type, Person, "the type is correct");
saveCount++;
if (get(record, 'name').indexOf('Bro') === -1) {
return Ember.RSVP.reject(new DS.InvalidError({ name: ['common... name requires a "bro"'] }));
} else {
return Ember.RSVP.resolve();
}
};
var yehuda = run(function(){
return store.push('person', { id: 1, name: "Brohuda Brokatz" });
});
Ember.run(function() {
store.find('person', 1).then(async(function(person) {
equal(person, yehuda, "The same object is passed through");
equal(get(yehuda, 'isValid'), true, "precond - the record is valid");
set(yehuda, 'name', "Yehuda Katz");
equal(get(yehuda, 'isValid'), true, "precond - the record is still valid as far as we know");
equal(get(yehuda, 'isDirty'), true, "the record is dirty");
return yehuda.save();
})).then(null, async(function(reason) {
equal(saveCount, 1, "The record has been saved once");
ok(reason.message.match("The backend rejected the commit because it was invalid"), "It should fail due to being invalid");
equal(get(yehuda, 'isDirty'), true, "the record is still dirty");
equal(get(yehuda, 'isValid'), false, "the record is invalid");
return yehuda.save();
})).then(null, async(function(reason) {
equal(saveCount, 2, "The record has been saved twice");
ok(reason.message.match("The backend rejected the commit because it was invalid"), "It should fail due to being invalid");
equal(get(yehuda, 'isValid'), false, "record is still invalid");
equal(get(yehuda, 'isDirty'), true, "record is still dirty");
set(yehuda, 'name', 'Brohuda Brokatz');
return yehuda.save();
})).then(async(function(person) {
equal(saveCount, 3, "The record has been saved thrice");
equal(get(yehuda, 'isValid'), true, "record is valid");
equal(get(yehuda, 'isDirty'), false, "record is not dirty");
equal(get(yehuda, 'errors.isEmpty'), true, "record has no errors");
}));
});
});
test("if a updated record is marked as erred by the server, it enters an error state", function() {
adapter.updateRecord = function(store, type, record) {
return Ember.RSVP.reject();
};
var person = run(function(){
return store.push(Person, { id: 1, name: "John Doe" });
});
run(store, 'find', 'person', 1).then(async(function(record) {
equal(record, person, "The person was resolved");
person.set('name', "Jonathan Doe");
return person.save();
})).then(null, async(function(reason) {
ok(get(person, 'isError'), "the record is in the error state");
}));
});
test("can be created after the DS.Store", function() {
expect(1);
adapter.find = function(store, type) {
equal(type, Person, "the type is correct");
return Ember.RSVP.resolve({ id: 1 });
};
run(function(){
store.find('person', 1);
});
});
test("the filter method can optionally take a server query as well", function() {
adapter.findQuery = function(store, type, query, array) {
return Ember.RSVP.resolve([
{ id: 1, name: "Yehuda Katz" },
{ id: 2, name: "Tom Dale" }
]);
};
var asyncFilter = store.filter('person', { page: 1 }, function(data) {
return data.get('name') === "Tom Dale";
});
var loadedFilter;
asyncFilter.then(async(function(filter) {
loadedFilter = filter;
return store.find('person', 2);
})).then(async(function(tom) {
equal(get(loadedFilter, 'length'), 1, "The filter has an item in it");
deepEqual(loadedFilter.toArray(), [ tom ], "The filter has a single entry in it");
}));
});
test("relationships returned via `commit` do not trigger additional findManys", function() {
Person.reopen({
dogs: DS.hasMany()
});
run(function(){
store.push('dog', { id: 1, name: "Scruffy" });
});
adapter.find = function(store, type, id) {
return Ember.RSVP.resolve({ id: 1, name: "Tom Dale", dogs: [1] });
};
adapter.updateRecord = function(store, type, record) {
return new Ember.RSVP.Promise(function(resolve, reject) {
store.push('person', { id: 1, name: "Tom Dale", dogs: [1, 2] });
store.push('dog', { id: 2, name: "Scruffles" });
resolve({ id: 1, name: "Scruffy" });
});
};
adapter.findMany = function(store, type, ids) {
ok(false, "Should not get here");
};
run(function(){
store.find('person', 1).then(async(function(person) {
return Ember.RSVP.hash({ tom: person, dog: store.find('dog', 1) });
})).then(async(function(records) {
records.tom.get('dogs');
return records.dog.save();
})).then(async(function(tom) {
ok(true, "Tom was saved");
}));
});
});
test("relationships don't get reset if the links is the same", function() {
Person.reopen({
dogs: DS.hasMany({ async: true })
});
var count = 0;
adapter.findHasMany = function() {
ok(count++ === 0, "findHasMany is only called once");
return Ember.RSVP.resolve([{ id: 1, name: "Scruffy" }]);
};
run(function(){
store.push('person', { id: 1, name: "Tom Dale", links: { dogs: "/dogs" } });
});
var tom, dogs;
run(store, 'find', 'person', 1).then(async(function(person) {
tom = person;
dogs = tom.get('dogs');
return dogs;
})).then(async(function(dogs) {
equal(dogs.get('length'), 1, "The dogs are loaded");
store.push('person', { id: 1, name: "Tom Dale", links: { dogs: "/dogs" } });
ok(tom.get('dogs') instanceof DS.PromiseArray, 'dogs is a promise');
return tom.get('dogs');
})).then(async(function(dogs) {
equal(dogs.get('length'), 1, "The same dogs are loaded");
}));
});
test("async hasMany always returns a promise", function() {
Person.reopen({
dogs: DS.hasMany({ async: true })
});
adapter.createRecord = function(store, type, record) {
var hash = { name: "Tom Dale" };
hash.dogs = [];
hash.id = 1;
return Ember.RSVP.resolve(hash);
};
var tom;
run(function(){
tom = store.createRecord('person', { name: "Tom Dale" });
});
ok(tom.get('dogs') instanceof DS.PromiseArray, "dogs is a promise before save");
run(function(){
tom.save().then(async(function() {
ok(tom.get('dogs') instanceof DS.PromiseArray, "dogs is a promise after save");
}));
});
});