ember-data-factory-guy 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -7
- data/bower.json +1 -1
- data/dist/ember-data-factory-guy.js +44 -8
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/ember-data-factory-guy.gemspec +1 -1
- data/package.json +1 -1
- data/src/factory_guy_test_mixin.js +44 -8
- data/tests/active_model_adapter_factory_test.js +13 -2
- data/tests/factory_guy_test_mixin_test.js +85 -18
- data/tests/rest_adapter_factory_test.js +9 -0
- data/tests/support/factories/hat_factory.js +4 -6
- data/tests/support/models/profile.js +1 -0
- data/tests/support/test_helper.js +1 -0
- data/tests/test_setup.js +5 -6
- data/vendor/assets/javascripts/ember_data_factory_guy.js +47 -10
- 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: 21e578c47d29e02b51bc42c7f6866184e7f956ed
|
4
|
+
data.tar.gz: 293d74863f20e2a2be14f44c789cc1401d67c070
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fba41b592a7a4976cd007bb17e732d072e5fe36c2ce52fd31f4121106b1e9a0eabb6713928b0ab92659911fd73074c2ba6b3db9a87643908be4502c85f2d1326
|
7
|
+
data.tar.gz: c9180e3a8d4995b930c2bf2b66cc0310b2b7fdd2184f020b589c3d4a1e7fd24105a5bddefa8eb6080a64d2ecd5c62c0c9b288bc7825dbd0e6406f0f5ff47dd6e
|
data/README.md
CHANGED
@@ -9,12 +9,12 @@ of ember-data-factory-guy.
|
|
9
9
|
- Versions:
|
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
|
-
- 0.
|
13
|
-
- 0.
|
12
|
+
- 0.8.1 -> ember-data-1.0.0-beta.11
|
13
|
+
- 0.8.1 -> ember-data-1.0.0-beta.12
|
14
14
|
|
15
|
-
**For versions ( 0.7.1 -> 0.
|
15
|
+
**For versions ( 0.7.1 -> 0.8.1 ), support for the fixture adapter is currently broken.**
|
16
16
|
|
17
|
-
*Version 0.
|
17
|
+
*Version 0.8.1 has many bug fixes and improvements that the earlier versions don't have, so
|
18
18
|
hopefully you can switch to newer version of ember-data and therefore the best
|
19
19
|
ember-data-factory-guy version, but if not, send me bug report and I will try and go back
|
20
20
|
and fix the older version you are using.*
|
@@ -34,7 +34,7 @@ gem 'ember-data-factory-guy', group: test
|
|
34
34
|
or for particular version:
|
35
35
|
|
36
36
|
```ruby
|
37
|
-
gem 'ember-data-factory-guy', '0.
|
37
|
+
gem 'ember-data-factory-guy', '0.8.1', group: test
|
38
38
|
```
|
39
39
|
|
40
40
|
then:
|
@@ -69,7 +69,7 @@ or for particular version:
|
|
69
69
|
"dependencies": {
|
70
70
|
"foo-dependency": "latest",
|
71
71
|
"other-foo-dependency": "latest",
|
72
|
-
"ember-data-factory-guy": "0.
|
72
|
+
"ember-data-factory-guy": "0.8.1"
|
73
73
|
}
|
74
74
|
```
|
75
75
|
|
@@ -700,7 +700,7 @@ to a particular user. To mock this createRecord call do this:
|
|
700
700
|
````
|
701
701
|
|
702
702
|
|
703
|
-
#####
|
703
|
+
##### Integration test sample
|
704
704
|
|
705
705
|
```javascript
|
706
706
|
|
data/bower.json
CHANGED
@@ -793,26 +793,61 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
793
793
|
Handling ajax POST ( create record ) for a model. You can mock
|
794
794
|
failed create by passing in success argument as false.
|
795
795
|
|
796
|
+
```js
|
797
|
+
handleCreate('post')
|
798
|
+
handleCreate('post', { match: {title: 'foo'} )
|
799
|
+
handleCreate('post', { match: {title: 'foo', user: user} )
|
800
|
+
handleCreate('post', { returns: {createdAt: new Date()} )
|
801
|
+
handleCreate('post', { match: {title: 'foo'}, returns: {createdAt: new Date()} )
|
802
|
+
handleCreate('post', { match: {title: 'foo'}, success: false} } )
|
803
|
+
```
|
804
|
+
|
805
|
+
match - attributes that must be in request json,
|
806
|
+
returns - attributes to include in response json,
|
807
|
+
succeed - flag to indicate if the request should succeed ( default is true )
|
808
|
+
|
809
|
+
Note that any attributes in match will be added to the response json automatically,
|
810
|
+
so you don't need to include them in the returns hash as well.
|
811
|
+
|
812
|
+
Also, if you match on a belongsTo association, you don't have to include that in the
|
813
|
+
returns hash.
|
814
|
+
|
796
815
|
@param {String} modelName name of model your creating like 'profile' for Profile
|
797
|
-
@param {Object} options hash of
|
798
|
-
@param {Boolean} succeed optional flag to indicate if the request
|
799
|
-
should succeed ( default is true )
|
816
|
+
@param {Object} options hash of options for handling request
|
800
817
|
*/
|
801
|
-
handleCreate: function (modelName, options
|
802
|
-
succeed = succeed === undefined ? true : succeed;
|
818
|
+
handleCreate: function (modelName, options) {
|
803
819
|
options = options === undefined ? {} : options;
|
820
|
+
var succeed = options.succeed === undefined ? true : options.succeed;
|
821
|
+
var match = options.match || {};
|
822
|
+
var returnArgs = options.returns || {};
|
823
|
+
|
804
824
|
var url = this.buildURL(modelName);
|
805
825
|
var definition = FactoryGuy.modelDefinitions[modelName];
|
826
|
+
|
827
|
+
var record = this.getStore().createRecord(modelName, match);
|
828
|
+
var expectedRequest = {};
|
829
|
+
expectedRequest[modelName] = record.serialize();
|
830
|
+
|
831
|
+
var httpOptions = {type: 'POST', data: JSON.stringify(expectedRequest)};
|
832
|
+
|
833
|
+
var modelType = this.getStore().modelFor(modelName)
|
834
|
+
|
806
835
|
var responseJson = {};
|
807
|
-
var httpOptions = { type: 'POST' };
|
808
836
|
if (succeed) {
|
809
|
-
responseJson[modelName] = $.extend(
|
837
|
+
responseJson[modelName] = $.extend({id: definition.nextId()}, match, returnArgs);
|
838
|
+
// Remove belongsTo associations since they will already be set when you called
|
839
|
+
// createRecord, and included them in those attributes
|
840
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
841
|
+
if (relationship.kind == 'belongsTo') {
|
842
|
+
delete responseJson[modelName][relationship.key];
|
843
|
+
}
|
844
|
+
})
|
810
845
|
} else {
|
811
846
|
httpOptions.status = 500;
|
812
847
|
}
|
848
|
+
|
813
849
|
this.stubEndpointForHttpRequest(url, responseJson, httpOptions);
|
814
850
|
},
|
815
|
-
|
816
851
|
/**
|
817
852
|
Handling ajax PUT ( update record ) for a model type. You can mock
|
818
853
|
failed update by passing in success argument as false.
|
@@ -851,6 +886,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
851
886
|
}
|
852
887
|
});
|
853
888
|
|
889
|
+
|
854
890
|
FactoryGuy.testMixin = FactoryGuyTestMixin;
|
855
891
|
FactoryGuy.sequence = Sequence;
|
856
892
|
FactoryGuy.sequence.missingSequenceError = MissingSequenceError;
|
@@ -1 +1 @@
|
|
1
|
-
(function(){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}};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"){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)};var FactoryGuy={modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},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){console.log("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)},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){if(!modelClass.FIXTURES){modelClass.FIXTURES=[]}modelClass.FIXTURES.push(fixture);return fixture},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(){var store=this;var fixture=FactoryGuy.build.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var modelType=store.modelFor(modelName);if(this.usingFixtureAdapter()){this.setAssociationsForFixtureAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{return store.makeModel(modelType,fixture)}},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},makeList:function(){var arr=[];var number=arguments[1];for(var i=0;i<number;i++){arr.push(this.makeFixture.apply(this,arguments))}return arr},setAssociationsForFixtureAdapter:function(modelType,modelName,fixture){var self=this;var adapter=this.adapterFor("application");Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var hasManyRelation=fixture[relationship.key];if(hasManyRelation){$.each(fixture[relationship.key],function(index,object){var id=object;if(Ember.typeOf(object)=="object"){id=object.id;hasManyRelation[index]=id}var hasManyfixtures=adapter.fixturesForType(relationship.type);var fixture=adapter.findFixtureById(hasManyfixtures,id);fixture[modelName]=fixture.id})}}if(relationship.kind=="belongsTo"){var 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)}}})},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){if(this.usingFixtureAdapter()){var model=this.modelFor(modelName);FactoryGuy.pushFixture(model,payload)}else{this._super(type,payload)}}})})();var FactoryGuyTestMixin=Em.Mixin.create({setup:function(app){this.set("container",app.__container__);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(){var store=this.getStore();return store.makeFixture.apply(store,arguments)},getStore:function(){return this.get("container").lookup("store:main")},pushPayload:function(type,hash){return this.getStore().pushPayload(type,hash)},pushRecord:function(type,hash){return this.getStore().push(type,hash)},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.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 store=this.getStore();store.makeList.apply(store,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};responseJson[modelName]=[];var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{type:"GET"})},handleCreate:function(modelName,options,succeed){succeed=succeed===undefined?true:succeed;options=options===undefined?{}:options;var url=this.buildURL(modelName);var definition=FactoryGuy.modelDefinitions[modelName];var responseJson={};var httpOptions={type:"POST"};if(succeed){responseJson[modelName]=$.extend(options,{id:definition.nextId()})}else{httpOptions.status=500}this.stubEndpointForHttpRequest(url,responseJson,httpOptions)},handleUpdate:function(type,id,succeed){succeed=succeed===undefined?true:succeed;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()}});FactoryGuy.testMixin=FactoryGuyTestMixin;FactoryGuy.sequence=Sequence;FactoryGuy.sequence.missingSequenceError=MissingSequenceError;if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports){exports=module.exports=FactoryGuy}exports.FactoryGuy=FactoryGuy}if(typeof define==="function"&&define.amd){define("factory-guy",[],function(){return FactoryGuy})}if(typeof window==="object"&&typeof window.document==="object"){window.FactoryGuy=FactoryGuy}})();
|
1
|
+
(function(){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}};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"){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)};var FactoryGuy={modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},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){console.log("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)},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){if(!modelClass.FIXTURES){modelClass.FIXTURES=[]}modelClass.FIXTURES.push(fixture);return fixture},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(){var store=this;var fixture=FactoryGuy.build.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var modelType=store.modelFor(modelName);if(this.usingFixtureAdapter()){this.setAssociationsForFixtureAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{return store.makeModel(modelType,fixture)}},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},makeList:function(){var arr=[];var number=arguments[1];for(var i=0;i<number;i++){arr.push(this.makeFixture.apply(this,arguments))}return arr},setAssociationsForFixtureAdapter:function(modelType,modelName,fixture){var self=this;var adapter=this.adapterFor("application");Ember.get(modelType,"relationshipsByName").forEach(function(relationship,name){if(relationship.kind=="hasMany"){var hasManyRelation=fixture[relationship.key];if(hasManyRelation){$.each(fixture[relationship.key],function(index,object){var id=object;if(Ember.typeOf(object)=="object"){id=object.id;hasManyRelation[index]=id}var hasManyfixtures=adapter.fixturesForType(relationship.type);var fixture=adapter.findFixtureById(hasManyfixtures,id);fixture[modelName]=fixture.id})}}if(relationship.kind=="belongsTo"){var 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)}}})},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){if(this.usingFixtureAdapter()){var model=this.modelFor(modelName);FactoryGuy.pushFixture(model,payload)}else{this._super(type,payload)}}})})();var FactoryGuyTestMixin=Em.Mixin.create({setup:function(app){this.set("container",app.__container__);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(){var store=this.getStore();return store.makeFixture.apply(store,arguments)},getStore:function(){return this.get("container").lookup("store:main")},pushPayload:function(type,hash){return this.getStore().pushPayload(type,hash)},pushRecord:function(type,hash){return this.getStore().push(type,hash)},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.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 store=this.getStore();store.makeList.apply(store,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var responseJson={};responseJson[modelName]=[];var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{type:"GET"})},handleCreate:function(modelName,options){options=options===undefined?{}:options;var succeed=options.succeed===undefined?true:options.succeed;var match=options.match||{};var returnArgs=options.returns||{};var url=this.buildURL(modelName);var definition=FactoryGuy.modelDefinitions[modelName];var record=this.getStore().createRecord(modelName,match);var expectedRequest={};expectedRequest[modelName]=record.serialize();var httpOptions={type:"POST",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(type,id,succeed){succeed=succeed===undefined?true:succeed;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()}});FactoryGuy.testMixin=FactoryGuyTestMixin;FactoryGuy.sequence=Sequence;FactoryGuy.sequence.missingSequenceError=MissingSequenceError;if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports){exports=module.exports=FactoryGuy}exports.FactoryGuy=FactoryGuy}if(typeof define==="function"&&define.amd){define("factory-guy",[],function(){return FactoryGuy})}if(typeof window==="object"&&typeof window.document==="object"){window.FactoryGuy=FactoryGuy}})();
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = "ember-data-factory-guy"
|
4
|
-
s.version = "0.8.
|
4
|
+
s.version = "0.8.2"
|
5
5
|
s.platform = Gem::Platform::RUBY
|
6
6
|
s.authors = ["Daniel Sudol", "Alex Opak"]
|
7
7
|
s.email = ["dansudol@yahoo.com", "opak.alexandr@gmail.com"]
|
data/package.json
CHANGED
@@ -103,26 +103,61 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
103
103
|
Handling ajax POST ( create record ) for a model. You can mock
|
104
104
|
failed create by passing in success argument as false.
|
105
105
|
|
106
|
+
```js
|
107
|
+
handleCreate('post')
|
108
|
+
handleCreate('post', { match: {title: 'foo'} )
|
109
|
+
handleCreate('post', { match: {title: 'foo', user: user} )
|
110
|
+
handleCreate('post', { returns: {createdAt: new Date()} )
|
111
|
+
handleCreate('post', { match: {title: 'foo'}, returns: {createdAt: new Date()} )
|
112
|
+
handleCreate('post', { match: {title: 'foo'}, success: false} } )
|
113
|
+
```
|
114
|
+
|
115
|
+
match - attributes that must be in request json,
|
116
|
+
returns - attributes to include in response json,
|
117
|
+
succeed - flag to indicate if the request should succeed ( default is true )
|
118
|
+
|
119
|
+
Note that any attributes in match will be added to the response json automatically,
|
120
|
+
so you don't need to include them in the returns hash as well.
|
121
|
+
|
122
|
+
Also, if you match on a belongsTo association, you don't have to include that in the
|
123
|
+
returns hash.
|
124
|
+
|
106
125
|
@param {String} modelName name of model your creating like 'profile' for Profile
|
107
|
-
@param {Object} options hash of
|
108
|
-
@param {Boolean} succeed optional flag to indicate if the request
|
109
|
-
should succeed ( default is true )
|
126
|
+
@param {Object} options hash of options for handling request
|
110
127
|
*/
|
111
|
-
handleCreate: function (modelName, options
|
112
|
-
succeed = succeed === undefined ? true : succeed;
|
128
|
+
handleCreate: function (modelName, options) {
|
113
129
|
options = options === undefined ? {} : options;
|
130
|
+
var succeed = options.succeed === undefined ? true : options.succeed;
|
131
|
+
var match = options.match || {};
|
132
|
+
var returnArgs = options.returns || {};
|
133
|
+
|
114
134
|
var url = this.buildURL(modelName);
|
115
135
|
var definition = FactoryGuy.modelDefinitions[modelName];
|
136
|
+
|
137
|
+
var record = this.getStore().createRecord(modelName, match);
|
138
|
+
var expectedRequest = {};
|
139
|
+
expectedRequest[modelName] = record.serialize();
|
140
|
+
|
141
|
+
var httpOptions = {type: 'POST', data: JSON.stringify(expectedRequest)};
|
142
|
+
|
143
|
+
var modelType = this.getStore().modelFor(modelName)
|
144
|
+
|
116
145
|
var responseJson = {};
|
117
|
-
var httpOptions = { type: 'POST' };
|
118
146
|
if (succeed) {
|
119
|
-
responseJson[modelName] = $.extend(
|
147
|
+
responseJson[modelName] = $.extend({id: definition.nextId()}, match, returnArgs);
|
148
|
+
// Remove belongsTo associations since they will already be set when you called
|
149
|
+
// createRecord, and included them in those attributes
|
150
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
151
|
+
if (relationship.kind == 'belongsTo') {
|
152
|
+
delete responseJson[modelName][relationship.key];
|
153
|
+
}
|
154
|
+
})
|
120
155
|
} else {
|
121
156
|
httpOptions.status = 500;
|
122
157
|
}
|
158
|
+
|
123
159
|
this.stubEndpointForHttpRequest(url, responseJson, httpOptions);
|
124
160
|
},
|
125
|
-
|
126
161
|
/**
|
127
162
|
Handling ajax PUT ( update record ) for a model type. You can mock
|
128
163
|
failed update by passing in success argument as false.
|
@@ -160,3 +195,4 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
160
195
|
$.mockjax.clear();
|
161
196
|
}
|
162
197
|
});
|
198
|
+
|
@@ -65,12 +65,12 @@ test("makeFixture with fixture options", function() {
|
|
65
65
|
ok(profile.get('description') == 'dude');
|
66
66
|
});
|
67
67
|
|
68
|
-
test("makeFixture with traits", function() {
|
68
|
+
test("makeFixture with attributes in traits", function() {
|
69
69
|
var profile = store.makeFixture('profile', 'goofy_description');
|
70
70
|
ok(profile.get('description') == 'goofy');
|
71
71
|
});
|
72
72
|
|
73
|
-
test("makeFixture with traits and fixture options ", function() {
|
73
|
+
test("makeFixture with attributes in traits and fixture options ", function() {
|
74
74
|
var profile = store.makeFixture('profile', 'goofy_description', {description: 'dude'});
|
75
75
|
ok(profile.get('description') == 'dude');
|
76
76
|
});
|
@@ -247,6 +247,17 @@ test("hasMany associations defined with traits", function() {
|
|
247
247
|
})
|
248
248
|
|
249
249
|
|
250
|
+
|
251
|
+
test("belongsTo associations defined with traits", function() {
|
252
|
+
var hat1 = store.makeFixture('hat', 'with_user');
|
253
|
+
equal(hat1.get('user') instanceof User, true)
|
254
|
+
|
255
|
+
var hat2 = store.makeFixture('hat', 'with_user', 'with_outfit');
|
256
|
+
equal(hat2.get('user') instanceof User, true)
|
257
|
+
equal(hat2.get('outfit') instanceof Outfit, true)
|
258
|
+
})
|
259
|
+
|
260
|
+
|
250
261
|
test("with (nested json fixture) belongsTo has a hasMany association which has a belongsTo", function() {
|
251
262
|
var project = store.makeFixture('project', 'with_user_having_hats_belonging_to_outfit');
|
252
263
|
var user = project.get('user');
|
@@ -38,9 +38,14 @@ module('FactoryGuyTestMixin (using mockjax) with DS.RESTAdapter', {
|
|
38
38
|
|
39
39
|
asyncTest("#handleCreate the basic", function() {
|
40
40
|
var customDescription = "special description"
|
41
|
-
testHelper.handleCreate('profile', {description: customDescription})
|
42
41
|
|
43
|
-
|
42
|
+
testHelper.handleCreate('profile', {
|
43
|
+
match: {description: customDescription}
|
44
|
+
})
|
45
|
+
|
46
|
+
store.createRecord('profile', {
|
47
|
+
description: customDescription
|
48
|
+
}).save().then(function(profile) {
|
44
49
|
ok(profile instanceof Profile)
|
45
50
|
ok(profile.get('description') == customDescription)
|
46
51
|
start();
|
@@ -106,11 +111,16 @@ module('FactoryGuyTestMixin (using mockjax) with DS.ActiveModelAdapter', {
|
|
106
111
|
|
107
112
|
/////// handleCreate //////////
|
108
113
|
|
109
|
-
asyncTest("#handleCreate
|
114
|
+
asyncTest("#handleCreate matches attributes and returns these", function() {
|
110
115
|
var customDescription = "special description"
|
111
|
-
testHelper.handleCreate('profile', {description: customDescription})
|
112
116
|
|
113
|
-
|
117
|
+
testHelper.handleCreate('profile', {
|
118
|
+
match: {description: customDescription}
|
119
|
+
})
|
120
|
+
|
121
|
+
store.createRecord('profile', {
|
122
|
+
description: customDescription
|
123
|
+
}).save().then(function(profile) {
|
114
124
|
ok(profile instanceof Profile)
|
115
125
|
ok(profile.id == 1)
|
116
126
|
ok(profile.get('description') == customDescription)
|
@@ -118,42 +128,81 @@ asyncTest("#handleCreate the basic", function() {
|
|
118
128
|
});
|
119
129
|
});
|
120
130
|
|
121
|
-
|
131
|
+
|
132
|
+
asyncTest("#handleCreate returns attributes", function() {
|
133
|
+
var date = new Date()
|
134
|
+
|
135
|
+
testHelper.handleCreate('profile', {
|
136
|
+
returns: {created_at: date}
|
137
|
+
})
|
138
|
+
|
139
|
+
store.createRecord('profile').save().then(function(profile) {
|
140
|
+
ok(profile.get('created_at') == date.toString())
|
141
|
+
start();
|
142
|
+
});
|
143
|
+
});
|
144
|
+
|
145
|
+
asyncTest("#handleCreate returns camelCase attributes", function() {
|
146
|
+
var customDescription = "special description"
|
147
|
+
|
148
|
+
testHelper.handleCreate('profile', {
|
149
|
+
returns: {camel_case_description: customDescription}
|
150
|
+
})
|
151
|
+
|
152
|
+
store.createRecord('profile', {
|
153
|
+
camel_case_description: 'description'
|
154
|
+
}).save().then(function(profile) {
|
155
|
+
ok(profile.get('camelCaseDescription') == customDescription)
|
156
|
+
start();
|
157
|
+
});
|
158
|
+
});
|
159
|
+
|
160
|
+
asyncTest("#handleCreate match belongsTo association", function() {
|
122
161
|
var company = store.makeFixture('company')
|
123
|
-
testHelper.handleCreate('profile')
|
162
|
+
testHelper.handleCreate('profile', {match:{ company: company}})
|
124
163
|
|
125
164
|
store.createRecord('profile', {company: company}).save().then(function(profile) {
|
126
|
-
ok(profile instanceof Profile)
|
127
|
-
ok(profile.id == 1)
|
128
165
|
ok(profile.get('company') == company)
|
129
166
|
start();
|
130
167
|
});
|
131
168
|
});
|
132
169
|
|
133
|
-
asyncTest("#handleCreate
|
170
|
+
asyncTest("#handleCreate match belongsTo polymorphic association", function() {
|
134
171
|
var group = store.makeFixture('group')
|
135
|
-
testHelper.handleCreate('profile')
|
172
|
+
testHelper.handleCreate('profile', {match:{ group: group}})
|
136
173
|
|
137
174
|
store.createRecord('profile', {group: group}).save().then(function(profile) {
|
138
|
-
ok(profile instanceof Profile)
|
139
|
-
ok(profile.id == 1)
|
140
175
|
ok(profile.get('group') == group)
|
141
176
|
start();
|
142
177
|
});
|
143
178
|
});
|
144
179
|
|
145
|
-
|
180
|
+
|
181
|
+
asyncTest("#handleCreate match attributes and return attributes", function() {
|
182
|
+
var date = new Date()
|
146
183
|
var customDescription = "special description"
|
147
|
-
|
184
|
+
var company = store.makeFixture('company')
|
185
|
+
var group = store.makeFixture('big_group')
|
148
186
|
|
149
|
-
|
150
|
-
|
187
|
+
testHelper.handleCreate('profile', {
|
188
|
+
match: {description: customDescription, company: company, group: group},
|
189
|
+
returns: {created_at: new Date()}
|
190
|
+
})
|
191
|
+
|
192
|
+
store.createRecord('profile', {
|
193
|
+
description: customDescription, company: company, group: group
|
194
|
+
}).save().then(function(profile) {
|
151
195
|
start();
|
196
|
+
ok(profile.get('created_at') == date.toString())
|
197
|
+
ok(profile.get('group') == group)
|
198
|
+
ok(profile.get('company') == company)
|
199
|
+
ok(profile.get('description') == customDescription)
|
152
200
|
});
|
153
201
|
});
|
154
202
|
|
203
|
+
|
155
204
|
asyncTest("#handleCreate failure", function() {
|
156
|
-
testHelper.handleCreate('profile', {}
|
205
|
+
testHelper.handleCreate('profile', { succeed: false } )
|
157
206
|
|
158
207
|
store.createRecord('profile').save()
|
159
208
|
.then(
|
@@ -166,6 +215,24 @@ asyncTest("#handleCreate failure", function() {
|
|
166
215
|
});
|
167
216
|
|
168
217
|
|
218
|
+
asyncTest("#handleCreate match but still fail", function() {
|
219
|
+
var description = "special description"
|
220
|
+
|
221
|
+
testHelper.handleCreate('profile', {
|
222
|
+
match: {description: description}, succeed: false
|
223
|
+
})
|
224
|
+
|
225
|
+
store.createRecord('profile', {description: description}).save()
|
226
|
+
.then(
|
227
|
+
function() {},
|
228
|
+
function() {
|
229
|
+
ok(true)
|
230
|
+
start();
|
231
|
+
}
|
232
|
+
)
|
233
|
+
});
|
234
|
+
|
235
|
+
|
169
236
|
|
170
237
|
/////// handleFindMany //////////
|
171
238
|
|
@@ -241,6 +241,15 @@ test("hasMany associations defined with traits", function() {
|
|
241
241
|
ok(user.get('projects.lastObject.user') == user)
|
242
242
|
})
|
243
243
|
|
244
|
+
test("belongsTo associations defined with traits", function() {
|
245
|
+
var hat1 = store.makeFixture('hat', 'with_user');
|
246
|
+
equal(hat1.get('user') instanceof User, true)
|
247
|
+
|
248
|
+
var hat2 = store.makeFixture('hat', 'with_user', 'with_outfit');
|
249
|
+
equal(hat2.get('user') instanceof User, true)
|
250
|
+
equal(hat2.get('outfit') instanceof Outfit, true)
|
251
|
+
})
|
252
|
+
|
244
253
|
|
245
254
|
test("with (nested json fixture) belongsTo has a hasMany association which has a belongsTo", function() {
|
246
255
|
var project = store.makeFixture('project', 'with_user_having_hats_belonging_to_outfit');
|
@@ -7,11 +7,9 @@ FactoryGuy.define('hat', {
|
|
7
7
|
type: 'BigHat'
|
8
8
|
},
|
9
9
|
traits: {
|
10
|
-
|
11
|
-
|
12
|
-
},
|
13
|
-
belonging_to_outfit: {
|
14
|
-
outfit: FactoryGuy.belongsTo('outfit')
|
15
|
-
}
|
10
|
+
with_user: { user: {} },
|
11
|
+
belonging_to_user: { user: {} },
|
12
|
+
with_outfit: { outfit: {} },
|
13
|
+
belonging_to_outfit: { outfit: {} }
|
16
14
|
}
|
17
15
|
})
|
@@ -68,6 +68,7 @@ TestHelper = Ember.Object.createWithMixins(FactoryGuy.testMixin,{
|
|
68
68
|
|
69
69
|
container.register('serializer:-default', serializer);
|
70
70
|
container.register('transform:string', DS.StringTransform);
|
71
|
+
container.register('transform:date', DS.DateTransform);
|
71
72
|
container.injection('serializer', 'store', 'store:main');
|
72
73
|
|
73
74
|
env.store = container.lookup('store:main');
|
data/tests/test_setup.js
CHANGED
@@ -54,12 +54,10 @@ FactoryGuy.define('hat', {
|
|
54
54
|
type: 'BigHat'
|
55
55
|
},
|
56
56
|
traits: {
|
57
|
-
|
58
|
-
|
59
|
-
},
|
60
|
-
belonging_to_outfit: {
|
61
|
-
outfit: FactoryGuy.belongsTo('outfit')
|
62
|
-
}
|
57
|
+
with_user: { user: {} },
|
58
|
+
belonging_to_user: { user: {} },
|
59
|
+
with_outfit: { outfit: {} },
|
60
|
+
belonging_to_outfit: { outfit: {} }
|
63
61
|
}
|
64
62
|
})
|
65
63
|
FactoryGuy.define('soft_material', {
|
@@ -242,6 +240,7 @@ Person = DS.Model.extend({
|
|
242
240
|
})
|
243
241
|
|
244
242
|
Profile = DS.Model.extend({
|
243
|
+
created_at: DS.attr('date'),
|
245
244
|
description: DS.attr('string'),
|
246
245
|
camelCaseDescription: DS.attr('string'),
|
247
246
|
snake_case_description: DS.attr('string'),
|
@@ -791,26 +791,61 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
791
791
|
Handling ajax POST ( create record ) for a model. You can mock
|
792
792
|
failed create by passing in success argument as false.
|
793
793
|
|
794
|
+
```js
|
795
|
+
handleCreate('post')
|
796
|
+
handleCreate('post', { match: {title: 'foo'} )
|
797
|
+
handleCreate('post', { match: {title: 'foo', user: user} )
|
798
|
+
handleCreate('post', { returns: {createdAt: new Date()} )
|
799
|
+
handleCreate('post', { match: {title: 'foo'}, returns: {createdAt: new Date()} )
|
800
|
+
handleCreate('post', { match: {title: 'foo'}, success: false} } )
|
801
|
+
```
|
802
|
+
|
803
|
+
match - attributes that must be in request json,
|
804
|
+
returns - attributes to include in response json,
|
805
|
+
succeed - flag to indicate if the request should succeed ( default is true )
|
806
|
+
|
807
|
+
Note that any attributes in match will be added to the response json automatically,
|
808
|
+
so you don't need to include them in the returns hash as well.
|
809
|
+
|
810
|
+
Also, if you match on a belongsTo association, you don't have to include that in the
|
811
|
+
returns hash.
|
812
|
+
|
794
813
|
@param {String} modelName name of model your creating like 'profile' for Profile
|
795
|
-
@param {Object} options hash of
|
796
|
-
@param {Boolean} succeed optional flag to indicate if the request
|
797
|
-
should succeed ( default is true )
|
814
|
+
@param {Object} options hash of options for handling request
|
798
815
|
*/
|
799
|
-
handleCreate: function (modelName, options
|
800
|
-
succeed = succeed === undefined ? true : succeed;
|
816
|
+
handleCreate: function (modelName, options) {
|
801
817
|
options = options === undefined ? {} : options;
|
818
|
+
var succeed = options.succeed === undefined ? true : options.succeed;
|
819
|
+
var match = options.match || {};
|
820
|
+
var returnArgs = options.returns || {};
|
821
|
+
|
802
822
|
var url = this.buildURL(modelName);
|
803
823
|
var definition = FactoryGuy.modelDefinitions[modelName];
|
824
|
+
|
825
|
+
var record = this.getStore().createRecord(modelName, match);
|
826
|
+
var expectedRequest = {};
|
827
|
+
expectedRequest[modelName] = record.serialize();
|
828
|
+
|
829
|
+
var httpOptions = {type: 'POST', data: JSON.stringify(expectedRequest)};
|
830
|
+
|
831
|
+
var modelType = this.getStore().modelFor(modelName)
|
832
|
+
|
804
833
|
var responseJson = {};
|
805
|
-
var httpOptions = { type: 'POST' };
|
806
834
|
if (succeed) {
|
807
|
-
responseJson[modelName] = $.extend(
|
835
|
+
responseJson[modelName] = $.extend({id: definition.nextId()}, match, returnArgs);
|
836
|
+
// Remove belongsTo associations since they will already be set when you called
|
837
|
+
// createRecord, and included them in those attributes
|
838
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
839
|
+
if (relationship.kind == 'belongsTo') {
|
840
|
+
delete responseJson[modelName][relationship.key];
|
841
|
+
}
|
842
|
+
})
|
808
843
|
} else {
|
809
844
|
httpOptions.status = 500;
|
810
845
|
}
|
846
|
+
|
811
847
|
this.stubEndpointForHttpRequest(url, responseJson, httpOptions);
|
812
848
|
},
|
813
|
-
|
814
849
|
/**
|
815
850
|
Handling ajax PUT ( update record ) for a model type. You can mock
|
816
851
|
failed update by passing in success argument as false.
|
@@ -849,6 +884,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
849
884
|
}
|
850
885
|
});
|
851
886
|
|
887
|
+
|
852
888
|
/*!
|
853
889
|
* MockJax - jQuery Plugin to Mock Ajax requests
|
854
890
|
*
|
@@ -964,9 +1000,11 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
964
1000
|
return null;
|
965
1001
|
}
|
966
1002
|
}
|
967
|
-
|
968
1003
|
// Inspect the data submitted in the request (either POST body or GET query string)
|
969
1004
|
if ( handler.data ) {
|
1005
|
+
// console.log('request.data', requestSettings.data )
|
1006
|
+
// console.log('handler.data', handler.data )
|
1007
|
+
// console.log('data equal', isMockDataEqual(handler.data, requestSettings.data) )
|
970
1008
|
if ( ! requestSettings.data || !isMockDataEqual(handler.data, requestSettings.data) ) {
|
971
1009
|
// They're not identical, do not mock this request
|
972
1010
|
return null;
|
@@ -978,7 +1016,6 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
978
1016
|
// The request type doesn't match (GET vs. POST)
|
979
1017
|
return null;
|
980
1018
|
}
|
981
|
-
|
982
1019
|
return handler;
|
983
1020
|
}
|
984
1021
|
|
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.8.
|
4
|
+
version: 0.8.2
|
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: 2014-11-
|
12
|
+
date: 2014-11-27 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easily create Fixtures for Ember Data
|
15
15
|
email:
|