-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
shared-adapter-behaviour.js
2132 lines (1701 loc) · 68 KB
/
shared-adapter-behaviour.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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Ember from 'ember';
import { run } from '@ember/runloop';
import { module, test } from 'qunit';
import { isEquivalent } from 'ember-data-factory-guy/utils/helper-functions';
import FactoryGuy, {
build,
buildList,
make,
makeList,
makeNew,
mockCreate,
mockDelete,
mockFindAll,
mockFindRecord,
mockQuery,
mockQueryRecord,
mockReload,
mockUpdate
} from 'ember-data-factory-guy';
import $ from 'jquery';
import Profile from 'dummy/models/profile';
import SuperHero from 'dummy/models/super-hero';
const A = Ember.A;
let SharedBehavior = {};
//////// mockFindRecord common /////////
SharedBehavior.mockFindRecordCommonTests = function() {
test("the basic returns default attributes", function(assert) {
run(() => {
let done = assert.async(),
mock = mockFindRecord('profile'),
profileId = mock.get('id');
FactoryGuy.store.findRecord('profile', profileId).then(function(profile) {
assert.equal(profile.get('id'), profileId);
assert.equal(profile.get('description'), 'Text goes here');
done();
});
});
});
test("when returns json is used", async function(assert) {
return run(async () => {
let json = build('profile'),
mock = mockFindRecord('profile').returns({json}),
profileId = mock.get('id');
let profile = await FactoryGuy.store.findRecord('profile', profileId);
assert.equal(profile.get('id'), profileId);
assert.equal(profile.get('description'), json.get('description'));
});
});
test("returns id succeeds and returns model when id for model type found in store after createRecord", async function(assert) {
let profileId = 1,
{store} = FactoryGuy;
mockCreate('profile').returns({attrs: {id: profileId}});
mockFindRecord('profile').returns({id: profileId});
let newRecord = run(() => store.createRecord('profile', {description: 'foo'}));
let newProfile = await run(() => newRecord.save());
let foundRecord = await run(() => store.findRecord('profile', profileId));
assert.deepEqual(foundRecord, newProfile);
});
test("returns id succeeds and returns model when id for model type found in store", async function(assert) {
let existingProfile = make('profile'),
{store} = FactoryGuy;
mockFindRecord('profile').returns({id: existingProfile.get('id')});
let profile = await run(() => store.findRecord('profile', existingProfile.get('id')));
assert.equal(profile.get('id'), existingProfile.get('id'));
});
test("returns id fails with 404 if record for id and model type not found in store", function(assert) {
run(() => {
let done = assert.async();
let profileId = 1;
mockFindRecord('profile').returns({id: profileId});
FactoryGuy.store.findRecord('profile', profileId)
.catch((reason) => {
assert.equal(reason.errors[0].status, '404');
done();
});
});
});
// test("returns model succeeds", function(assert) {
// run(() => {
// let done = assert.async();
// let cat = make('cat', { type: 'Cutest' });
// let mock = mockFindRecord(cat);//.returns({ model: cat });
// console.log(mock.index, cat.get('id'));
// FactoryGuy.store.findRecord('cat', mock.get('id'), { reload: true }).then(function(catA) {
// assert.equal(catA.get('type'), 'Cutest');
// done();
// });
// });
// });
//
// test("with model that has attribute named type, is not polymorphic, and returns model", function(assert) {
// run(() => {
// let done = assert.async();
// let cat = make('cat', { type: 'Cutest' });
// let mock = mockFindRecord(cat);//.returns({ model: cat });
// console.log(mock.index, cat.get('id'));
// FactoryGuy.store.findRecord('cat', mock.get('id'), { reload: true }).then(function(catA) {
// assert.equal(catA.get('type'), 'Cutest');
// done();
// });
// });
// });
test("returns model that has attribute named type, but is not polymorphic", async function(assert) {
return run(async () => {
let cat = make('cat', {type: 'Cutest'});
let mock = mockFindRecord('cat').returns({model: cat});
let catA = await FactoryGuy.store.findRecord('cat', mock.get('id'), {reload: true});
assert.equal(catA.get('type'), 'Cutest');
});
});
test("when using model as the param of modelName to find record", async function(assert) {
return run(async () => {
let cat = make('cat'),
mock = mockFindRecord(cat);
let catA = await FactoryGuy.store.findRecord('cat', mock.get('id'), {reload: true});
assert.deepEqual(catA, cat);
});
});
// test for issue # 219
test("with model that has attribute key defined in serializer attrs", function(assert) {
run(() => {
let done = assert.async();
let mock = mockFindRecord('cat');
assert.equal(mock.get('catName'), 'Cat 1');
assert.equal(mock.get('catFriend'), 'Friend 1');
FactoryGuy.store.findRecord('cat', mock.get('id')).then(function(cat) {
assert.equal(cat.get('name'), 'Cat 1');
assert.equal(cat.get('friend'), 'Friend 1');
done();
});
});
});
test("with model that has primaryKey defined in serializer attrs and is attribute of model", function(assert) {
run(() => {
let done = assert.async();
let mock = mockFindRecord('dog');
FactoryGuy.store.findRecord('dog', mock.get('id')).then(function(dog) {
assert.equal(dog.get('id'), 'Dog1');
assert.equal(dog.get('dogNumber'), 'Dog1');
done();
});
});
});
test("with fixture options", function(assert) {
run(() => {
let done = assert.async();
let mock = mockFindRecord('profile', {description: 'dude'});
let profileId = mock.get('id');
FactoryGuy.store.findRecord('profile', profileId).then(function(profile) {
assert.ok(profile.get('description') === 'dude');
done();
});
});
});
test("handles differently cased attributes", function(assert) {
run(() => {
let done = assert.async();
let mock = mockFindRecord('profile');
let profileId = mock.get('id');
FactoryGuy.store.findRecord('profile', profileId).then(function(profile) {
assert.ok(profile.get('camelCaseDescription') === 'textGoesHere');
assert.ok(profile.get('snake_case_description') === 'text_goes_here');
done();
});
});
});
test("with traits", function(assert) {
run(() => {
let done = assert.async();
let mock = mockFindRecord('profile', 'goofy_description');
let profileId = mock.get('id');
FactoryGuy.store.findRecord('profile', profileId).then(function(profile) {
assert.ok(profile.get('description') === 'goofy');
done();
});
});
});
test("with traits and extra options", function(assert) {
run(() => {
let done = assert.async();
let mock = mockFindRecord('profile', 'goofy_description', {description: 'dude'});
let profileId = mock.get('id');
FactoryGuy.store.findRecord('profile', profileId).then(function(profile) {
assert.ok(profile.get('description') === 'dude');
done();
});
});
});
test("failure with fails method when passing modelName as parameter", function(assert) {
let done = assert.async();
run(() => {
let mock = mockFindRecord('profile').fails();
FactoryGuy.store.findRecord('profile', mock.get('id')).catch(() => {
assert.equal(mock.timesCalled, 1);
done();
});
});
});
test("failure with fails method when passing modeName as parameter and returning instance", function(assert) {
run(() => {
let model = make('profile');
let mock = mockFindRecord('profile').returns({model}).fails();
return FactoryGuy.store.findRecord('profile', model.id, {reload: true})
.catch(() => {
assert.equal(mock.timesCalled, 1);
assert.equal(mock.status, 500);
});
});
});
test("failure with fails method when passing model instance as parameter and no returns is used", function(assert) {
let done = assert.async();
let profile = make('profile');
let mock = mockFindRecord(profile).fails();
run(() => {
FactoryGuy.store.findRecord('profile', profile.id, {reload: true})
.catch(() => {
assert.equal(mock.timesCalled, 1, 'mock called once');
assert.equal(mock.status, 500, 'stats 500');
done();
});
});
});
};
SharedBehavior.mockFindRecordSideloadingTests = function() {
module('#mockFindRecord | sideloading', function() {
test("belongsTo association", function(assert) {
run(() => {
let done = assert.async();
let profile = mockFindRecord('profile', 'with_company', 'with_bat_man');
let profileId = profile.get('id');
FactoryGuy.store.findRecord('profile', profileId).then(function(profile) {
assert.ok(profile.get('company.name') === 'Silly corp');
assert.ok(profile.get('superHero.name') === 'BatMan');
done();
});
});
});
test("hasMany association", function(assert) {
run(() => {
let done = assert.async();
let user = mockFindRecord('user', 'with_hats');
let userId = user.get('id');
FactoryGuy.store.findRecord('user', userId).then(function(user) {
assert.ok(user.get('hats.length') === 2);
assert.ok(user.get('hats.firstObject.type') === 'BigHat');
done();
});
});
});
test("using returns with json", function(assert) {
run(() => {
let done = assert.async();
let json = build('profile', 'with_company', 'with_bat_man');
mockFindRecord('profile').returns({json});
let profileId = json.get('id');
FactoryGuy.store.findRecord('profile', profileId).then(function(profile) {
assert.ok(profile.get('company.name') === 'Silly corp');
assert.ok(profile.get('superHero.name') === 'BatMan');
done();
});
});
});
test("using returns with json with composed hasMany association", function(assert) {
run(() => {
let done = assert.async();
let hat1 = build('big-hat');
let hat2 = build('big-hat');
let json = build('user', {hats: [hat1, hat2]});
mockFindRecord('user').returns({json});
FactoryGuy.store.findRecord('user', json.get('id')).then(function(user) {
assert.ok(user.get('hats.firstObject.id') === hat1.get('id') + '');
assert.ok(user.get('hats.lastObject.id') === hat2.get('id') + '');
done();
});
});
});
test("using returns with model", function(assert) {
run(() => {
let done = assert.async();
let model = make('profile', 'with_company', 'with_bat_man');
let profile = mockFindRecord('profile').returns({model});
let profileId = profile.get('id');
FactoryGuy.store.findRecord('profile', profileId, {reload: true}).then(function(profile) {
assert.ok(profile.get('company.name') === 'Silly corp');
assert.ok(profile.get('superHero.name') === 'BatMan');
assert.equal(FactoryGuy.store.peekAll('profile').get('content').length, 1, "does not make another profile");
done();
});
});
});
});
};
SharedBehavior.mockFindRecordEmbeddedTests = function() {
module('#mockFindRecord | embedded', function() {
test("belongsTo", function(assert) {
run(() => {
let done = assert.async();
let mock = mockFindRecord('comic-book', 'marvel');
FactoryGuy.store.findRecord('comic-book', mock.get('id')).then(function(comic) {
assert.ok(comic.get('name') === 'Comic Times #1');
assert.ok(comic.get('company.name') === 'Marvel Comics');
done();
});
});
});
test("hasMany", function(assert) {
run(() => {
let done = assert.async();
let mock = mockFindRecord('comic-book', 'with_bad_guys');
FactoryGuy.store.findRecord('comic-book', mock.get('id')).then(function(comic) {
assert.ok(comic.get('name') === 'Comic Times #1');
assert.ok(comic.get('characters').mapBy('name') + '' === ['BadGuy#1', 'BadGuy#2'] + '');
done();
});
});
});
});
};
//////// mockReload /////////
SharedBehavior.mockReloadTests = function() {
test("with a record handles reload, and does not change attributes", function(assert) {
let done = assert.async();
run(() => {
let profile = make('profile', {description: "whatever"});
mockReload(profile);
profile.reload().then(function(reloaded) {
assert.ok(reloaded.id === profile.id);
assert.ok(reloaded.get('description') === profile.get('description'));
done();
});
});
});
test("can change the attributes using returns method with attrs", function(assert) {
let done = assert.async();
run(() => {
let profile = make('profile', {description: "whatever", camelCaseDescription: "noodles"});
mockReload(profile).returns({attrs: {description: "moo"}});
profile.reload().then(function(reloaded) {
assert.ok(reloaded.id === profile.id, 'does not change id');
assert.ok(reloaded.get('description') === "moo", "attribute changed");
assert.ok(reloaded.get('camelCaseDescription') === "noodles", "other attributes are same");
done();
});
});
});
test("using returns method with json", function(assert) {
let done = assert.async();
run(() => {
let profile = make('profile', {description: "tomatoes", camelCaseDescription: "noodles"});
let newProfile = build('profile', {id: profile.get('id'), description: "potatoes", camelCaseDescription: "poodles"});
mockReload(profile).returns({json: newProfile});
profile.reload().then(function(reloaded) {
assert.ok(reloaded.id === profile.id, 'does not change id');
assert.ok(reloaded.get('description') === "potatoes", "description changed");
assert.ok(reloaded.get('camelCaseDescription') === "poodles", "camelCaseDescription changes");
done();
});
});
});
test("failure with fails method", function(assert) {
run(() => {
let done = assert.async();
let mock = mockReload('profile', 1).fails();
FactoryGuy.store.findRecord('profile', 1)
.catch(() => {
assert.equal(mock.timesCalled, 1);
assert.ok(true);
done();
}
);
});
});
};
/////// mockFindAll common //////////
SharedBehavior.mockFindAllCommonTests = function() {
test("the basic", function(assert) {
run(() => {
let done = assert.async();
mockFindAll('user', 2);
FactoryGuy.store.findAll('user').then(function(users) {
assert.ok(users.get('length') === 2);
done();
});
});
});
test("handles differently cased attributes", function(assert) {
run(() => {
let done = assert.async();
mockFindAll('profile', 1);
FactoryGuy.store.findAll('profile').then(function(profiles) {
assert.ok(profiles.get('firstObject.camelCaseDescription') === 'textGoesHere');
assert.ok(profiles.get('firstObject.snake_case_description') === 'text_goes_here');
done();
});
});
});
test("asking for no return records", function(assert) {
run(() => {
let done = assert.async();
mockFindAll('user', 0);
FactoryGuy.store.findAll('user').then(function(profiles) {
assert.ok(profiles.get('length') === 0);
done();
});
});
});
test("with fixture options", function(assert) {
run(() => {
let done = assert.async();
mockFindAll('profile', 2, {description: 'dude'});
FactoryGuy.store.findAll('profile').then(function(profiles) {
assert.ok(profiles.get('length') === 2);
assert.ok(profiles.get('firstObject.description') === 'dude');
done();
});
});
});
test("with traits", function(assert) {
let done = assert.async();
mockFindAll('profile', 2, 'goofy_description');
FactoryGuy.store.findAll('profile').then(function(profiles) {
assert.ok(profiles.get('length') === 2);
assert.ok(profiles.get('firstObject.description') === 'goofy');
done();
});
});
test("with traits and extra options", function(assert) {
let done = assert.async();
mockFindAll('profile', 2, 'goofy_description', {description: 'dude'});
FactoryGuy.store.findAll('profile').then(function(profiles) {
assert.ok(profiles.get('length') === 2);
assert.ok(profiles.get('firstObject.description') === 'dude');
done();
});
});
};
//////// mockFindAll with sideloading /////////
SharedBehavior.mockFindAllSideloadingTests = function() {
module('#mockFindAll | sideloading', function() {
test("with belongsTo association", function(assert) {
run(() => {
let done = assert.async();
mockFindAll('profile', 2, 'with_company', 'with_bat_man');
FactoryGuy.store.findAll('profile').then(function(profiles) {
assert.ok(profiles.get('length') === 2);
assert.ok(profiles.get('firstObject.company.name') === 'Silly corp');
assert.ok(profiles.get('lastObject.superHero.name') === 'BatMan');
done();
});
});
});
test("with hasMany association", function(assert) {
run(() => {
let done = assert.async();
mockFindAll('user', 2, 'with_hats');
FactoryGuy.store.findAll('user').then(function(users) {
assert.ok(users.get('length') === 2);
assert.ok(A(users.get('lastObject.hats')).mapBy('type') + '' === ['BigHat', 'BigHat'] + '');
assert.ok(A(users.get('lastObject.hats')).mapBy('id') + '' === [3, 4] + '');
done();
});
});
});
test("with diverse models", function(assert) {
run(() => {
let done = assert.async();
mockFindAll('profile', 'goofy_description', {description: 'foo'}, ['goofy_description', {aBooleanField: true}]);
FactoryGuy.store.findAll('profile').then(function(profiles) {
assert.ok(profiles.get('length') === 3);
assert.ok(A(profiles).objectAt(0).get('description') === 'goofy');
assert.ok(A(profiles).objectAt(0).get('aBooleanField') === false);
assert.ok(A(profiles).objectAt(1).get('description') === 'foo');
assert.ok(A(profiles).objectAt(1).get('aBooleanField') === false);
assert.ok(A(profiles).objectAt(2).get('description') === 'goofy');
assert.ok(A(profiles).objectAt(2).get('aBooleanField') === true);
done();
});
});
});
test("using returns with json", function(assert) {
run(() => {
let done = assert.async();
let json = buildList('profile', 'with_company', 'with_bat_man');
mockFindAll('profile').returns({json});
FactoryGuy.store.findAll('profile').then(function(profiles) {
assert.ok(profiles.get('firstObject.company.name') === 'Silly corp');
assert.ok(profiles.get('lastObject.superHero.name') === 'BatMan');
done();
});
});
});
test("using returns with model", function(assert) {
let done = assert.async();
let models = makeList('profile', 'with_company', 'with_bat_man');
mockFindAll('profile').returns({models});
FactoryGuy.store.findAll('profile').then(function(profiles) {
assert.ok(profiles.get('firstObject.company.name') === 'Silly corp');
assert.ok(profiles.get('lastObject.superHero.name') === 'BatMan');
assert.equal(FactoryGuy.store.peekAll('profile').get('content').length, 2, "does not make new profiles");
done();
});
});
// test("handles include params", function(assert) {
// run(()=> {
// let done = assert.async();
//
// let json = buildList('profile', 'with_company');
// mockFindAll('profile').withParams({include: 'company'}).returns({ json });
//
// FactoryGuy.store.findAll('profile', {include: 'company'}).then(function(profiles) {
// assert.ok(profiles.get('firstObject.company.name') === 'Silly corp');
// done();
// });
// });
// });
});
};
SharedBehavior.mockFindAllEmbeddedTests = function() {
module('#mockFindAll | embedded', function() {
test("belongsTo", function(assert) {
run(() => {
let done = assert.async();
mockFindAll('comic-book', 2, 'marvel');
FactoryGuy.store.findAll('comic-book').then(function(comics) {
assert.ok(comics.mapBy('name') + '' === ['Comic Times #1', 'Comic Times #2'] + '');
assert.ok(comics.mapBy('company.name') + '' === ['Marvel Comics', 'Marvel Comics'] + '');
done();
});
});
});
test("hasMany", function(assert) {
run(() => {
let done = assert.async();
mockFindAll('comic-book', 2, 'with_bad_guys');
FactoryGuy.store.findAll('comic-book').then(function(comics) {
assert.ok(comics.mapBy('name') + '' === ['Comic Times #1', 'Comic Times #2'] + '');
assert.ok(comics.get('firstObject.characters').mapBy('name') + '' === ['BadGuy#1', 'BadGuy#2'] + '');
assert.ok(comics.get('lastObject.characters').mapBy('name') + '' === ['BadGuy#3', 'BadGuy#4'] + '');
done();
});
});
});
});
};
/////// mockQuery //////////
SharedBehavior.mockQueryTests = function() {
test("not using returns", function(assert) {
run(() => {
let done = assert.async();
mockQuery('user', {name: 'Bob'});
FactoryGuy.store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 0, "nothing returned");
done();
});
});
});
test("with no parameters matches query with any parameters", function(assert) {
var done = assert.async();
mockQuery('user');
FactoryGuy.store.query('user', {name: 'Bob'})
.then(() => {
assert.ok(true);
done();
});
});
test("using fails makes the request fail", function(assert) {
run(() => {
let done = assert.async();
let errors = {errors: {name: ['wrong']}};
let mock = mockQuery('user').fails({status: 422, response: errors});
FactoryGuy.store.query('user', {})
.catch(() => {
assert.equal(mock.timesCalled, 1);
assert.ok(true);
done();
});
});
});
test("using returns with headers adds the headers to the response", function(assert) {
var done = assert.async();
const queryParams = {name: 'MyCompany'};
const handler = mockQuery('company', queryParams);
handler.returns({headers: {'X-Testing': 'absolutely'}});
$(document).ajaxComplete(function(event, xhr) {
assert.equal(xhr.getResponseHeader('X-Testing'), 'absolutely');
$(document).off('ajaxComplete');
done();
});
FactoryGuy.store.query('company', queryParams);
});
test("using nested search params", function(assert) {
run(() => {
let done = assert.async();
let models = makeList('company', 2);
mockQuery('company', {name: {like: 'Dude*'}}).returns({models});
FactoryGuy.store.query('company', {name: {like: 'Dude*'}}).then(function(companies) {
assert.deepEqual(A(companies).mapBy('id'), A(models).mapBy('id'));
done();
});
});
});
test("using returns with empty array", function(assert) {
run(() => {
let done = assert.async();
mockQuery('user', {name: 'Bob'}).returns({models: []});
FactoryGuy.store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 0, "nothing returned");
done();
});
});
});
test("using returns with model instances returns your models, and does not create new ones", function(assert) {
run(() => {
let done = assert.async();
let bob = make('user');
mockQuery('user', {name: 'Bob'}).returns({models: [bob]});
FactoryGuy.store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 1);
assert.equal(users.get('firstObject'), bob);
assert.equal(FactoryGuy.store.peekAll('user').get('content').length, 1, "does not make another user");
done();
});
});
});
test("using returns with model instances having hasMany models", async function(assert) {
let models = makeList('user', 2, 'with_hats');
mockQuery('user', {name: 'Bob'}).returns({models});
assert.equal(FactoryGuy.store.peekAll('user').get('content.length'), 2, 'start out with 2 instances');
let users = await Ember.run(async () => FactoryGuy.store.query('user', {name: 'Bob'}));
assert.equal(users.get('length'), 2);
assert.equal(users.get('firstObject.name'), 'User1');
assert.equal(users.get('firstObject.hats.length'), 2);
assert.equal(users.get('lastObject.name'), 'User2');
assert.equal(FactoryGuy.store.peekAll('user').get('content.length'), 2, 'no new instances created');
});
test("using returns with model instances with hasMany and belongsTo relationships", function(assert) {
run(() => {
let done = assert.async();
let models = makeList('company', 2, 'with_projects', 'with_profile');
mockQuery('company', {name: 'Dude Company'}).returns({models});
assert.equal(FactoryGuy.store.peekAll('company').get('content.length'), 2, 'start out with 2 instances');
FactoryGuy.store.query('company', {name: 'Dude Company'}).then(function(companies) {
assert.equal(companies.get('length'), 2);
assert.ok(companies.get('firstObject.profile') instanceof Profile);
assert.equal(companies.get('firstObject.projects.length'), 2);
assert.ok(companies.get('lastObject.profile') instanceof Profile);
assert.equal(companies.get('lastObject.projects.length'), 2);
assert.equal(FactoryGuy.store.peekAll('company').get('content.length'), 2, 'no new instances created');
done();
});
});
});
test("using returns with json returns and creates models", function(assert) {
run(() => {
let done = assert.async();
let json = buildList('user', 1);
mockQuery('user', {name: 'Bob'}).returns({json});
FactoryGuy.store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 1);
// makes the user after getting query response
assert.equal(FactoryGuy.store.peekAll('user').get('content').length, 1);
done();
});
});
});
test("using returns with model ids returns those models and does not create new ones", function(assert) {
run(() => {
let done = assert.async();
let bob = make('user');
let ids = [bob.id];
mockQuery('user', {name: 'Bob'}).returns({ids});
FactoryGuy.store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 1);
assert.equal(users.get('firstObject'), bob);
// does not create a new model
assert.equal(FactoryGuy.store.peekAll('user').get('content').length, 1);
done();
});
});
});
// test created for issue #143
test("reuse mock query to first return nothing then use returns to return something", function(assert) {
run(() => {
let done = assert.async();
let store = FactoryGuy.store;
let bobQueryHander = mockQuery('user', {name: 'Bob'});
store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 0);
mockCreate('user', {name: 'Bob'});
store.createRecord('user', {name: 'Bob'}).save().then(function(user) {
bobQueryHander.returns({models: [user]});
store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 1);
done();
});
});
});
});
});
test("reusing mock query using returns with different models and different params returns different results", function(assert) {
run(() => {
let done = assert.async();
let companies1 = makeList('company', 2);
mockQuery('company', {name: 'Dude'}).returns({models: companies1});
let companies2 = makeList('company', 2);
mockQuery('company', {type: 'Small'}).returns({models: companies2});
FactoryGuy.store.query('company', {name: 'Dude'}).then(function(companies) {
assert.equal(A(companies).mapBy('id') + '', A(companies1).mapBy('id') + '');
FactoryGuy.store.query('company', {type: 'Small'}).then(function(companies) {
assert.equal(A(companies).mapBy('id') + '', A(companies2).mapBy('id') + '');
done();
});
});
});
});
test("using returns with same json and different query params returns same results", function(assert) {
run(() => {
let done = assert.async();
let expectedAssertions = 2;
function finalizeTest() {
--expectedAssertions;
if (expectedAssertions === 0) {
done();
}
}
let companies = makeList('company', 2);
mockQuery('company', {name: 'Dude'}).returns({models: companies});
mockQuery('company', {type: 'Small', name: 'Dude'}).returns({models: companies});
let request1 = FactoryGuy.store.query('company', {name: 'Dude'});
let request2 = FactoryGuy.store.query('company', {type: 'Small', name: 'Dude'});
request1.then(function(returnedCompanies) {
assert.equal(A(companies).mapBy('id') + '', A(returnedCompanies).mapBy('id') + '');
finalizeTest();
});
request2.then(function(returnedCompanies) {
assert.equal(A(companies).mapBy('id') + '', A(returnedCompanies).mapBy('id') + '');
finalizeTest();
});
});
});
test("reusing mock query using returns with different models and withParams with different params returns different results", function(assert) {
run(() => {
let done = assert.async();
let companies1 = makeList('company', 2);
let companies2 = makeList('company', 2);
let queryHandler = mockQuery('company', {name: 'Dude'}).returns({models: companies1});
FactoryGuy.store.query('company', {name: 'Dude'}).then(function(companies) {
assert.equal(A(companies).mapBy('id') + '', A(companies1).mapBy('id') + '');
queryHandler.withParams({type: 'Small'}).returns({models: companies2});
FactoryGuy.store.query('company', {type: 'Small'}).then(function(companies) {
assert.equal(A(companies).mapBy('id') + '', A(companies2).mapBy('id') + '');
done();
});
});
});
});
test("mock query with withSomeParams captures the query even if it contains additional params", function(assert) {
run(() => {
let done = assert.async();
let companies1 = makeList('company', 2);
let companies2 = makeList('company', 2);
let matchQueryHandler = mockQuery('company').withSomeParams({name: 'Dude'}).returns({models: companies1});
let allQueryHandler = mockQuery('company').returns({models: companies2});
FactoryGuy.store.query('company', {name: 'Dude', page: 1}).then(function(companies) {
assert.equal(A(companies).mapBy('id') + '', A(companies1).mapBy('id') + '');
assert.equal(matchQueryHandler.timesCalled, 1);
FactoryGuy.store.query('company', {name: 'Other', page: 1}).then(function(companies) {
assert.equal(A(companies).mapBy('id') + '', A(companies2).mapBy('id') + '');
assert.equal(allQueryHandler.timesCalled, 1);
done();
});
});
});
});
};
SharedBehavior.mockQueryMetaTests = function() {
module('#mockQuery | meta', function() {
test("with proxy payload", function(assert) {
run(() => {
let done = assert.async();
let json1 = buildList('profile', 2).add({meta: {previous: '/profiles?page=1', next: '/profiles?page=3'}});
let json2 = buildList('profile', 2).add({meta: {previous: '/profiles?page=2', next: '/profiles?page=4'}});
mockQuery('profile', {page: 2}).returns({json: json1});
mockQuery('profile', {page: 3}).returns({json: json2});
FactoryGuy.store.query('profile', {page: 2}).then(function(profiles) {
assert.deepEqual(profiles.mapBy('id'), ["1", "2"]);
assert.ok(isEquivalent(profiles.get('meta'), {previous: '/profiles?page=1', next: '/profiles?page=3'}));
FactoryGuy.store.query('profile', {page: 3}).then(function(profiles2) {
assert.deepEqual(profiles2.mapBy('id'), ["3", "4"]);
assert.ok(isEquivalent(profiles2.get('meta'), {previous: '/profiles?page=2', next: '/profiles?page=4'}));
done();
});
});
});
});
});
};
/////// mockQueryRecord //////////
SharedBehavior.mockQueryRecordTests = function() {
test("when returning no result", async function(assert) {
mockQueryRecord('user');
FactoryGuy.store.queryRecord('user', {});
assert.ok(true);
});