ember-data-factory-guy 0.9.4 → 0.9.5
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 +41 -14
- data/bower.json +1 -1
- data/dist/amd/factory-guy.js +70 -37
- data/dist/ember-data-factory-guy.js +70 -37
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/package.json +1 -1
- data/src/factory_guy_test_mixin.js +70 -37
- data/tests/factory_guy_test_mixin_test.js +91 -28
- data/vendor/assets/javascripts/ember_data_factory_guy.js +72 -38
- 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: 6abc061f668dd5d677abe7b0ec4f559d16af91f9
|
4
|
+
data.tar.gz: 2d38bdb9f7f4f5255609cf8fe42f49f88d9857da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cb599906cc1ce6ed367506f141aebaf405676142d34ee85984e3d76c85e167586a47abb8ffc30ae5e859e8eca1343930b4109f51436103478cebe203a34fc43
|
7
|
+
data.tar.gz: 9969fb77e5903fe169847c9bb741073194222b4e5fb92c1071df6fe17606e51a5d9a24b3e75f5e67e74b41aeb7da6b1d171d3da7811e88ade8a40674fe4f6de7
|
data/README.md
CHANGED
@@ -10,9 +10,12 @@ 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.4 -> ember-data-1.0.0-beta.12
|
14
14
|
|
15
|
-
**
|
15
|
+
**Waiting for ember-data-1.0.0-beta.15 to make upgrade release, since there are a few issues with
|
16
|
+
ember-data-1.0.0-beta.14.1 that make it difficult to use**
|
17
|
+
|
18
|
+
**Support for fixture adapter is back in business as of version 0.9.3**
|
16
19
|
|
17
20
|
*Version 0.9.0 and up deprecates explicit call to store.makeFixture in your tests, in favor
|
18
21
|
of using the FactoryGuy.make or testHelper.make function from FactoryGuyTestHelperMixin instead.
|
@@ -39,7 +42,7 @@ gem 'ember-data-factory-guy', group: test
|
|
39
42
|
or for particular version:
|
40
43
|
|
41
44
|
```ruby
|
42
|
-
gem 'ember-data-factory-guy', '0.9.
|
45
|
+
gem 'ember-data-factory-guy', '0.9.4', group: test
|
43
46
|
```
|
44
47
|
|
45
48
|
then:
|
@@ -74,7 +77,7 @@ or for particular version:
|
|
74
77
|
"dependencies": {
|
75
78
|
"foo-dependency": "latest",
|
76
79
|
"other-foo-dependency": "latest",
|
77
|
-
"ember-data-factory-guy": "0.9.
|
80
|
+
"ember-data-factory-guy": "0.9.4"
|
78
81
|
}
|
79
82
|
```
|
80
83
|
|
@@ -460,7 +463,7 @@ the reverse user hasMany 'projects' association is being setup for you on the us
|
|
460
463
|
|
461
464
|
``` javascript
|
462
465
|
FactoryGuy.define('user', {
|
463
|
-
user_with_projects: { FactoryGuy.hasMany('project', 2) }
|
466
|
+
user_with_projects: { projects: FactoryGuy.hasMany('project', 2) }
|
464
467
|
});
|
465
468
|
|
466
469
|
var user = FactoryGuy.make('user_with_projects');
|
@@ -594,8 +597,8 @@ test("make a user using your applications default adapter", function() {
|
|
594
597
|
|
595
598
|
- Uses mockjax
|
596
599
|
- Has helper methods
|
597
|
-
-
|
598
|
-
-
|
600
|
+
- handleFindAll
|
601
|
+
- handleFind
|
599
602
|
- handleFindQuery
|
600
603
|
- handleCreate
|
601
604
|
- handleUpdate
|
@@ -619,24 +622,43 @@ and this is already bundled for you when you use the ember-data-factory-guy libr
|
|
619
622
|
tests run as shown in the previous section (Using FactoryGuyTestMixin)**
|
620
623
|
|
621
624
|
|
622
|
-
#####
|
625
|
+
##### handleFindAll
|
623
626
|
- for dealing with finding all records of a particular type
|
627
|
+
- handleFindMany ( deprecated alias )
|
624
628
|
|
625
629
|
```javascript
|
626
630
|
// can use traits and extra fixture options here as you would with FactoryGuy#makeList
|
627
|
-
testHelper.
|
631
|
+
testHelper.handleFindAll('profile', 2);
|
628
632
|
|
629
633
|
store.find('profile').then(function (profiles) {
|
630
634
|
profiles.get('length') //=> 2
|
631
635
|
});
|
632
636
|
```
|
633
637
|
|
634
|
-
#####
|
635
|
-
-
|
638
|
+
##### handleFind
|
639
|
+
- pass in a record to handle reload
|
640
|
+
- pass in fixture name and options ( including id if needed ) to handle making a record
|
641
|
+
with those options and finding that record
|
642
|
+
- handleFindOne ( deprecated alias )
|
643
|
+
|
644
|
+
*Passing in a model instance*
|
636
645
|
|
637
646
|
```javascript
|
638
|
-
|
639
|
-
testHelper.
|
647
|
+
var profile = FactoryGuy.make('profile')
|
648
|
+
testHelper.handleFind(profile);
|
649
|
+
|
650
|
+
profile.reload().then(function (profile2) {
|
651
|
+
ok(profile2.id == profile.id);
|
652
|
+
start();
|
653
|
+
});
|
654
|
+
```
|
655
|
+
|
656
|
+
*Passing in fixture name and options*
|
657
|
+
|
658
|
+
```javascript
|
659
|
+
// can use traits and extra fixture options here as you would with FactoryGuy#make,
|
660
|
+
// since a record will be made, and placed in the store for you.
|
661
|
+
testHelper.handleFind('profile', {id: 1});
|
640
662
|
|
641
663
|
store.find('profile', 1).then(function (profile) {
|
642
664
|
profile.get('id') //=> 1
|
@@ -714,8 +736,13 @@ match and or returns options.
|
|
714
736
|
// Simplest case
|
715
737
|
// Don't care about a match just handle createRecord for any project
|
716
738
|
testHelper.handleCreate('project')
|
739
|
+
|
717
740
|
// Exactly matching attributes
|
718
741
|
testHelper.handleCreate('project', {match: {name: "Moo", user: user}})
|
742
|
+
|
743
|
+
// Matching some attributes
|
744
|
+
testHelper.handleCreate('project', {match: {name: "Moo"}})
|
745
|
+
|
719
746
|
// Exactly matching attributes, and returning extra attributes
|
720
747
|
testHelper.handleCreate('project', {
|
721
748
|
match: {name: "Moo", user: user}, returns: {created_at: new Date()}
|
@@ -732,7 +759,7 @@ match and or returns options.
|
|
732
759
|
// when the createRecord on the 'project' is called, it will fail
|
733
760
|
store.createRecord('project').save() //=> fails
|
734
761
|
|
735
|
-
// or fail only if the
|
762
|
+
// or fail only if the attributes match the match options
|
736
763
|
testHelper.handleCreate('project', {
|
737
764
|
match: {name: "Moo", user: user}, {succeed: false}
|
738
765
|
})
|
data/bower.json
CHANGED
data/dist/amd/factory-guy.js
CHANGED
@@ -913,6 +913,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
913
913
|
if (options.data) {
|
914
914
|
request.data = options.data;
|
915
915
|
}
|
916
|
+
|
916
917
|
$.mockjax(request);
|
917
918
|
},
|
918
919
|
/**
|
@@ -934,7 +935,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
934
935
|
// Pass in the parameters you would normally pass into FactoryGuy.makeList,
|
935
936
|
// like fixture name, number of fixtures to make, and optional traits,
|
936
937
|
// or fixture options
|
937
|
-
testHelper.
|
938
|
+
testHelper.handleFindAll('user', 2, 'with_hats');
|
938
939
|
|
939
940
|
store.find('user').then(function(users){
|
940
941
|
|
@@ -946,7 +947,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
946
947
|
@param {String} trait optional traits (one or more)
|
947
948
|
@param {Object} opts optional fixture options
|
948
949
|
*/
|
949
|
-
|
950
|
+
handleFindAll: function () {
|
950
951
|
// make the records and load them in the store
|
951
952
|
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
952
953
|
var name = arguments[0];
|
@@ -957,7 +958,10 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
957
958
|
var url = this.buildURL(modelName);
|
958
959
|
this.stubEndpointForHttpRequest(url, responseJson);
|
959
960
|
},
|
960
|
-
|
961
|
+
handleFindMany: function() {
|
962
|
+
Ember.deprecate('DEPRECATION Warning: use handleFindAll instead');
|
963
|
+
this.handleFindAll.apply(this, arguments)
|
964
|
+
},
|
961
965
|
/**
|
962
966
|
Handling ajax GET for finding one record for a type of model and an id.
|
963
967
|
You can mock failed find by passing in success argument as false.
|
@@ -977,18 +981,28 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
977
981
|
@param {String} trait optional traits (one or more)
|
978
982
|
@param {Object} opts optional fixture options (including id)
|
979
983
|
*/
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
var modelName
|
984
|
+
handleFind: function () {
|
985
|
+
var args = Array.prototype.slice.call(arguments)
|
986
|
+
Ember.assert("To handleFindOne pass in a model instance or a type and model options", args.length>0)
|
987
|
+
|
988
|
+
var record, modelName;
|
989
|
+
if (args[0] instanceof DS.Model) {
|
990
|
+
record = args[0];
|
991
|
+
modelName = record.constructor.typeKey;
|
992
|
+
} else {
|
993
|
+
// make the record and load it in the store
|
994
|
+
record = FactoryGuy.make.apply(FactoryGuy, arguments);
|
995
|
+
var name = arguments[0];
|
996
|
+
modelName = FactoryGuy.lookupModelForFixtureName(name);
|
997
|
+
}
|
998
|
+
|
985
999
|
var responseJson = {};
|
986
1000
|
var json = record.toJSON({includeId: true});
|
987
1001
|
responseJson[modelName.pluralize()] = json;
|
988
1002
|
var url = this.buildURL(modelName, record.id);
|
989
1003
|
this.stubEndpointForHttpRequest(url, responseJson);
|
990
1004
|
},
|
991
|
-
|
1005
|
+
handleFindOne: function() { this.handleFind.apply(this, arguments) },
|
992
1006
|
/**
|
993
1007
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
994
1008
|
|
@@ -1053,55 +1067,74 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1053
1067
|
succeed - flag to indicate if the request should succeed ( default is true )
|
1054
1068
|
|
1055
1069
|
Note:
|
1056
|
-
1)
|
1057
|
-
so you don't need to include them in the returns hash
|
1070
|
+
1) Any attributes in match will be added to the response json automatically,
|
1071
|
+
so you don't need to include them in the returns hash.
|
1072
|
+
|
1073
|
+
2) As long as all the match attributes are found in the record being created,
|
1074
|
+
the create will succeed. In other words, there may be other attributes in the
|
1075
|
+
createRecord call, but you don't have to match them all. For example:
|
1058
1076
|
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
returns hash.
|
1077
|
+
```js
|
1078
|
+
handleCreate('post', {match: {title: 'foo'})
|
1079
|
+
store.createRecord('post', {title: 'foo', created_at: new Date()})
|
1080
|
+
```
|
1081
|
+
|
1082
|
+
2) If you match on a belongsTo association, you don't have to include that in the
|
1083
|
+
returns hash either.
|
1066
1084
|
|
1067
1085
|
@param {String} modelName name of model your creating like 'profile' for Profile
|
1068
1086
|
@param {Object} options hash of options for handling request
|
1069
1087
|
*/
|
1070
1088
|
handleCreate: function (modelName, options) {
|
1071
1089
|
var opts = options === undefined ? {} : options;
|
1090
|
+
var store = this.getStore()
|
1072
1091
|
var succeed = opts.succeed === undefined ? true : opts.succeed;
|
1073
1092
|
var match = opts.match || {};
|
1074
1093
|
var returnArgs = opts.returns || {};
|
1075
1094
|
|
1076
|
-
var url = this.buildURL(modelName);
|
1077
|
-
var definition = FactoryGuy.modelDefinitions[modelName];
|
1078
|
-
|
1079
|
-
var httpOptions = {type: 'POST'};
|
1080
1095
|
if (opts.match) {
|
1081
|
-
var
|
1082
|
-
var
|
1083
|
-
expectedRequest[modelName] = record.serialize();
|
1084
|
-
httpOptions.data = JSON.stringify(expectedRequest);
|
1096
|
+
var record = store.createRecord(modelName, match);
|
1097
|
+
var expectedRequest = record.serialize();
|
1085
1098
|
}
|
1086
1099
|
|
1087
|
-
var modelType =
|
1100
|
+
var modelType = store.modelFor(modelName)
|
1088
1101
|
|
1089
1102
|
var responseJson = {};
|
1090
1103
|
if (succeed) {
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1104
|
+
var definition = FactoryGuy.modelDefinitions[modelName];
|
1105
|
+
responseJson[modelName] = $.extend({id: definition.nextId()}, match, returnArgs);
|
1106
|
+
// Remove belongsTo associations since they will already be set when you called
|
1107
|
+
// createRecord, so they don't need to be returned.
|
1108
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
1109
|
+
if (relationship.kind == 'belongsTo') {
|
1110
|
+
delete responseJson[modelName][relationship.key];
|
1111
|
+
}
|
1112
|
+
})
|
1113
|
+
}
|
1114
|
+
|
1115
|
+
var url = this.buildURL(modelName);
|
1116
|
+
|
1117
|
+
var handler = function(settings) {
|
1118
|
+
if (settings.url != url || settings.type != 'POST') { return false}
|
1119
|
+
|
1120
|
+
if (opts.match) {
|
1121
|
+
var requestData = JSON.parse(settings.data)[modelName];
|
1122
|
+
for (attribute in expectedRequest) {
|
1123
|
+
if (expectedRequest[attribute] && requestData[attribute] != expectedRequest[attribute]) {
|
1124
|
+
return false
|
1097
1125
|
}
|
1098
|
-
}
|
1099
|
-
|
1100
|
-
|
1126
|
+
}
|
1127
|
+
}
|
1128
|
+
var responseStatus = (succeed ? 200: 500);
|
1129
|
+
return {
|
1130
|
+
responseText: responseJson,
|
1131
|
+
status: responseStatus
|
1132
|
+
};
|
1101
1133
|
}
|
1102
1134
|
|
1103
|
-
|
1135
|
+
$.mockjax(handler);
|
1104
1136
|
},
|
1137
|
+
|
1105
1138
|
/**
|
1106
1139
|
Handling ajax PUT ( update record ) for a model type. You can mock
|
1107
1140
|
failed update by passing in success argument as false.
|
@@ -908,6 +908,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
908
908
|
if (options.data) {
|
909
909
|
request.data = options.data;
|
910
910
|
}
|
911
|
+
|
911
912
|
$.mockjax(request);
|
912
913
|
},
|
913
914
|
/**
|
@@ -929,7 +930,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
929
930
|
// Pass in the parameters you would normally pass into FactoryGuy.makeList,
|
930
931
|
// like fixture name, number of fixtures to make, and optional traits,
|
931
932
|
// or fixture options
|
932
|
-
testHelper.
|
933
|
+
testHelper.handleFindAll('user', 2, 'with_hats');
|
933
934
|
|
934
935
|
store.find('user').then(function(users){
|
935
936
|
|
@@ -941,7 +942,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
941
942
|
@param {String} trait optional traits (one or more)
|
942
943
|
@param {Object} opts optional fixture options
|
943
944
|
*/
|
944
|
-
|
945
|
+
handleFindAll: function () {
|
945
946
|
// make the records and load them in the store
|
946
947
|
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
947
948
|
var name = arguments[0];
|
@@ -952,7 +953,10 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
952
953
|
var url = this.buildURL(modelName);
|
953
954
|
this.stubEndpointForHttpRequest(url, responseJson);
|
954
955
|
},
|
955
|
-
|
956
|
+
handleFindMany: function() {
|
957
|
+
Ember.deprecate('DEPRECATION Warning: use handleFindAll instead');
|
958
|
+
this.handleFindAll.apply(this, arguments)
|
959
|
+
},
|
956
960
|
/**
|
957
961
|
Handling ajax GET for finding one record for a type of model and an id.
|
958
962
|
You can mock failed find by passing in success argument as false.
|
@@ -972,18 +976,28 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
972
976
|
@param {String} trait optional traits (one or more)
|
973
977
|
@param {Object} opts optional fixture options (including id)
|
974
978
|
*/
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
var modelName
|
979
|
+
handleFind: function () {
|
980
|
+
var args = Array.prototype.slice.call(arguments)
|
981
|
+
Ember.assert("To handleFindOne pass in a model instance or a type and model options", args.length>0)
|
982
|
+
|
983
|
+
var record, modelName;
|
984
|
+
if (args[0] instanceof DS.Model) {
|
985
|
+
record = args[0];
|
986
|
+
modelName = record.constructor.typeKey;
|
987
|
+
} else {
|
988
|
+
// make the record and load it in the store
|
989
|
+
record = FactoryGuy.make.apply(FactoryGuy, arguments);
|
990
|
+
var name = arguments[0];
|
991
|
+
modelName = FactoryGuy.lookupModelForFixtureName(name);
|
992
|
+
}
|
993
|
+
|
980
994
|
var responseJson = {};
|
981
995
|
var json = record.toJSON({includeId: true});
|
982
996
|
responseJson[modelName.pluralize()] = json;
|
983
997
|
var url = this.buildURL(modelName, record.id);
|
984
998
|
this.stubEndpointForHttpRequest(url, responseJson);
|
985
999
|
},
|
986
|
-
|
1000
|
+
handleFindOne: function() { this.handleFind.apply(this, arguments) },
|
987
1001
|
/**
|
988
1002
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
989
1003
|
|
@@ -1048,55 +1062,74 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1048
1062
|
succeed - flag to indicate if the request should succeed ( default is true )
|
1049
1063
|
|
1050
1064
|
Note:
|
1051
|
-
1)
|
1052
|
-
so you don't need to include them in the returns hash
|
1065
|
+
1) Any attributes in match will be added to the response json automatically,
|
1066
|
+
so you don't need to include them in the returns hash.
|
1067
|
+
|
1068
|
+
2) As long as all the match attributes are found in the record being created,
|
1069
|
+
the create will succeed. In other words, there may be other attributes in the
|
1070
|
+
createRecord call, but you don't have to match them all. For example:
|
1053
1071
|
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
returns hash.
|
1072
|
+
```js
|
1073
|
+
handleCreate('post', {match: {title: 'foo'})
|
1074
|
+
store.createRecord('post', {title: 'foo', created_at: new Date()})
|
1075
|
+
```
|
1076
|
+
|
1077
|
+
2) If you match on a belongsTo association, you don't have to include that in the
|
1078
|
+
returns hash either.
|
1061
1079
|
|
1062
1080
|
@param {String} modelName name of model your creating like 'profile' for Profile
|
1063
1081
|
@param {Object} options hash of options for handling request
|
1064
1082
|
*/
|
1065
1083
|
handleCreate: function (modelName, options) {
|
1066
1084
|
var opts = options === undefined ? {} : options;
|
1085
|
+
var store = this.getStore()
|
1067
1086
|
var succeed = opts.succeed === undefined ? true : opts.succeed;
|
1068
1087
|
var match = opts.match || {};
|
1069
1088
|
var returnArgs = opts.returns || {};
|
1070
1089
|
|
1071
|
-
var url = this.buildURL(modelName);
|
1072
|
-
var definition = FactoryGuy.modelDefinitions[modelName];
|
1073
|
-
|
1074
|
-
var httpOptions = {type: 'POST'};
|
1075
1090
|
if (opts.match) {
|
1076
|
-
var
|
1077
|
-
var
|
1078
|
-
expectedRequest[modelName] = record.serialize();
|
1079
|
-
httpOptions.data = JSON.stringify(expectedRequest);
|
1091
|
+
var record = store.createRecord(modelName, match);
|
1092
|
+
var expectedRequest = record.serialize();
|
1080
1093
|
}
|
1081
1094
|
|
1082
|
-
var modelType =
|
1095
|
+
var modelType = store.modelFor(modelName)
|
1083
1096
|
|
1084
1097
|
var responseJson = {};
|
1085
1098
|
if (succeed) {
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1099
|
+
var definition = FactoryGuy.modelDefinitions[modelName];
|
1100
|
+
responseJson[modelName] = $.extend({id: definition.nextId()}, match, returnArgs);
|
1101
|
+
// Remove belongsTo associations since they will already be set when you called
|
1102
|
+
// createRecord, so they don't need to be returned.
|
1103
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
1104
|
+
if (relationship.kind == 'belongsTo') {
|
1105
|
+
delete responseJson[modelName][relationship.key];
|
1106
|
+
}
|
1107
|
+
})
|
1108
|
+
}
|
1109
|
+
|
1110
|
+
var url = this.buildURL(modelName);
|
1111
|
+
|
1112
|
+
var handler = function(settings) {
|
1113
|
+
if (settings.url != url || settings.type != 'POST') { return false}
|
1114
|
+
|
1115
|
+
if (opts.match) {
|
1116
|
+
var requestData = JSON.parse(settings.data)[modelName];
|
1117
|
+
for (attribute in expectedRequest) {
|
1118
|
+
if (expectedRequest[attribute] && requestData[attribute] != expectedRequest[attribute]) {
|
1119
|
+
return false
|
1092
1120
|
}
|
1093
|
-
}
|
1094
|
-
|
1095
|
-
|
1121
|
+
}
|
1122
|
+
}
|
1123
|
+
var responseStatus = (succeed ? 200: 500);
|
1124
|
+
return {
|
1125
|
+
responseText: responseJson,
|
1126
|
+
status: responseStatus
|
1127
|
+
};
|
1096
1128
|
}
|
1097
1129
|
|
1098
|
-
|
1130
|
+
$.mockjax(handler);
|
1099
1131
|
},
|
1132
|
+
|
1100
1133
|
/**
|
1101
1134
|
Handling ajax PUT ( update record ) for a model type. You can mock
|
1102
1135
|
failed update by passing in success argument as false.
|
@@ -1 +1 @@
|
|
1
|
-
var Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};var MissingSequenceError=function(message){this.toString=function(){return message}};if(FactoryGuy!==undefined){FactoryGuy.sequence=Sequence;FactoryGuy.missingSequenceError=MissingSequenceError}var ModelDefinition=function(model,config){var sequences={};var traits={};var defaultAttributes={};var namedModels={};var modelId=1;var sequenceName=null;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.nextId=function(){return modelId++};this.generate=function(name,sequenceFn){if(sequenceFn){if(!sequences[name]){sequences[name]=new Sequence(sequenceFn)}}var sequence=sequences[name];if(!sequence){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequence.next()};this.build=function(name,opts,traitArgs){var traitsObj={};traitArgs.forEach(function(trait){$.extend(traitsObj,traits[trait])});var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,traitsObj,opts);for(var attribute in fixture){if(Ember.typeOf(fixture[attribute])=="function"){fixture[attribute]=fixture[attribute].call(this,fixture)}else if(Ember.typeOf(fixture[attribute])=="object"){if(FactoryGuy.getStore()){if(FactoryGuy.isAttributeRelationship(this.model,attribute)){fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}else{fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}}if(!fixture.id){fixture.id=this.nextId()}return fixture};this.buildList=function(name,number,traits,opts){var arr=[];for(var i=0;i<number;i++){arr.push(this.build(name,opts,traits))}return arr};this.reset=function(){modelId=1;for(var name in sequences){sequences[name].reset()}};var parseDefault=function(object){if(!object){return}defaultAttributes=object};var parseTraits=function(object){if(!object){return}traits=object};var parseSequences=function(object){if(!object){return}for(sequenceName in object){var sequenceFn=object[sequenceName];if(Ember.typeOf(sequenceFn)!="function"){throw new Error("Problem with ["+sequenceName+"] sequence definition. Sequences must be functions")}object[sequenceName]=new Sequence(sequenceFn)}sequences=object};var parseConfig=function(config){parseSequences(config.sequences);delete config.sequences;parseTraits(config.traits);delete config.traits;parseDefault(config.default);delete config.default;namedModels=config};parseConfig(config)};if(FactoryGuy!==undefined){FactoryGuy.modelDefiniton=ModelDefinition}var FactoryGuy={modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},setStore:function(store){Ember.assert("FactoryGuy#setStore needs a valid store instance.You passed in ["+store+"]",store instanceof DS.Store);this.store=store},getStore:function(){return this.store},isAttributeRelationship:function(typeName,attribute){if(!this.store){Ember.debug("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures");return true}var model=this.store.modelFor(typeName);return!!model.typeForRelationship(attribute)},generate:function(nameOrFunction){var sortaRandomName=Math.floor((1+Math.random())*65536).toString(16)+Date.now();return function(){if(Em.typeOf(nameOrFunction)=="function"){return this.generate(sortaRandomName,nameOrFunction)}else{return this.generate(nameOrFunction)}}},belongsTo:function(fixtureName,opts){return function(){return FactoryGuy.build(fixtureName,opts)}},association:function(fixtureName,opts){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.belongsTo instead");return this.belongsTo(fixtureName,opts)},hasMany:function(fixtureName,number,opts){return function(){return FactoryGuy.buildList(fixtureName,number,opts)}},lookupModelForFixtureName:function(name){var definition=this.lookupDefinitionForFixtureName(name);if(definition){return definition.model}},lookupDefinitionForFixtureName:function(name){for(var model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition}}},build:function(){var args=Array.prototype.slice.call(arguments);var opts={};var name=args.shift();if(!name){throw new Error("Build needs a factory name to build")}if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.build(name,opts,traits)},buildList:function(){var args=Array.prototype.slice.call(arguments);var name=args.shift();var number=args.shift();if(!name||!number){throw new Error("buildList needs a name and a number ( at least ) to build with")}var opts={};if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,traits,opts)},make:function(){var store=this.store;Ember.assert("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures",store);var fixture=this.build.apply(this,arguments);var name=arguments[0];var modelName=this.lookupModelForFixtureName(name);var modelType=store.modelFor(modelName);if(store.usingFixtureAdapter()){store.setAssociationsForFixtureAdapter(modelType,modelName,fixture);fixture=FactoryGuy.pushFixture(modelType,fixture);store.loadModelForFixtureAdapter(modelType,fixture);return fixture}else{return store.makeModel(modelType,fixture)}},makeList:function(){Ember.assert("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures",this.store);var arr=[];var args=Array.prototype.slice.call(arguments);Ember.assert("makeList needs at least 2 arguments, a name and a number",args.length>=2);var number=args.splice(1,1)[0];Ember.assert("Second argument to makeList should be a number (of fixtures to make.)",typeof number=="number");for(var i=0;i<number;i++){arr.push(this.make.apply(this,args))}return arr},resetModels:function(store){for(var model in this.modelDefinitions){var definition=this.modelDefinitions[model];definition.reset();try{var modelType=store.modelFor(definition.model);if(store.usingFixtureAdapter()){modelType.FIXTURES=[]}store.unloadAll(modelType)}catch(e){console.log("resetModels",e)}}},pushFixture:function(modelClass,fixture){var index;if(!modelClass.FIXTURES){modelClass.FIXTURES=[]}index=this.indexOfFixture(modelClass.FIXTURES,fixture);if(index>-1){modelClass.FIXTURES.splice(index,1)}modelClass.FIXTURES.push(fixture);return fixture},indexOfFixture:function(fixtures,fixture){var index=-1,id=fixture.id+"";Ember.A(fixtures).find(function(r,i){if(""+Ember.get(r,"id")===id){index=i;return true}else{return false}});return index},clear:function(opts){if(!opts){this.modelDefinitions={}}}};(function(){DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.make instead");FactoryGuy.make.call(FactoryGuy,arguments)},makeList:function(){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.makeList instead");FactoryGuy.makeList.call(FactoryGuy,arguments)},makeModel:function(modelType,fixture){var store=this,modelName=store.modelFor(modelType).typeKey,model;Em.run(function(){store.findEmbeddedAssociationsForRESTAdapter(modelType,fixture);if(fixture.type){modelName=fixture.type.underscore();modelType=store.modelFor(modelName)}model=store.push(modelName,fixture);store.setAssociationsForRESTAdapter(modelType,modelName,model)});return model},setAssociationsForFixtureAdapter:function(modelType,modelName,fixture){var self=this;var adapter=this.adapterFor("application");Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){var hasManyRelation,belongsToRecord;if(relationship.kind=="hasMany"){hasManyRelation=fixture[relationship.key];if(hasManyRelation){$.each(fixture[relationship.key],function(index,object){var id=object;if(Ember.typeOf(object)=="object"){FactoryGuy.pushFixture(relationship.type,object);id=object.id;hasManyRelation[index]=id}var hasManyfixtures=adapter.fixturesForType(relationship.type);var hasManyFixture=adapter.findFixtureById(hasManyfixtures,id);hasManyFixture[modelName]=fixture.id;self.loadModelForFixtureAdapter(relationship.type,hasManyFixture)})}}if(relationship.kind=="belongsTo"){belongsToRecord=fixture[relationship.key];if(belongsToRecord){if(typeof belongsToRecord=="object"){FactoryGuy.pushFixture(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord.id}var hasManyName=self.findHasManyRelationshipNameForFixtureAdapter(relationship.type,relationship.parentType);var belongsToFixtures=adapter.fixturesForType(relationship.type);var belongsTofixture=adapter.findFixtureById(belongsToFixtures,fixture[relationship.key]);if(!belongsTofixture[hasManyName]){belongsTofixture[hasManyName]=[]}belongsTofixture[hasManyName].push(fixture.id);self.loadModelForFixtureAdapter(relationship.type,belongsTofixture)}}})},loadModelForFixtureAdapter:function(modelType,fixture){var storeModel=this.getById(modelType,fixture.id),that=this;if(!Ember.isPresent(storeModel)||storeModel.get("isEmpty")){Ember.run(function(){var dup=Ember.copy(fixture,true);that.push(modelType,fixture);Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(fixture[relationship.key]){fixture[relationship.key]=dup[relationship.key]}})})}},findEmbeddedAssociationsForRESTAdapter:function(modelType,fixture){var store=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(Ember.typeOf(belongsToRecord)=="object"){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,belongsToRecord);belongsToRecord=store.push(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord}}if(relationship.kind=="hasMany"){var hasManyRecords=fixture[relationship.key];if(Ember.typeOf(hasManyRecords)=="array"){if(Ember.typeOf(hasManyRecords[0])=="object"){var records=Em.A();hasManyRecords.map(function(object){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,object);var record=store.push(relationship.type,object);records.push(record);return record});fixture[relationship.key]=records}}}})},setAssociationsForRESTAdapter:function(modelType,modelName,model){var self=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var children=model.get(name)||[];children.forEach(function(child){var belongsToName=self.findRelationshipName("belongsTo",child.constructor,model);if(belongsToName){child.set(belongsToName,model)}})}})},findRelationshipName:function(kind,belongToModelType,childModel){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind==kind&&childModel instanceof relationship.type){relationshipName=relationship.key}});return relationshipName},findHasManyRelationshipNameForFixtureAdapter:function(belongToModelType,childModelType){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"&&childModelType==relationship.type){relationshipName=relationship.key}});return relationshipName},pushPayload:function(type,payload){var typeName,model;if(this.usingFixtureAdapter()){if(Ember.typeOf(type)==="string"&&Ember.isPresent(payload)&&Ember.isPresent(payload.id)){model=this.modelFor(type);FactoryGuy.pushFixture(model,payload);this.push(model,Ember.copy(payload,true))}else if(Ember.typeOf(type)==="object"||Ember.typeOf(payload)==="object"){if(Ember.isBlank(payload)){payload=type}for(var prop in payload){typeName=Ember.String.camelize(Ember.String.singularize(prop));model=this.modelFor(typeName);this.pushMany(model,Ember.makeArray(Ember.copy(payload[prop],true)));Ember.ArrayPolyfills.forEach.call(Ember.makeArray(payload[prop]),function(hash){FactoryGuy.pushFixture(model,hash)},this)}}else{throw new Ember.Error("Assertion Failed: You cannot use `store#pushPayload` with this method signature pushPayload("+type+","+payload+")")}}else{this._super(type,payload)}}})})();var FactoryGuyTestMixin=Em.Mixin.create({setup:function(app){this.set("container",app.__container__);FactoryGuy.setStore(this.getStore());return this},useFixtureAdapter:function(app){app.ApplicationAdapter=DS.FixtureAdapter;this.getStore().adapterFor("application").simulateRemoteResponse=false},usingActiveModelSerializer:function(type){var store=this.getStore();var modelType=store.modelFor(type);var serializer=store.serializerFor(modelType.typeKey);return serializer instanceof DS.ActiveModelSerializer},find:function(type,id){return this.getStore().find(type,id)},make:function(){return FactoryGuy.make.apply(FactoryGuy,arguments)},getStore:function(){return this.get("container").lookup("store:main")},stubEndpointForHttpRequest:function(url,json,options){options=options||{};var request={url:url,dataType:"json",responseText:json,type:options.type||"GET",status:options.status||200};if(options.urlParams){request.urlParams=options.urlParams}if(options.data){request.data=options.data}$.mockjax(request)},buildURL:function(typeName,id){var type=this.getStore().modelFor(typeName);return this.getStore().adapterFor(type).buildURL(type.typeKey,id)},handleFindMany:function(){var records=FactoryGuy.makeList.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};var json=records.map(function(record){return record.toJSON({includeId:true})});responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson)},handleFindOne:function(){var record=FactoryGuy.make.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};var json=record.toJSON({includeId:true});responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName,record.id);this.stubEndpointForHttpRequest(url,responseJson)},handleFindQuery:function(modelName,searchParams,records){Ember.assert("The second argument of searchParams must be an array",Em.typeOf(searchParams)=="array");if(records){Ember.assert("The third argument ( records ) must be an array - found type:"+Em.typeOf(records),Em.typeOf(records)=="array")}else{records=[]}var json=records.map(function(record){return record.toJSON({includeId:true})});var responseJson={};responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{urlParams:searchParams})},handleCreate:function(modelName,options){var opts=options===undefined?{}:options;var succeed=opts.succeed===undefined?true:opts.succeed;var match=opts.match||{};var returnArgs=opts.returns||{};var url=this.buildURL(modelName);var definition=FactoryGuy.modelDefinitions[modelName];var httpOptions={type:"POST"};if(opts.match){var expectedRequest={};var record=this.getStore().createRecord(modelName,match);expectedRequest[modelName]=record.serialize();httpOptions.data=JSON.stringify(expectedRequest)}var modelType=this.getStore().modelFor(modelName);var responseJson={};if(succeed){responseJson[modelName]=$.extend({id:definition.nextId()},match,returnArgs);Ember.get(modelType,"relationshipsByName").forEach(function(relationship){if(relationship.kind=="belongsTo"){delete responseJson[modelName][relationship.key]}})}else{httpOptions.status=500}this.stubEndpointForHttpRequest(url,responseJson,httpOptions)},handleUpdate:function(){var args=Array.prototype.slice.call(arguments);Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0);var succeed=true;if(typeof args[args.length-1]=="boolean"){args.pop();succeed=false}Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0);var type,id;if(args[0]instanceof DS.Model){var model=args[0];type=model.constructor.typeKey;id=model.id}else if(typeof args[0]=="string"&&typeof parseInt(args[1])=="number"){type=args[0];id=args[1]}Ember.assert("To handleUpdate pass in a model instance or a type and an id",type&&id);this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"PUT",status:succeed?200:500})},handleDelete:function(type,id,succeed){succeed=succeed===undefined?true:succeed;this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"DELETE",status:succeed?200:500})},teardown:function(){FactoryGuy.resetModels(this.getStore());$.mockjax.clear()}});if(FactoryGuy!==undefined){FactoryGuy.testMixin=FactoryGuyTestMixin}
|
1
|
+
var Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};var MissingSequenceError=function(message){this.toString=function(){return message}};if(FactoryGuy!==undefined){FactoryGuy.sequence=Sequence;FactoryGuy.missingSequenceError=MissingSequenceError}var ModelDefinition=function(model,config){var sequences={};var traits={};var defaultAttributes={};var namedModels={};var modelId=1;var sequenceName=null;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.nextId=function(){return modelId++};this.generate=function(name,sequenceFn){if(sequenceFn){if(!sequences[name]){sequences[name]=new Sequence(sequenceFn)}}var sequence=sequences[name];if(!sequence){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequence.next()};this.build=function(name,opts,traitArgs){var traitsObj={};traitArgs.forEach(function(trait){$.extend(traitsObj,traits[trait])});var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,traitsObj,opts);for(var attribute in fixture){if(Ember.typeOf(fixture[attribute])=="function"){fixture[attribute]=fixture[attribute].call(this,fixture)}else if(Ember.typeOf(fixture[attribute])=="object"){if(FactoryGuy.getStore()){if(FactoryGuy.isAttributeRelationship(this.model,attribute)){fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}else{fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}}if(!fixture.id){fixture.id=this.nextId()}return fixture};this.buildList=function(name,number,traits,opts){var arr=[];for(var i=0;i<number;i++){arr.push(this.build(name,opts,traits))}return arr};this.reset=function(){modelId=1;for(var name in sequences){sequences[name].reset()}};var parseDefault=function(object){if(!object){return}defaultAttributes=object};var parseTraits=function(object){if(!object){return}traits=object};var parseSequences=function(object){if(!object){return}for(sequenceName in object){var sequenceFn=object[sequenceName];if(Ember.typeOf(sequenceFn)!="function"){throw new Error("Problem with ["+sequenceName+"] sequence definition. Sequences must be functions")}object[sequenceName]=new Sequence(sequenceFn)}sequences=object};var parseConfig=function(config){parseSequences(config.sequences);delete config.sequences;parseTraits(config.traits);delete config.traits;parseDefault(config.default);delete config.default;namedModels=config};parseConfig(config)};if(FactoryGuy!==undefined){FactoryGuy.modelDefiniton=ModelDefinition}var FactoryGuy={modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},setStore:function(store){Ember.assert("FactoryGuy#setStore needs a valid store instance.You passed in ["+store+"]",store instanceof DS.Store);this.store=store},getStore:function(){return this.store},isAttributeRelationship:function(typeName,attribute){if(!this.store){Ember.debug("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures");return true}var model=this.store.modelFor(typeName);return!!model.typeForRelationship(attribute)},generate:function(nameOrFunction){var sortaRandomName=Math.floor((1+Math.random())*65536).toString(16)+Date.now();return function(){if(Em.typeOf(nameOrFunction)=="function"){return this.generate(sortaRandomName,nameOrFunction)}else{return this.generate(nameOrFunction)}}},belongsTo:function(fixtureName,opts){return function(){return FactoryGuy.build(fixtureName,opts)}},association:function(fixtureName,opts){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.belongsTo instead");return this.belongsTo(fixtureName,opts)},hasMany:function(fixtureName,number,opts){return function(){return FactoryGuy.buildList(fixtureName,number,opts)}},lookupModelForFixtureName:function(name){var definition=this.lookupDefinitionForFixtureName(name);if(definition){return definition.model}},lookupDefinitionForFixtureName:function(name){for(var model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition}}},build:function(){var args=Array.prototype.slice.call(arguments);var opts={};var name=args.shift();if(!name){throw new Error("Build needs a factory name to build")}if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.build(name,opts,traits)},buildList:function(){var args=Array.prototype.slice.call(arguments);var name=args.shift();var number=args.shift();if(!name||!number){throw new Error("buildList needs a name and a number ( at least ) to build with")}var opts={};if(Ember.typeOf(args[args.length-1])=="object"){opts=args.pop()}var traits=args;var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,traits,opts)},make:function(){var store=this.store;Ember.assert("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures",store);var fixture=this.build.apply(this,arguments);var name=arguments[0];var modelName=this.lookupModelForFixtureName(name);var modelType=store.modelFor(modelName);if(store.usingFixtureAdapter()){store.setAssociationsForFixtureAdapter(modelType,modelName,fixture);fixture=FactoryGuy.pushFixture(modelType,fixture);store.loadModelForFixtureAdapter(modelType,fixture);return fixture}else{return store.makeModel(modelType,fixture)}},makeList:function(){Ember.assert("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures",this.store);var arr=[];var args=Array.prototype.slice.call(arguments);Ember.assert("makeList needs at least 2 arguments, a name and a number",args.length>=2);var number=args.splice(1,1)[0];Ember.assert("Second argument to makeList should be a number (of fixtures to make.)",typeof number=="number");for(var i=0;i<number;i++){arr.push(this.make.apply(this,args))}return arr},resetModels:function(store){for(var model in this.modelDefinitions){var definition=this.modelDefinitions[model];definition.reset();try{var modelType=store.modelFor(definition.model);if(store.usingFixtureAdapter()){modelType.FIXTURES=[]}store.unloadAll(modelType)}catch(e){console.log("resetModels",e)}}},pushFixture:function(modelClass,fixture){var index;if(!modelClass.FIXTURES){modelClass.FIXTURES=[]}index=this.indexOfFixture(modelClass.FIXTURES,fixture);if(index>-1){modelClass.FIXTURES.splice(index,1)}modelClass.FIXTURES.push(fixture);return fixture},indexOfFixture:function(fixtures,fixture){var index=-1,id=fixture.id+"";Ember.A(fixtures).find(function(r,i){if(""+Ember.get(r,"id")===id){index=i;return true}else{return false}});return index},clear:function(opts){if(!opts){this.modelDefinitions={}}}};(function(){DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.make instead");FactoryGuy.make.call(FactoryGuy,arguments)},makeList:function(){Ember.deprecate("DEPRECATION Warning: use FactoryGuy.makeList instead");FactoryGuy.makeList.call(FactoryGuy,arguments)},makeModel:function(modelType,fixture){var store=this,modelName=store.modelFor(modelType).typeKey,model;Em.run(function(){store.findEmbeddedAssociationsForRESTAdapter(modelType,fixture);if(fixture.type){modelName=fixture.type.underscore();modelType=store.modelFor(modelName)}model=store.push(modelName,fixture);store.setAssociationsForRESTAdapter(modelType,modelName,model)});return model},setAssociationsForFixtureAdapter:function(modelType,modelName,fixture){var self=this;var adapter=this.adapterFor("application");Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){var hasManyRelation,belongsToRecord;if(relationship.kind=="hasMany"){hasManyRelation=fixture[relationship.key];if(hasManyRelation){$.each(fixture[relationship.key],function(index,object){var id=object;if(Ember.typeOf(object)=="object"){FactoryGuy.pushFixture(relationship.type,object);id=object.id;hasManyRelation[index]=id}var hasManyfixtures=adapter.fixturesForType(relationship.type);var hasManyFixture=adapter.findFixtureById(hasManyfixtures,id);hasManyFixture[modelName]=fixture.id;self.loadModelForFixtureAdapter(relationship.type,hasManyFixture)})}}if(relationship.kind=="belongsTo"){belongsToRecord=fixture[relationship.key];if(belongsToRecord){if(typeof belongsToRecord=="object"){FactoryGuy.pushFixture(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord.id}var hasManyName=self.findHasManyRelationshipNameForFixtureAdapter(relationship.type,relationship.parentType);var belongsToFixtures=adapter.fixturesForType(relationship.type);var belongsTofixture=adapter.findFixtureById(belongsToFixtures,fixture[relationship.key]);if(!belongsTofixture[hasManyName]){belongsTofixture[hasManyName]=[]}belongsTofixture[hasManyName].push(fixture.id);self.loadModelForFixtureAdapter(relationship.type,belongsTofixture)}}})},loadModelForFixtureAdapter:function(modelType,fixture){var storeModel=this.getById(modelType,fixture.id),that=this;if(!Ember.isPresent(storeModel)||storeModel.get("isEmpty")){Ember.run(function(){var dup=Ember.copy(fixture,true);that.push(modelType,fixture);Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(fixture[relationship.key]){fixture[relationship.key]=dup[relationship.key]}})})}},findEmbeddedAssociationsForRESTAdapter:function(modelType,fixture){var store=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(Ember.typeOf(belongsToRecord)=="object"){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,belongsToRecord);belongsToRecord=store.push(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord}}if(relationship.kind=="hasMany"){var hasManyRecords=fixture[relationship.key];if(Ember.typeOf(hasManyRecords)=="array"){if(Ember.typeOf(hasManyRecords[0])=="object"){var records=Em.A();hasManyRecords.map(function(object){store.findEmbeddedAssociationsForRESTAdapter(relationship.type,object);var record=store.push(relationship.type,object);records.push(record);return record});fixture[relationship.key]=records}}}})},setAssociationsForRESTAdapter:function(modelType,modelName,model){var self=this;Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var children=model.get(name)||[];children.forEach(function(child){var belongsToName=self.findRelationshipName("belongsTo",child.constructor,model);if(belongsToName){child.set(belongsToName,model)}})}})},findRelationshipName:function(kind,belongToModelType,childModel){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind==kind&&childModel instanceof relationship.type){relationshipName=relationship.key}});return relationshipName},findHasManyRelationshipNameForFixtureAdapter:function(belongToModelType,childModelType){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"&&childModelType==relationship.type){relationshipName=relationship.key}});return relationshipName},pushPayload:function(type,payload){var typeName,model;if(this.usingFixtureAdapter()){if(Ember.typeOf(type)==="string"&&Ember.isPresent(payload)&&Ember.isPresent(payload.id)){model=this.modelFor(type);FactoryGuy.pushFixture(model,payload);this.push(model,Ember.copy(payload,true))}else if(Ember.typeOf(type)==="object"||Ember.typeOf(payload)==="object"){if(Ember.isBlank(payload)){payload=type}for(var prop in payload){typeName=Ember.String.camelize(Ember.String.singularize(prop));model=this.modelFor(typeName);this.pushMany(model,Ember.makeArray(Ember.copy(payload[prop],true)));Ember.ArrayPolyfills.forEach.call(Ember.makeArray(payload[prop]),function(hash){FactoryGuy.pushFixture(model,hash)},this)}}else{throw new Ember.Error("Assertion Failed: You cannot use `store#pushPayload` with this method signature pushPayload("+type+","+payload+")")}}else{this._super(type,payload)}}})})();var FactoryGuyTestMixin=Em.Mixin.create({setup:function(app){this.set("container",app.__container__);FactoryGuy.setStore(this.getStore());return this},useFixtureAdapter:function(app){app.ApplicationAdapter=DS.FixtureAdapter;this.getStore().adapterFor("application").simulateRemoteResponse=false},usingActiveModelSerializer:function(type){var store=this.getStore();var modelType=store.modelFor(type);var serializer=store.serializerFor(modelType.typeKey);return serializer instanceof DS.ActiveModelSerializer},find:function(type,id){return this.getStore().find(type,id)},make:function(){return FactoryGuy.make.apply(FactoryGuy,arguments)},getStore:function(){return this.get("container").lookup("store:main")},stubEndpointForHttpRequest:function(url,json,options){options=options||{};var request={url:url,dataType:"json",responseText:json,type:options.type||"GET",status:options.status||200};if(options.urlParams){request.urlParams=options.urlParams}if(options.data){request.data=options.data}$.mockjax(request)},buildURL:function(typeName,id){var type=this.getStore().modelFor(typeName);return this.getStore().adapterFor(type).buildURL(type.typeKey,id)},handleFindAll:function(){var records=FactoryGuy.makeList.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};var json=records.map(function(record){return record.toJSON({includeId:true})});responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson)},handleFindMany:function(){Ember.deprecate("DEPRECATION Warning: use handleFindAll instead");this.handleFindAll.apply(this,arguments)},handleFind:function(){var args=Array.prototype.slice.call(arguments);Ember.assert("To handleFindOne pass in a model instance or a type and model options",args.length>0);var record,modelName;if(args[0]instanceof DS.Model){record=args[0];modelName=record.constructor.typeKey}else{record=FactoryGuy.make.apply(FactoryGuy,arguments);var name=arguments[0];modelName=FactoryGuy.lookupModelForFixtureName(name)}var responseJson={};var json=record.toJSON({includeId:true});responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName,record.id);this.stubEndpointForHttpRequest(url,responseJson)},handleFindOne:function(){this.handleFind.apply(this,arguments)},handleFindQuery:function(modelName,searchParams,records){Ember.assert("The second argument of searchParams must be an array",Em.typeOf(searchParams)=="array");if(records){Ember.assert("The third argument ( records ) must be an array - found type:"+Em.typeOf(records),Em.typeOf(records)=="array")}else{records=[]}var json=records.map(function(record){return record.toJSON({includeId:true})});var responseJson={};responseJson[modelName.pluralize()]=json;var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{urlParams:searchParams})},handleCreate:function(modelName,options){var opts=options===undefined?{}:options;var store=this.getStore();var succeed=opts.succeed===undefined?true:opts.succeed;var match=opts.match||{};var returnArgs=opts.returns||{};if(opts.match){var record=store.createRecord(modelName,match);var expectedRequest=record.serialize()}var modelType=store.modelFor(modelName);var responseJson={};if(succeed){var definition=FactoryGuy.modelDefinitions[modelName];responseJson[modelName]=$.extend({id:definition.nextId()},match,returnArgs);Ember.get(modelType,"relationshipsByName").forEach(function(relationship){if(relationship.kind=="belongsTo"){delete responseJson[modelName][relationship.key]}})}var url=this.buildURL(modelName);var handler=function(settings){if(settings.url!=url||settings.type!="POST"){return false}if(opts.match){var requestData=JSON.parse(settings.data)[modelName];for(attribute in expectedRequest){if(expectedRequest[attribute]&&requestData[attribute]!=expectedRequest[attribute]){return false}}}var responseStatus=succeed?200:500;return{responseText:responseJson,status:responseStatus}};$.mockjax(handler)},handleUpdate:function(){var args=Array.prototype.slice.call(arguments);Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0);var succeed=true;if(typeof args[args.length-1]=="boolean"){args.pop();succeed=false}Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0);var type,id;if(args[0]instanceof DS.Model){var model=args[0];type=model.constructor.typeKey;id=model.id}else if(typeof args[0]=="string"&&typeof parseInt(args[1])=="number"){type=args[0];id=args[1]}Ember.assert("To handleUpdate pass in a model instance or a type and an id",type&&id);this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"PUT",status:succeed?200:500})},handleDelete:function(type,id,succeed){succeed=succeed===undefined?true:succeed;this.stubEndpointForHttpRequest(this.buildURL(type,id),{},{type:"DELETE",status:succeed?200:500})},teardown:function(){FactoryGuy.resetModels(this.getStore());$.mockjax.clear()}});if(FactoryGuy!==undefined){FactoryGuy.testMixin=FactoryGuyTestMixin}
|
data/package.json
CHANGED
@@ -60,6 +60,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
60
60
|
if (options.data) {
|
61
61
|
request.data = options.data;
|
62
62
|
}
|
63
|
+
|
63
64
|
$.mockjax(request);
|
64
65
|
},
|
65
66
|
/**
|
@@ -81,7 +82,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
81
82
|
// Pass in the parameters you would normally pass into FactoryGuy.makeList,
|
82
83
|
// like fixture name, number of fixtures to make, and optional traits,
|
83
84
|
// or fixture options
|
84
|
-
testHelper.
|
85
|
+
testHelper.handleFindAll('user', 2, 'with_hats');
|
85
86
|
|
86
87
|
store.find('user').then(function(users){
|
87
88
|
|
@@ -93,7 +94,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
93
94
|
@param {String} trait optional traits (one or more)
|
94
95
|
@param {Object} opts optional fixture options
|
95
96
|
*/
|
96
|
-
|
97
|
+
handleFindAll: function () {
|
97
98
|
// make the records and load them in the store
|
98
99
|
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
99
100
|
var name = arguments[0];
|
@@ -104,7 +105,10 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
104
105
|
var url = this.buildURL(modelName);
|
105
106
|
this.stubEndpointForHttpRequest(url, responseJson);
|
106
107
|
},
|
107
|
-
|
108
|
+
handleFindMany: function() {
|
109
|
+
Ember.deprecate('DEPRECATION Warning: use handleFindAll instead');
|
110
|
+
this.handleFindAll.apply(this, arguments)
|
111
|
+
},
|
108
112
|
/**
|
109
113
|
Handling ajax GET for finding one record for a type of model and an id.
|
110
114
|
You can mock failed find by passing in success argument as false.
|
@@ -124,18 +128,28 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
124
128
|
@param {String} trait optional traits (one or more)
|
125
129
|
@param {Object} opts optional fixture options (including id)
|
126
130
|
*/
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
var modelName
|
131
|
+
handleFind: function () {
|
132
|
+
var args = Array.prototype.slice.call(arguments)
|
133
|
+
Ember.assert("To handleFindOne pass in a model instance or a type and model options", args.length>0)
|
134
|
+
|
135
|
+
var record, modelName;
|
136
|
+
if (args[0] instanceof DS.Model) {
|
137
|
+
record = args[0];
|
138
|
+
modelName = record.constructor.typeKey;
|
139
|
+
} else {
|
140
|
+
// make the record and load it in the store
|
141
|
+
record = FactoryGuy.make.apply(FactoryGuy, arguments);
|
142
|
+
var name = arguments[0];
|
143
|
+
modelName = FactoryGuy.lookupModelForFixtureName(name);
|
144
|
+
}
|
145
|
+
|
132
146
|
var responseJson = {};
|
133
147
|
var json = record.toJSON({includeId: true});
|
134
148
|
responseJson[modelName.pluralize()] = json;
|
135
149
|
var url = this.buildURL(modelName, record.id);
|
136
150
|
this.stubEndpointForHttpRequest(url, responseJson);
|
137
151
|
},
|
138
|
-
|
152
|
+
handleFindOne: function() { this.handleFind.apply(this, arguments) },
|
139
153
|
/**
|
140
154
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
141
155
|
|
@@ -200,55 +214,74 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
200
214
|
succeed - flag to indicate if the request should succeed ( default is true )
|
201
215
|
|
202
216
|
Note:
|
203
|
-
1)
|
204
|
-
so you don't need to include them in the returns hash
|
217
|
+
1) Any attributes in match will be added to the response json automatically,
|
218
|
+
so you don't need to include them in the returns hash.
|
219
|
+
|
220
|
+
2) As long as all the match attributes are found in the record being created,
|
221
|
+
the create will succeed. In other words, there may be other attributes in the
|
222
|
+
createRecord call, but you don't have to match them all. For example:
|
223
|
+
|
224
|
+
```js
|
225
|
+
handleCreate('post', {match: {title: 'foo'})
|
226
|
+
store.createRecord('post', {title: 'foo', created_at: new Date()})
|
227
|
+
```
|
205
228
|
|
206
|
-
2) If you
|
207
|
-
|
208
|
-
style handleCreate, because if the responseJson returns a non empty data hash ( with even only
|
209
|
-
the id), this essentially empty hash of attributes will override ( and nullify ) all the attributes
|
210
|
-
that set when you created the record.
|
211
|
-
3) If you match on a belongsTo association, you don't have to include that in the
|
212
|
-
returns hash.
|
229
|
+
2) If you match on a belongsTo association, you don't have to include that in the
|
230
|
+
returns hash either.
|
213
231
|
|
214
232
|
@param {String} modelName name of model your creating like 'profile' for Profile
|
215
233
|
@param {Object} options hash of options for handling request
|
216
234
|
*/
|
217
235
|
handleCreate: function (modelName, options) {
|
218
236
|
var opts = options === undefined ? {} : options;
|
237
|
+
var store = this.getStore()
|
219
238
|
var succeed = opts.succeed === undefined ? true : opts.succeed;
|
220
239
|
var match = opts.match || {};
|
221
240
|
var returnArgs = opts.returns || {};
|
222
241
|
|
223
|
-
var url = this.buildURL(modelName);
|
224
|
-
var definition = FactoryGuy.modelDefinitions[modelName];
|
225
|
-
|
226
|
-
var httpOptions = {type: 'POST'};
|
227
242
|
if (opts.match) {
|
228
|
-
var
|
229
|
-
var
|
230
|
-
expectedRequest[modelName] = record.serialize();
|
231
|
-
httpOptions.data = JSON.stringify(expectedRequest);
|
243
|
+
var record = store.createRecord(modelName, match);
|
244
|
+
var expectedRequest = record.serialize();
|
232
245
|
}
|
233
246
|
|
234
|
-
var modelType =
|
247
|
+
var modelType = store.modelFor(modelName)
|
235
248
|
|
236
249
|
var responseJson = {};
|
237
250
|
if (succeed) {
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
251
|
+
var definition = FactoryGuy.modelDefinitions[modelName];
|
252
|
+
responseJson[modelName] = $.extend({id: definition.nextId()}, match, returnArgs);
|
253
|
+
// Remove belongsTo associations since they will already be set when you called
|
254
|
+
// createRecord, so they don't need to be returned.
|
255
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
256
|
+
if (relationship.kind == 'belongsTo') {
|
257
|
+
delete responseJson[modelName][relationship.key];
|
258
|
+
}
|
259
|
+
})
|
260
|
+
}
|
261
|
+
|
262
|
+
var url = this.buildURL(modelName);
|
263
|
+
|
264
|
+
var handler = function(settings) {
|
265
|
+
if (settings.url != url || settings.type != 'POST') { return false}
|
266
|
+
|
267
|
+
if (opts.match) {
|
268
|
+
var requestData = JSON.parse(settings.data)[modelName];
|
269
|
+
for (attribute in expectedRequest) {
|
270
|
+
if (expectedRequest[attribute] && requestData[attribute] != expectedRequest[attribute]) {
|
271
|
+
return false
|
244
272
|
}
|
245
|
-
}
|
246
|
-
|
247
|
-
|
273
|
+
}
|
274
|
+
}
|
275
|
+
var responseStatus = (succeed ? 200: 500);
|
276
|
+
return {
|
277
|
+
responseText: responseJson,
|
278
|
+
status: responseStatus
|
279
|
+
};
|
248
280
|
}
|
249
281
|
|
250
|
-
|
282
|
+
$.mockjax(handler);
|
251
283
|
},
|
284
|
+
|
252
285
|
/**
|
253
286
|
Handling ajax PUT ( update record ) for a model type. You can mock
|
254
287
|
failed update by passing in success argument as false.
|
@@ -107,10 +107,10 @@ asyncTest("#handleFindQuery passing in existing instances with hasMany and belon
|
|
107
107
|
});
|
108
108
|
|
109
109
|
|
110
|
-
///////
|
110
|
+
/////// handleFindAll //////////
|
111
111
|
|
112
|
-
asyncTest("#
|
113
|
-
testHelper.
|
112
|
+
asyncTest("#handleFindAll the basic", function () {
|
113
|
+
testHelper.handleFindAll('profile', 2);
|
114
114
|
|
115
115
|
store.findAll('profile').then(function (profiles) {
|
116
116
|
ok(profiles.get('length') == 2);
|
@@ -118,8 +118,8 @@ asyncTest("#handleFindMany the basic", function () {
|
|
118
118
|
});
|
119
119
|
});
|
120
120
|
|
121
|
-
asyncTest("#
|
122
|
-
testHelper.
|
121
|
+
asyncTest("#handleFindAll with fixture options", function () {
|
122
|
+
testHelper.handleFindAll('profile', 2, {description: 'dude'});
|
123
123
|
|
124
124
|
store.findAll('profile').then(function (profiles) {
|
125
125
|
ok(profiles.get('length') == 2);
|
@@ -128,8 +128,8 @@ asyncTest("#handleFindMany with fixture options", function () {
|
|
128
128
|
});
|
129
129
|
});
|
130
130
|
|
131
|
-
asyncTest("#
|
132
|
-
testHelper.
|
131
|
+
asyncTest("#handleFindAll with traits", function () {
|
132
|
+
testHelper.handleFindAll('profile', 2, 'goofy_description');
|
133
133
|
|
134
134
|
store.findAll('profile').then(function (profiles) {
|
135
135
|
ok(profiles.get('length') == 2);
|
@@ -138,8 +138,8 @@ asyncTest("#handleFindMany with traits", function () {
|
|
138
138
|
});
|
139
139
|
});
|
140
140
|
|
141
|
-
asyncTest("#
|
142
|
-
testHelper.
|
141
|
+
asyncTest("#handleFindAll with traits and extra options", function () {
|
142
|
+
testHelper.handleFindAll('profile', 2, 'goofy_description', {description: 'dude'});
|
143
143
|
|
144
144
|
store.findAll('profile').then(function (profiles) {
|
145
145
|
ok(profiles.get('length') == 2);
|
@@ -149,9 +149,9 @@ asyncTest("#handleFindMany with traits and extra options", function () {
|
|
149
149
|
});
|
150
150
|
|
151
151
|
|
152
|
-
////////
|
152
|
+
//////// handleFind /////////
|
153
153
|
|
154
|
-
asyncTest("#handleFindOne
|
154
|
+
asyncTest("#handleFindOne aliases handleFind", function () {
|
155
155
|
var id = 1
|
156
156
|
testHelper.handleFindOne('profile', {id: id});
|
157
157
|
|
@@ -159,11 +159,45 @@ asyncTest("#handleFindOne the basic", function () {
|
|
159
159
|
ok(profile.get('id') == id);
|
160
160
|
start();
|
161
161
|
});
|
162
|
+
|
163
|
+
});
|
164
|
+
|
165
|
+
asyncTest("#handleFind with a record returns the record", function () {
|
166
|
+
var profile = FactoryGuy.make('profile')
|
167
|
+
testHelper.handleFind(profile);
|
168
|
+
|
169
|
+
store.find('profile', 1).then(function (profile2) {
|
170
|
+
ok(profile2.id == profile.id);
|
171
|
+
start();
|
172
|
+
});
|
173
|
+
|
174
|
+
});
|
175
|
+
|
176
|
+
asyncTest("#handleFind with a record handles reload", function () {
|
177
|
+
var profile = FactoryGuy.make('profile')
|
178
|
+
testHelper.handleFind(profile);
|
179
|
+
|
180
|
+
profile.reload().then(function (profile2) {
|
181
|
+
ok(profile2.id == profile.id);
|
182
|
+
start();
|
183
|
+
});
|
184
|
+
|
185
|
+
});
|
186
|
+
|
187
|
+
asyncTest("#handleFind with options", function () {
|
188
|
+
var id = 1
|
189
|
+
testHelper.handleFind('profile', {id: id});
|
190
|
+
|
191
|
+
store.find('profile', 1).then(function (profile) {
|
192
|
+
ok(profile.get('id') == id);
|
193
|
+
start();
|
194
|
+
});
|
195
|
+
|
162
196
|
});
|
163
197
|
|
164
|
-
asyncTest("#
|
198
|
+
asyncTest("#handleFind with traits", function () {
|
165
199
|
var id = 1
|
166
|
-
testHelper.
|
200
|
+
testHelper.handleFind('profile', 'goofy_description', {id: id});
|
167
201
|
|
168
202
|
store.find('profile', 1).then(function (profile) {
|
169
203
|
ok(profile.get('description') == 'goofy');
|
@@ -171,10 +205,10 @@ asyncTest("#handleFindOne with traits", function () {
|
|
171
205
|
});
|
172
206
|
});
|
173
207
|
|
174
|
-
asyncTest("#
|
208
|
+
asyncTest("#handleFind with arguments", function () {
|
175
209
|
var id = 1
|
176
210
|
var description = 'guy';
|
177
|
-
testHelper.
|
211
|
+
testHelper.handleFind('profile', {id: id, description: description });
|
178
212
|
|
179
213
|
store.find('profile', 1).then(function (profile) {
|
180
214
|
ok(profile.get('description') == description);
|
@@ -182,10 +216,10 @@ asyncTest("#handleFindOne with arguments", function () {
|
|
182
216
|
});
|
183
217
|
});
|
184
218
|
|
185
|
-
asyncTest("#
|
219
|
+
asyncTest("#handleFind with traits and arguments", function () {
|
186
220
|
var id = 1
|
187
221
|
var description = 'guy';
|
188
|
-
testHelper.
|
222
|
+
testHelper.handleFind('profile', 'goofy_description', {id: id, description: description});
|
189
223
|
|
190
224
|
store.find('profile', 1).then(function (profile) {
|
191
225
|
ok(profile.get('description') == description);
|
@@ -219,15 +253,16 @@ asyncTest("#handleCreate with no specific match", function() {
|
|
219
253
|
});
|
220
254
|
});
|
221
255
|
|
222
|
-
asyncTest("#handleCreate
|
256
|
+
asyncTest("#handleCreate match some attributes", function() {
|
223
257
|
var customDescription = "special description"
|
258
|
+
var date = new Date();
|
224
259
|
|
225
260
|
testHelper.handleCreate('profile', {
|
226
261
|
match: {description: customDescription}
|
227
262
|
})
|
228
263
|
|
229
264
|
store.createRecord('profile', {
|
230
|
-
description: customDescription
|
265
|
+
description: customDescription, created_at: date
|
231
266
|
}).save().then(function(profile) {
|
232
267
|
ok(profile instanceof Profile)
|
233
268
|
ok(profile.id == 1)
|
@@ -236,6 +271,25 @@ asyncTest("#handleCreate matches attributes and returns these", function() {
|
|
236
271
|
});
|
237
272
|
});
|
238
273
|
|
274
|
+
asyncTest("#handleCreate match all attributes", function() {
|
275
|
+
var customDescription = "special description"
|
276
|
+
var date = new Date();
|
277
|
+
|
278
|
+
testHelper.handleCreate('profile', {
|
279
|
+
match: {description: customDescription, created_at: date}
|
280
|
+
})
|
281
|
+
|
282
|
+
store.createRecord('profile', {
|
283
|
+
description: customDescription, created_at: date
|
284
|
+
}).save().then(function(profile) {
|
285
|
+
ok(profile instanceof Profile)
|
286
|
+
ok(profile.id == 1)
|
287
|
+
ok(profile.get('description') == customDescription)
|
288
|
+
ok(profile.get('created_at') == date.toString())
|
289
|
+
start();
|
290
|
+
});
|
291
|
+
});
|
292
|
+
|
239
293
|
|
240
294
|
asyncTest("#handleCreate returns attributes", function() {
|
241
295
|
var date = new Date()
|
@@ -342,12 +396,21 @@ asyncTest("#handleCreate match but still fail", function() {
|
|
342
396
|
|
343
397
|
|
344
398
|
|
345
|
-
///////
|
399
|
+
/////// handleFindAll //////////
|
346
400
|
|
347
|
-
|
348
|
-
asyncTest("#handleFindMany the basic", function () {
|
401
|
+
asyncTest("#handleFindMany aliases handleFindAll", function () {
|
349
402
|
testHelper.handleFindMany('profile', 2);
|
350
403
|
|
404
|
+
store.find('profile').then(function (profiles) {
|
405
|
+
ok(profiles.get('length') == 2);
|
406
|
+
ok(profiles.get('firstObject.description') == 'Text goes here');
|
407
|
+
start();
|
408
|
+
});
|
409
|
+
})
|
410
|
+
|
411
|
+
asyncTest("#handleFindAll the basic", function () {
|
412
|
+
testHelper.handleFindAll('profile', 2);
|
413
|
+
|
351
414
|
store.find('profile').then(function (profiles) {
|
352
415
|
ok(profiles.get('length') == 2);
|
353
416
|
ok(profiles.get('firstObject.description') == 'Text goes here');
|
@@ -355,8 +418,8 @@ asyncTest("#handleFindMany the basic", function () {
|
|
355
418
|
});
|
356
419
|
});
|
357
420
|
|
358
|
-
asyncTest("#
|
359
|
-
testHelper.
|
421
|
+
asyncTest("#handleFindAll with fixture options", function () {
|
422
|
+
testHelper.handleFindAll('profile', 2, {description: 'dude'});
|
360
423
|
|
361
424
|
store.find('profile').then(function (profiles) {
|
362
425
|
ok(profiles.get('length') == 2);
|
@@ -365,8 +428,8 @@ asyncTest("#handleFindMany with fixture options", function () {
|
|
365
428
|
});
|
366
429
|
});
|
367
430
|
|
368
|
-
asyncTest("#
|
369
|
-
testHelper.
|
431
|
+
asyncTest("#handleFindAll with traits", function () {
|
432
|
+
testHelper.handleFindAll('profile', 2, 'goofy_description');
|
370
433
|
|
371
434
|
store.find('profile').then(function (profiles) {
|
372
435
|
ok(profiles.get('length') == 2);
|
@@ -375,8 +438,8 @@ asyncTest("#handleFindMany with traits", function () {
|
|
375
438
|
});
|
376
439
|
});
|
377
440
|
|
378
|
-
asyncTest("#
|
379
|
-
testHelper.
|
441
|
+
asyncTest("#handleFindAll with traits and fixture options", function () {
|
442
|
+
testHelper.handleFindAll('profile', 2, 'goofy_description', {description: 'dude'});
|
380
443
|
|
381
444
|
store.find('profile').then(function (profiles) {
|
382
445
|
ok(profiles.get('length') == 2);
|
@@ -908,6 +908,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
908
908
|
if (options.data) {
|
909
909
|
request.data = options.data;
|
910
910
|
}
|
911
|
+
|
911
912
|
$.mockjax(request);
|
912
913
|
},
|
913
914
|
/**
|
@@ -929,7 +930,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
929
930
|
// Pass in the parameters you would normally pass into FactoryGuy.makeList,
|
930
931
|
// like fixture name, number of fixtures to make, and optional traits,
|
931
932
|
// or fixture options
|
932
|
-
testHelper.
|
933
|
+
testHelper.handleFindAll('user', 2, 'with_hats');
|
933
934
|
|
934
935
|
store.find('user').then(function(users){
|
935
936
|
|
@@ -941,7 +942,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
941
942
|
@param {String} trait optional traits (one or more)
|
942
943
|
@param {Object} opts optional fixture options
|
943
944
|
*/
|
944
|
-
|
945
|
+
handleFindAll: function () {
|
945
946
|
// make the records and load them in the store
|
946
947
|
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
947
948
|
var name = arguments[0];
|
@@ -952,7 +953,10 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
952
953
|
var url = this.buildURL(modelName);
|
953
954
|
this.stubEndpointForHttpRequest(url, responseJson);
|
954
955
|
},
|
955
|
-
|
956
|
+
handleFindMany: function() {
|
957
|
+
Ember.deprecate('DEPRECATION Warning: use handleFindAll instead');
|
958
|
+
this.handleFindAll.apply(this, arguments)
|
959
|
+
},
|
956
960
|
/**
|
957
961
|
Handling ajax GET for finding one record for a type of model and an id.
|
958
962
|
You can mock failed find by passing in success argument as false.
|
@@ -972,18 +976,28 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
972
976
|
@param {String} trait optional traits (one or more)
|
973
977
|
@param {Object} opts optional fixture options (including id)
|
974
978
|
*/
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
var modelName
|
979
|
+
handleFind: function () {
|
980
|
+
var args = Array.prototype.slice.call(arguments)
|
981
|
+
Ember.assert("To handleFindOne pass in a model instance or a type and model options", args.length>0)
|
982
|
+
|
983
|
+
var record, modelName;
|
984
|
+
if (args[0] instanceof DS.Model) {
|
985
|
+
record = args[0];
|
986
|
+
modelName = record.constructor.typeKey;
|
987
|
+
} else {
|
988
|
+
// make the record and load it in the store
|
989
|
+
record = FactoryGuy.make.apply(FactoryGuy, arguments);
|
990
|
+
var name = arguments[0];
|
991
|
+
modelName = FactoryGuy.lookupModelForFixtureName(name);
|
992
|
+
}
|
993
|
+
|
980
994
|
var responseJson = {};
|
981
995
|
var json = record.toJSON({includeId: true});
|
982
996
|
responseJson[modelName.pluralize()] = json;
|
983
997
|
var url = this.buildURL(modelName, record.id);
|
984
998
|
this.stubEndpointForHttpRequest(url, responseJson);
|
985
999
|
},
|
986
|
-
|
1000
|
+
handleFindOne: function() { this.handleFind.apply(this, arguments) },
|
987
1001
|
/**
|
988
1002
|
Handling ajax GET for finding all records for a type of model with query parameters.
|
989
1003
|
|
@@ -1048,55 +1062,74 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1048
1062
|
succeed - flag to indicate if the request should succeed ( default is true )
|
1049
1063
|
|
1050
1064
|
Note:
|
1051
|
-
1)
|
1052
|
-
so you don't need to include them in the returns hash
|
1065
|
+
1) Any attributes in match will be added to the response json automatically,
|
1066
|
+
so you don't need to include them in the returns hash.
|
1067
|
+
|
1068
|
+
2) As long as all the match attributes are found in the record being created,
|
1069
|
+
the create will succeed. In other words, there may be other attributes in the
|
1070
|
+
createRecord call, but you don't have to match them all. For example:
|
1071
|
+
|
1072
|
+
```js
|
1073
|
+
handleCreate('post', {match: {title: 'foo'})
|
1074
|
+
store.createRecord('post', {title: 'foo', created_at: new Date()})
|
1075
|
+
```
|
1053
1076
|
|
1054
|
-
2) If you
|
1055
|
-
|
1056
|
-
style handleCreate, because if the responseJson returns a non empty data hash ( with even only
|
1057
|
-
the id), this essentially empty hash of attributes will override ( and nullify ) all the attributes
|
1058
|
-
that set when you created the record.
|
1059
|
-
3) If you match on a belongsTo association, you don't have to include that in the
|
1060
|
-
returns hash.
|
1077
|
+
2) If you match on a belongsTo association, you don't have to include that in the
|
1078
|
+
returns hash either.
|
1061
1079
|
|
1062
1080
|
@param {String} modelName name of model your creating like 'profile' for Profile
|
1063
1081
|
@param {Object} options hash of options for handling request
|
1064
1082
|
*/
|
1065
1083
|
handleCreate: function (modelName, options) {
|
1066
1084
|
var opts = options === undefined ? {} : options;
|
1085
|
+
var store = this.getStore()
|
1067
1086
|
var succeed = opts.succeed === undefined ? true : opts.succeed;
|
1068
1087
|
var match = opts.match || {};
|
1069
1088
|
var returnArgs = opts.returns || {};
|
1070
1089
|
|
1071
|
-
var url = this.buildURL(modelName);
|
1072
|
-
var definition = FactoryGuy.modelDefinitions[modelName];
|
1073
|
-
|
1074
|
-
var httpOptions = {type: 'POST'};
|
1075
1090
|
if (opts.match) {
|
1076
|
-
var
|
1077
|
-
var
|
1078
|
-
expectedRequest[modelName] = record.serialize();
|
1079
|
-
httpOptions.data = JSON.stringify(expectedRequest);
|
1091
|
+
var record = store.createRecord(modelName, match);
|
1092
|
+
var expectedRequest = record.serialize();
|
1080
1093
|
}
|
1081
1094
|
|
1082
|
-
var modelType =
|
1095
|
+
var modelType = store.modelFor(modelName)
|
1083
1096
|
|
1084
1097
|
var responseJson = {};
|
1085
1098
|
if (succeed) {
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1099
|
+
var definition = FactoryGuy.modelDefinitions[modelName];
|
1100
|
+
responseJson[modelName] = $.extend({id: definition.nextId()}, match, returnArgs);
|
1101
|
+
// Remove belongsTo associations since they will already be set when you called
|
1102
|
+
// createRecord, so they don't need to be returned.
|
1103
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
1104
|
+
if (relationship.kind == 'belongsTo') {
|
1105
|
+
delete responseJson[modelName][relationship.key];
|
1106
|
+
}
|
1107
|
+
})
|
1108
|
+
}
|
1109
|
+
|
1110
|
+
var url = this.buildURL(modelName);
|
1111
|
+
|
1112
|
+
var handler = function(settings) {
|
1113
|
+
if (settings.url != url || settings.type != 'POST') { return false}
|
1114
|
+
|
1115
|
+
if (opts.match) {
|
1116
|
+
var requestData = JSON.parse(settings.data)[modelName];
|
1117
|
+
for (attribute in expectedRequest) {
|
1118
|
+
if (expectedRequest[attribute] && requestData[attribute] != expectedRequest[attribute]) {
|
1119
|
+
return false
|
1092
1120
|
}
|
1093
|
-
}
|
1094
|
-
|
1095
|
-
|
1121
|
+
}
|
1122
|
+
}
|
1123
|
+
var responseStatus = (succeed ? 200: 500);
|
1124
|
+
return {
|
1125
|
+
responseText: responseJson,
|
1126
|
+
status: responseStatus
|
1127
|
+
};
|
1096
1128
|
}
|
1097
1129
|
|
1098
|
-
|
1130
|
+
$.mockjax(handler);
|
1099
1131
|
},
|
1132
|
+
|
1100
1133
|
/**
|
1101
1134
|
Handling ajax PUT ( update record ) for a model type. You can mock
|
1102
1135
|
failed update by passing in success argument as false.
|
@@ -1650,6 +1683,7 @@ if (FactoryGuy !== undefined) {
|
|
1650
1683
|
}
|
1651
1684
|
|
1652
1685
|
mockHandler = getMockForRequest( mockHandlers[k], requestSettings );
|
1686
|
+
|
1653
1687
|
if(!mockHandler) {
|
1654
1688
|
// No valid mock found for this request
|
1655
1689
|
continue;
|
@@ -1701,7 +1735,7 @@ if (FactoryGuy !== undefined) {
|
|
1701
1735
|
}
|
1702
1736
|
|
1703
1737
|
copyUrlParameters(mockHandler, origSettings);
|
1704
|
-
|
1738
|
+
//console.log('here copyUrlParameters', 'mockHandler=>',mockHandler, 'requestSettings=>',requestSettings, 'origSettings=>',origSettings)
|
1705
1739
|
(function(mockHandler, requestSettings, origSettings, origHandler) {
|
1706
1740
|
|
1707
1741
|
mockRequest = _ajax.call($, $.extend(true, {}, origSettings, {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ember-data-factory-guy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Sudol
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-01-
|
12
|
+
date: 2015-01-17 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easily create Fixtures for Ember Data
|
15
15
|
email:
|