ember-data-factory-guy 0.9.2 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +38 -8
- data/bower.json +1 -1
- data/dist/amd/factory-guy.js +94 -20
- data/dist/ember-data-factory-guy.js +95 -21
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/ember-data-factory-guy.gemspec +5 -1
- data/package.json +1 -1
- data/src/factory_guy.js +3 -1
- data/src/factory_guy_test_mixin.js +39 -13
- data/src/store.js +53 -7
- data/tests/factory_guy_test.js +1 -1
- data/tests/factory_guy_test_mixin_test.js +44 -6
- data/tests/fixture_adapter_factory_test.js +112 -60
- data/tests/index.html +1 -1
- data/tests/store_test.js +198 -0
- data/tests/support/factories/company_factory.js +4 -1
- data/tests/support/factories/property_factory.js +6 -1
- data/tests/support/factories/user_factory.js +3 -0
- data/tests/support/models/person.js +3 -1
- data/tests/support/models/user.js +1 -1
- data/tests/support/test_helper.js +3 -3
- data/tests/test_setup.js +17 -2
- data/vendor/assets/javascripts/ember_data_factory_guy.js +98 -23
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7bc82d125a83024abd95b12c5f11892c76047002
|
4
|
+
data.tar.gz: e64f296ca9c2e7d82fa4d62d1e65356874ca6c5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8e7451735f51c0511124daf19add6710e490f904e48e61eb44b22761585bd7d29e78eed73a3468a2caae46047bc8eb1f0b835cca3b07c79d87bf106647609ec
|
7
|
+
data.tar.gz: ba2d24705c6e111e00ae752a0a16dc04e94981ca2e742314f66dbe76a5c2dbdf5cb9946f2d0076e0b1ff820b1f137b473191a4ef43f91a95f3f3d6cd3c488018
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ of ember-data-factory-guy.
|
|
10
10
|
- 0.6.4 -> ember-data-1.0.0-beta.8 and under
|
11
11
|
- 0.7.1.1 -> ember-data-1.0.0-beta.10
|
12
12
|
- 0.8.6 -> ember-data-1.0.0-beta.11
|
13
|
-
- 0.9.
|
13
|
+
- 0.9.2 -> ember-data-1.0.0-beta.12
|
14
14
|
|
15
15
|
**Support for fixture adapter is currently kinda broken.**
|
16
16
|
|
@@ -39,7 +39,7 @@ gem 'ember-data-factory-guy', group: test
|
|
39
39
|
or for particular version:
|
40
40
|
|
41
41
|
```ruby
|
42
|
-
gem 'ember-data-factory-guy', '0.9.
|
42
|
+
gem 'ember-data-factory-guy', '0.9.2', group: test
|
43
43
|
```
|
44
44
|
|
45
45
|
then:
|
@@ -74,7 +74,7 @@ or for particular version:
|
|
74
74
|
"dependencies": {
|
75
75
|
"foo-dependency": "latest",
|
76
76
|
"other-foo-dependency": "latest",
|
77
|
-
"ember-data-factory-guy": "0.9.
|
77
|
+
"ember-data-factory-guy": "0.9.2"
|
78
78
|
}
|
79
79
|
```
|
80
80
|
|
@@ -114,7 +114,8 @@ You do not have to use the DS.FixtureAdapter. But if you do choose to use the F
|
|
114
114
|
which does not run any faster, and does not handle associations as elegantly
|
115
115
|
( and in some cases not at all ), you may run into problems with accessing associations.
|
116
116
|
|
117
|
-
Error: Assertion Failed: You looked up the 'projects' relationship on '<User:ember379:1>'
|
117
|
+
Error: Assertion Failed: You looked up the 'projects' relationship on '<User:ember379:1>'
|
118
|
+
but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (`DS.hasMany({ async: true })`)
|
118
119
|
|
119
120
|
If you do get these types of errors try requiring the factory_guy_has_many.js file
|
120
121
|
( located in dist dir and vendor dir ) AFTER you require ember-data,
|
@@ -226,9 +227,10 @@ the store is looking up the correct model type name
|
|
226
227
|
- Can add attributes with traits ( see traits section )
|
227
228
|
|
228
229
|
```javascript
|
229
|
-
// First set the store on FactoryGuy. You don't have to do this step manually
|
230
|
-
// FactoryGuyTestHelperMixin since this is done for you in the setup
|
231
|
-
// store lookup assumes you have a namespace for your Ember
|
230
|
+
// First set the store on FactoryGuy. You don't have to do this step manually
|
231
|
+
// if you use FactoryGuyTestHelperMixin since this is done for you in the setup
|
232
|
+
// method. The following store lookup assumes you have a namespace for your Ember
|
233
|
+
// app named 'App'.
|
232
234
|
var store = App.__container__.lookup('store:main');
|
233
235
|
FactoryGuy.setStore(store);
|
234
236
|
|
@@ -631,6 +633,9 @@ tests run as shown in the previous section (Using FactoryGuyTestMixin)**
|
|
631
633
|
|
632
634
|
##### handleFindQuery
|
633
635
|
- for dealing with finding all records for a type of model with query parameters.
|
636
|
+
- can pass in fixture json, model instances or empty array
|
637
|
+
|
638
|
+
*Passing in array of fixture json*
|
634
639
|
|
635
640
|
```javascript
|
636
641
|
// First build json for the instances you want 'returned' in your query.
|
@@ -641,10 +646,35 @@ tests run as shown in the previous section (Using FactoryGuyTestMixin)**
|
|
641
646
|
testHelper.handleFindQuery('user', ['name', 'age'], usersJson);
|
642
647
|
|
643
648
|
store.findQuery('user', {name:'Bob', age: 10}}).then(function(userInstances){
|
644
|
-
|
649
|
+
// userInstances returned are created from the usersJson that you passed in
|
645
650
|
})
|
646
651
|
```
|
647
652
|
|
653
|
+
*Passing in array of model instances*
|
654
|
+
|
655
|
+
```js
|
656
|
+
// Create model instances
|
657
|
+
var users = FactoryGuy.makeList('user', 2, 'with_hats');
|
658
|
+
|
659
|
+
// Pass in the array of model instances as last argument
|
660
|
+
testHelper.handleFindQuery('user', ['name', 'age'], users);
|
661
|
+
|
662
|
+
store.findQuery('user', {name:'Bob', age: 10}}).then(function(userInstances){
|
663
|
+
/// userInstances will be the same of the users that were passed in
|
664
|
+
})
|
665
|
+
```
|
666
|
+
|
667
|
+
*Passing in nothing for last argument*
|
668
|
+
|
669
|
+
```js
|
670
|
+
// This simulates a query that returns no results
|
671
|
+
testHelper.handleFindQuery('user', ['age']);
|
672
|
+
|
673
|
+
store.findQuery('user', {age: 10000}}).then(function(userInstances){
|
674
|
+
/// userInstances will be empty
|
675
|
+
})
|
676
|
+
```
|
677
|
+
|
648
678
|
|
649
679
|
##### handleCreate
|
650
680
|
- options
|
data/bower.json
CHANGED
data/dist/amd/factory-guy.js
CHANGED
@@ -467,7 +467,9 @@ var FactoryGuy = {
|
|
467
467
|
|
468
468
|
if (store.usingFixtureAdapter()) {
|
469
469
|
store.setAssociationsForFixtureAdapter(modelType, modelName, fixture);
|
470
|
-
|
470
|
+
fixture = FactoryGuy.pushFixture(modelType, fixture);
|
471
|
+
store.loadModelForFixtureAdapter(modelType, fixture);
|
472
|
+
return fixture;
|
471
473
|
} else {
|
472
474
|
return store.makeModel(modelType, fixture);
|
473
475
|
}
|
@@ -648,9 +650,11 @@ var FactoryGuy = {
|
|
648
650
|
setAssociationsForFixtureAdapter: function (modelType, modelName, fixture) {
|
649
651
|
var self = this;
|
650
652
|
var adapter = this.adapterFor('application');
|
653
|
+
|
651
654
|
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship, name) {
|
655
|
+
var hasManyRelation, belongsToRecord;
|
652
656
|
if (relationship.kind == 'hasMany') {
|
653
|
-
|
657
|
+
hasManyRelation = fixture[relationship.key];
|
654
658
|
if (hasManyRelation) {
|
655
659
|
$.each(fixture[relationship.key], function (index, object) {
|
656
660
|
// used to require that the relationship was set by id,
|
@@ -658,17 +662,19 @@ var FactoryGuy = {
|
|
658
662
|
// normalize that back to the id
|
659
663
|
var id = object;
|
660
664
|
if (Ember.typeOf(object) == 'object') {
|
665
|
+
FactoryGuy.pushFixture(relationship.type, object);
|
661
666
|
id = object.id;
|
662
667
|
hasManyRelation[index] = id;
|
663
668
|
}
|
664
669
|
var hasManyfixtures = adapter.fixturesForType(relationship.type);
|
665
|
-
var
|
666
|
-
|
670
|
+
var hasManyFixture = adapter.findFixtureById(hasManyfixtures, id);
|
671
|
+
hasManyFixture[modelName] = fixture.id;
|
672
|
+
self.loadModelForFixtureAdapter(relationship.type, hasManyFixture);
|
667
673
|
});
|
668
674
|
}
|
669
675
|
}
|
670
676
|
if (relationship.kind == 'belongsTo') {
|
671
|
-
|
677
|
+
belongsToRecord = fixture[relationship.key];
|
672
678
|
if (belongsToRecord) {
|
673
679
|
if (typeof belongsToRecord == 'object') {
|
674
680
|
FactoryGuy.pushFixture(relationship.type, belongsToRecord);
|
@@ -681,10 +687,29 @@ var FactoryGuy = {
|
|
681
687
|
belongsTofixture[hasManyName] = [];
|
682
688
|
}
|
683
689
|
belongsTofixture[hasManyName].push(fixture.id);
|
690
|
+
self.loadModelForFixtureAdapter(relationship.type, belongsTofixture);
|
684
691
|
}
|
685
692
|
}
|
686
693
|
});
|
687
694
|
},
|
695
|
+
|
696
|
+
loadModelForFixtureAdapter: function(modelType, fixture) {
|
697
|
+
var storeModel = this.getById(modelType, fixture.id),
|
698
|
+
that = this;
|
699
|
+
if (!Ember.isPresent(storeModel) || storeModel.get('isEmpty')) {
|
700
|
+
Ember.run(function () {
|
701
|
+
var dup = Ember.copy(fixture, true);
|
702
|
+
that.push(modelType, fixture);
|
703
|
+
//replace relationships back to ids instead of built ember objects
|
704
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship, name) {
|
705
|
+
if(fixture[relationship.key]) {
|
706
|
+
fixture[relationship.key] = dup[relationship.key];
|
707
|
+
}
|
708
|
+
});
|
709
|
+
});
|
710
|
+
}
|
711
|
+
},
|
712
|
+
|
688
713
|
/**
|
689
714
|
Before pushing the fixture to the store, do some preprocessing. Descend into the tree
|
690
715
|
of object data, and convert child objects to record instances recursively.
|
@@ -793,9 +818,32 @@ var FactoryGuy = {
|
|
793
818
|
@param {Object} payload
|
794
819
|
*/
|
795
820
|
pushPayload: function (type, payload) {
|
821
|
+
var typeName, model;
|
822
|
+
|
796
823
|
if (this.usingFixtureAdapter()) {
|
797
|
-
|
798
|
-
|
824
|
+
if (Ember.typeOf(type) === 'string' && Ember.isPresent(payload) && Ember.isPresent(payload.id)){
|
825
|
+
//pushPayload('user', {id:..})
|
826
|
+
model = this.modelFor(type);
|
827
|
+
FactoryGuy.pushFixture(model, payload);
|
828
|
+
this.push(model, Ember.copy(payload, true));
|
829
|
+
} else if(Ember.typeOf(type) === 'object' || Ember.typeOf(payload) === 'object') {
|
830
|
+
//pushPayload({users: {id:..}}) OR pushPayload('user', {users: {id:..}})
|
831
|
+
if(Ember.isBlank(payload)){
|
832
|
+
payload = type;
|
833
|
+
}
|
834
|
+
|
835
|
+
for (var prop in payload) {
|
836
|
+
typeName = Ember.String.camelize(Ember.String.singularize(prop));
|
837
|
+
model = this.modelFor(typeName);
|
838
|
+
|
839
|
+
this.pushMany(model, Ember.makeArray( Ember.copy(payload[prop], true) ));
|
840
|
+
Ember.ArrayPolyfills.forEach.call(Ember.makeArray(payload[prop]), function(hash) {
|
841
|
+
FactoryGuy.pushFixture(model, hash);
|
842
|
+
}, this);
|
843
|
+
}
|
844
|
+
} else {
|
845
|
+
throw new Ember.Error('Assertion Failed: You cannot use `store#pushPayload` with this method signature pushPayload(' + type + ',' + payload + ')');
|
846
|
+
}
|
799
847
|
} else {
|
800
848
|
this._super(type, payload);
|
801
849
|
}
|
@@ -882,6 +930,17 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
882
930
|
Handling ajax GET for finding all records for a type of model.
|
883
931
|
You can mock failed find by passing in success argument as false.
|
884
932
|
|
933
|
+
```js
|
934
|
+
// Pass in the parameters you would normally pass into FactoryGuy.makeList,
|
935
|
+
// like fixture name, number of fixtures to make, and optional traits,
|
936
|
+
// or fixture options
|
937
|
+
testHelper.handleFindMany('user', 2, 'with_hats');
|
938
|
+
|
939
|
+
store.find('user').then(function(users){
|
940
|
+
|
941
|
+
});
|
942
|
+
```
|
943
|
+
|
885
944
|
@param {String} name name of the fixture ( or model ) to find
|
886
945
|
@param {Number} number number of fixtures to create
|
887
946
|
@param {String} trait optional traits (one or more)
|
@@ -889,39 +948,54 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
889
948
|
*/
|
890
949
|
handleFindMany: function () {
|
891
950
|
// make the records and load them in the store
|
892
|
-
FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
951
|
+
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
893
952
|
var name = arguments[0];
|
894
953
|
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
895
954
|
var responseJson = {};
|
896
|
-
|
955
|
+
var json = records.map(function(record) {return record.toJSON({includeId: true})});
|
956
|
+
responseJson[modelName.pluralize()] = json;
|
897
957
|
var url = this.buildURL(modelName);
|
898
|
-
// mock the ajax call, but return nothing, since the records will be
|
899
|
-
// retrieved from the store where they were just loaded above
|
900
958
|
this.stubEndpointForHttpRequest(url, responseJson);
|
901
959
|
},
|
902
960
|
/**
|
903
961
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
904
962
|
|
963
|
+
First variation = pass in model instances
|
905
964
|
```js
|
906
|
-
// First build json for the instances you want 'returned' in your query.
|
907
|
-
var usersJson = FactoryGuy.buildList('user', 2);
|
908
965
|
|
909
|
-
//
|
910
|
-
|
911
|
-
|
966
|
+
// Create model instances
|
967
|
+
var users = FactoryGuy.makeList('user', 2, 'with_hats');
|
968
|
+
|
969
|
+
// Pass in the array of model instances as last argument
|
970
|
+
testHelper.handleFindQuery('user', ['name', 'age'], users);
|
912
971
|
|
913
972
|
store.findQuery('user', {name:'Bob', age: 10}}).then(function(userInstances){
|
914
|
-
/// userInstances
|
973
|
+
/// userInstances will be the same of the users that were passed in
|
915
974
|
})
|
916
975
|
```
|
917
976
|
|
918
|
-
|
977
|
+
Third variation - pass in nothing for last argument
|
978
|
+
```js
|
979
|
+
// This simulates a query that returns no results
|
980
|
+
testHelper.handleFindQuery('user', ['age']);
|
981
|
+
|
982
|
+
store.findQuery('user', {age: 10000}}).then(function(userInstances){
|
983
|
+
/// userInstances will be empty
|
984
|
+
})
|
985
|
+
```
|
919
986
|
|
920
987
|
@param {String} modelName name of the mode like 'user' for User model type
|
921
988
|
@param {String} searchParams the parameters that will be queried
|
922
|
-
@param {
|
989
|
+
@param {Array} array of DS.Model records to be 'returned' by query
|
923
990
|
*/
|
924
|
-
handleFindQuery: function (modelName, searchParams,
|
991
|
+
handleFindQuery: function (modelName, searchParams, records) {
|
992
|
+
Ember.assert('The second argument of searchParams must be an array',Em.typeOf(searchParams) == 'array')
|
993
|
+
if (records) {
|
994
|
+
Ember.assert('The third argument ( records ) must be an array - found type:' + Em.typeOf(records), Em.typeOf(records) == 'array')
|
995
|
+
} else {
|
996
|
+
records = []
|
997
|
+
}
|
998
|
+
var json = records.map(function(record) {return record.toJSON({includeId: true})})
|
925
999
|
var responseJson = {};
|
926
1000
|
responseJson[modelName.pluralize()] = json;
|
927
1001
|
var url = this.buildURL(modelName);
|
@@ -462,7 +462,9 @@ var FactoryGuy = {
|
|
462
462
|
|
463
463
|
if (store.usingFixtureAdapter()) {
|
464
464
|
store.setAssociationsForFixtureAdapter(modelType, modelName, fixture);
|
465
|
-
|
465
|
+
fixture = FactoryGuy.pushFixture(modelType, fixture);
|
466
|
+
store.loadModelForFixtureAdapter(modelType, fixture);
|
467
|
+
return fixture;
|
466
468
|
} else {
|
467
469
|
return store.makeModel(modelType, fixture);
|
468
470
|
}
|
@@ -594,7 +596,7 @@ var FactoryGuy = {
|
|
594
596
|
* Most of the work of making the model from the json fixture is going on here.
|
595
597
|
* @param modelType
|
596
598
|
* @param fixture
|
597
|
-
* @returns {
|
599
|
+
* @returns {DS.Model} instance of DS.Model
|
598
600
|
*/
|
599
601
|
makeModel: function (modelType, fixture) {
|
600
602
|
var store = this,
|
@@ -643,9 +645,11 @@ var FactoryGuy = {
|
|
643
645
|
setAssociationsForFixtureAdapter: function (modelType, modelName, fixture) {
|
644
646
|
var self = this;
|
645
647
|
var adapter = this.adapterFor('application');
|
648
|
+
|
646
649
|
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship, name) {
|
650
|
+
var hasManyRelation, belongsToRecord;
|
647
651
|
if (relationship.kind == 'hasMany') {
|
648
|
-
|
652
|
+
hasManyRelation = fixture[relationship.key];
|
649
653
|
if (hasManyRelation) {
|
650
654
|
$.each(fixture[relationship.key], function (index, object) {
|
651
655
|
// used to require that the relationship was set by id,
|
@@ -653,17 +657,19 @@ var FactoryGuy = {
|
|
653
657
|
// normalize that back to the id
|
654
658
|
var id = object;
|
655
659
|
if (Ember.typeOf(object) == 'object') {
|
660
|
+
FactoryGuy.pushFixture(relationship.type, object);
|
656
661
|
id = object.id;
|
657
662
|
hasManyRelation[index] = id;
|
658
663
|
}
|
659
664
|
var hasManyfixtures = adapter.fixturesForType(relationship.type);
|
660
|
-
var
|
661
|
-
|
665
|
+
var hasManyFixture = adapter.findFixtureById(hasManyfixtures, id);
|
666
|
+
hasManyFixture[modelName] = fixture.id;
|
667
|
+
self.loadModelForFixtureAdapter(relationship.type, hasManyFixture);
|
662
668
|
});
|
663
669
|
}
|
664
670
|
}
|
665
671
|
if (relationship.kind == 'belongsTo') {
|
666
|
-
|
672
|
+
belongsToRecord = fixture[relationship.key];
|
667
673
|
if (belongsToRecord) {
|
668
674
|
if (typeof belongsToRecord == 'object') {
|
669
675
|
FactoryGuy.pushFixture(relationship.type, belongsToRecord);
|
@@ -676,10 +682,29 @@ var FactoryGuy = {
|
|
676
682
|
belongsTofixture[hasManyName] = [];
|
677
683
|
}
|
678
684
|
belongsTofixture[hasManyName].push(fixture.id);
|
685
|
+
self.loadModelForFixtureAdapter(relationship.type, belongsTofixture);
|
679
686
|
}
|
680
687
|
}
|
681
688
|
});
|
682
689
|
},
|
690
|
+
|
691
|
+
loadModelForFixtureAdapter: function(modelType, fixture) {
|
692
|
+
var storeModel = this.getById(modelType, fixture.id),
|
693
|
+
that = this;
|
694
|
+
if (!Ember.isPresent(storeModel) || storeModel.get('isEmpty')) {
|
695
|
+
Ember.run(function () {
|
696
|
+
var dup = Ember.copy(fixture, true);
|
697
|
+
that.push(modelType, fixture);
|
698
|
+
//replace relationships back to ids instead of built ember objects
|
699
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship, name) {
|
700
|
+
if(fixture[relationship.key]) {
|
701
|
+
fixture[relationship.key] = dup[relationship.key];
|
702
|
+
}
|
703
|
+
});
|
704
|
+
});
|
705
|
+
}
|
706
|
+
},
|
707
|
+
|
683
708
|
/**
|
684
709
|
Before pushing the fixture to the store, do some preprocessing. Descend into the tree
|
685
710
|
of object data, and convert child objects to record instances recursively.
|
@@ -788,9 +813,32 @@ var FactoryGuy = {
|
|
788
813
|
@param {Object} payload
|
789
814
|
*/
|
790
815
|
pushPayload: function (type, payload) {
|
816
|
+
var typeName, model;
|
817
|
+
|
791
818
|
if (this.usingFixtureAdapter()) {
|
792
|
-
|
793
|
-
|
819
|
+
if (Ember.typeOf(type) === 'string' && Ember.isPresent(payload) && Ember.isPresent(payload.id)){
|
820
|
+
//pushPayload('user', {id:..})
|
821
|
+
model = this.modelFor(type);
|
822
|
+
FactoryGuy.pushFixture(model, payload);
|
823
|
+
this.push(model, Ember.copy(payload, true));
|
824
|
+
} else if(Ember.typeOf(type) === 'object' || Ember.typeOf(payload) === 'object') {
|
825
|
+
//pushPayload({users: {id:..}}) OR pushPayload('user', {users: {id:..}})
|
826
|
+
if(Ember.isBlank(payload)){
|
827
|
+
payload = type;
|
828
|
+
}
|
829
|
+
|
830
|
+
for (var prop in payload) {
|
831
|
+
typeName = Ember.String.camelize(Ember.String.singularize(prop));
|
832
|
+
model = this.modelFor(typeName);
|
833
|
+
|
834
|
+
this.pushMany(model, Ember.makeArray( Ember.copy(payload[prop], true) ));
|
835
|
+
Ember.ArrayPolyfills.forEach.call(Ember.makeArray(payload[prop]), function(hash) {
|
836
|
+
FactoryGuy.pushFixture(model, hash);
|
837
|
+
}, this);
|
838
|
+
}
|
839
|
+
} else {
|
840
|
+
throw new Ember.Error('Assertion Failed: You cannot use `store#pushPayload` with this method signature pushPayload(' + type + ',' + payload + ')');
|
841
|
+
}
|
794
842
|
} else {
|
795
843
|
this._super(type, payload);
|
796
844
|
}
|
@@ -877,6 +925,17 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
877
925
|
Handling ajax GET for finding all records for a type of model.
|
878
926
|
You can mock failed find by passing in success argument as false.
|
879
927
|
|
928
|
+
```js
|
929
|
+
// Pass in the parameters you would normally pass into FactoryGuy.makeList,
|
930
|
+
// like fixture name, number of fixtures to make, and optional traits,
|
931
|
+
// or fixture options
|
932
|
+
testHelper.handleFindMany('user', 2, 'with_hats');
|
933
|
+
|
934
|
+
store.find('user').then(function(users){
|
935
|
+
|
936
|
+
});
|
937
|
+
```
|
938
|
+
|
880
939
|
@param {String} name name of the fixture ( or model ) to find
|
881
940
|
@param {Number} number number of fixtures to create
|
882
941
|
@param {String} trait optional traits (one or more)
|
@@ -884,39 +943,54 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
884
943
|
*/
|
885
944
|
handleFindMany: function () {
|
886
945
|
// make the records and load them in the store
|
887
|
-
FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
946
|
+
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
888
947
|
var name = arguments[0];
|
889
948
|
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
890
949
|
var responseJson = {};
|
891
|
-
|
950
|
+
var json = records.map(function(record) {return record.toJSON({includeId: true})});
|
951
|
+
responseJson[modelName.pluralize()] = json;
|
892
952
|
var url = this.buildURL(modelName);
|
893
|
-
// mock the ajax call, but return nothing, since the records will be
|
894
|
-
// retrieved from the store where they were just loaded above
|
895
953
|
this.stubEndpointForHttpRequest(url, responseJson);
|
896
954
|
},
|
897
955
|
/**
|
898
956
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
899
957
|
|
958
|
+
First variation = pass in model instances
|
900
959
|
```js
|
901
|
-
// First build json for the instances you want 'returned' in your query.
|
902
|
-
var usersJson = FactoryGuy.buildList('user', 2);
|
903
960
|
|
904
|
-
//
|
905
|
-
|
906
|
-
|
961
|
+
// Create model instances
|
962
|
+
var users = FactoryGuy.makeList('user', 2, 'with_hats');
|
963
|
+
|
964
|
+
// Pass in the array of model instances as last argument
|
965
|
+
testHelper.handleFindQuery('user', ['name', 'age'], users);
|
907
966
|
|
908
967
|
store.findQuery('user', {name:'Bob', age: 10}}).then(function(userInstances){
|
909
|
-
/// userInstances
|
968
|
+
/// userInstances will be the same of the users that were passed in
|
910
969
|
})
|
911
970
|
```
|
912
971
|
|
913
|
-
|
972
|
+
Third variation - pass in nothing for last argument
|
973
|
+
```js
|
974
|
+
// This simulates a query that returns no results
|
975
|
+
testHelper.handleFindQuery('user', ['age']);
|
976
|
+
|
977
|
+
store.findQuery('user', {age: 10000}}).then(function(userInstances){
|
978
|
+
/// userInstances will be empty
|
979
|
+
})
|
980
|
+
```
|
914
981
|
|
915
982
|
@param {String} modelName name of the mode like 'user' for User model type
|
916
983
|
@param {String} searchParams the parameters that will be queried
|
917
|
-
@param {
|
984
|
+
@param {Array} array of DS.Model records to be 'returned' by query
|
918
985
|
*/
|
919
|
-
handleFindQuery: function (modelName, searchParams,
|
986
|
+
handleFindQuery: function (modelName, searchParams, records) {
|
987
|
+
Ember.assert('The second argument of searchParams must be an array',Em.typeOf(searchParams) == 'array')
|
988
|
+
if (records) {
|
989
|
+
Ember.assert('The third argument ( records ) must be an array - found type:' + Em.typeOf(records), Em.typeOf(records) == 'array')
|
990
|
+
} else {
|
991
|
+
records = []
|
992
|
+
}
|
993
|
+
var json = records.map(function(record) {return record.toJSON({includeId: true})})
|
920
994
|
var responseJson = {};
|
921
995
|
responseJson[modelName.pluralize()] = json;
|
922
996
|
var url = this.buildURL(modelName);
|