ember-data-factory-guy 0.9.6 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gruntfile.js +1 -0
- data/README.md +8 -4
- data/bower.json +1 -1
- data/dist/amd/factory-guy.js +34 -8
- data/dist/ember-data-factory-guy.js +34 -8
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/package.json +1 -1
- data/src/factory_guy_test_mixin.js +31 -6
- data/src/mock_create_request.js +3 -2
- data/tests/factory_guy_test_mixin_test.js +4 -3
- data/vendor/assets/javascripts/ember_data_factory_guy.js +106 -6
- 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: 635976ca31386b3b5023526335088bfd90ca80b6
|
4
|
+
data.tar.gz: 3cd896fa342d199cabe1f977ae5064170e0f015f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc167c9d68080b700f68ce04f97caff52e377582fa9693c145aa9be5beadeb14a8f78ae1b3888f14b5ac13203fa3416c04c8c2892eaeef1924b8b734a86e247f
|
7
|
+
data.tar.gz: 6138a1ed022f6ee1a2d941dd9c214f5248ba79ab25e4d0dec18ba513a9b0b66c4f89ffbc83fe43c12596543a5a0eb4fda2dfe898656c88ffa220cd3661383fa5
|
data/Gruntfile.js
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Ember Data Factory Guy [![Build Status](https://secure.travis-ci.org/danielspaniel/ember-data-factory-guy.png?branch=master)](http://travis-ci.org/danielspaniel/ember-data-factory-guy)
|
2
2
|
|
3
|
+
Feel the thrill and enjoyment of testing when using Factories instead of Fixtures.
|
4
|
+
Factories simplify the process of testing, making you more efficient and your tests more readable.
|
5
|
+
|
6
|
+
|
3
7
|
*NOTE*
|
4
8
|
|
5
9
|
ember-data is changing the way they are doing relationships in 1.0.0-beta.10 and above
|
@@ -10,7 +14,7 @@ of ember-data-factory-guy.
|
|
10
14
|
- 0.6.4 -> ember-data-1.0.0-beta.8 and under
|
11
15
|
- 0.7.1.1 -> ember-data-1.0.0-beta.10
|
12
16
|
- 0.8.6 -> ember-data-1.0.0-beta.11
|
13
|
-
- 0.9.
|
17
|
+
- 0.9.6 -> ember-data-1.0.0-beta.12
|
14
18
|
|
15
19
|
**Waiting for ember-data-1.0.0-beta.15 to make upgrade release, since there are a few issues with
|
16
20
|
ember-data-1.0.0-beta.14.1 that make it difficult to use**
|
@@ -42,7 +46,7 @@ gem 'ember-data-factory-guy', group: test
|
|
42
46
|
or for particular version:
|
43
47
|
|
44
48
|
```ruby
|
45
|
-
gem 'ember-data-factory-guy', '0.9.
|
49
|
+
gem 'ember-data-factory-guy', '0.9.6', group: test
|
46
50
|
```
|
47
51
|
|
48
52
|
then:
|
@@ -77,7 +81,7 @@ or for particular version:
|
|
77
81
|
"dependencies": {
|
78
82
|
"foo-dependency": "latest",
|
79
83
|
"other-foo-dependency": "latest",
|
80
|
-
"ember-data-factory-guy": "0.9.
|
84
|
+
"ember-data-factory-guy": "0.9.6"
|
81
85
|
}
|
82
86
|
```
|
83
87
|
|
@@ -736,7 +740,7 @@ Realistically, you will have code in a view action or controller action that wil
|
|
736
740
|
|
737
741
|
In this case, you are are creating a 'project' record with a specific name, and belonging
|
738
742
|
to a particular user. To mock this createRecord call here are a few ways to do this using
|
739
|
-
|
743
|
+
chainable methods, or options hash.
|
740
744
|
|
741
745
|
|
742
746
|
######Using chainable methods
|
data/bower.json
CHANGED
data/dist/amd/factory-guy.js
CHANGED
@@ -584,8 +584,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
584
584
|
|
585
585
|
this.calculate = function() {
|
586
586
|
if (matchArgs) {
|
587
|
-
var
|
588
|
-
expectedRequest =
|
587
|
+
var tmpRecord = store.createRecord(modelName, matchArgs);
|
588
|
+
expectedRequest = tmpRecord.serialize(matchArgs);
|
589
|
+
tmpRecord.deleteRecord();
|
589
590
|
}
|
590
591
|
|
591
592
|
if (succeed) {
|
@@ -1001,6 +1002,34 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1001
1002
|
var type = this.getStore().modelFor(typeName);
|
1002
1003
|
return this.getStore().adapterFor(type).buildURL(type.typeKey, id);
|
1003
1004
|
},
|
1005
|
+
/**
|
1006
|
+
Map many json objects to response json.
|
1007
|
+
|
1008
|
+
Allows custom serializing mappings and meta data to be added to requests.
|
1009
|
+
|
1010
|
+
@param {String} modelName model name
|
1011
|
+
@param {Object} json Json objects from records.map
|
1012
|
+
@return {Object} responseJson
|
1013
|
+
*/
|
1014
|
+
mapFindAll: function(modelName, json) {
|
1015
|
+
var responseJson = {};
|
1016
|
+
responseJson[modelName.pluralize()] = json;
|
1017
|
+
return responseJson;
|
1018
|
+
},
|
1019
|
+
/**
|
1020
|
+
Map single object to response json.
|
1021
|
+
|
1022
|
+
Allows custom serializing mappings and meta data to be added to requests.
|
1023
|
+
|
1024
|
+
@param {String} modelName model name
|
1025
|
+
@param {Object} json Json object from record.toJSON
|
1026
|
+
@return {Object} responseJson
|
1027
|
+
*/
|
1028
|
+
mapFind:function(modelName, json){
|
1029
|
+
var responseJson = {};
|
1030
|
+
responseJson[modelName.pluralize()] = json;
|
1031
|
+
return responseJson;
|
1032
|
+
},
|
1004
1033
|
/**
|
1005
1034
|
Handling ajax GET for finding all records for a type of model.
|
1006
1035
|
You can mock failed find by passing in success argument as false.
|
@@ -1026,9 +1055,8 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1026
1055
|
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
1027
1056
|
var name = arguments[0];
|
1028
1057
|
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
1029
|
-
var responseJson = {};
|
1030
1058
|
var json = records.map(function(record) {return record.toJSON({includeId: true})});
|
1031
|
-
responseJson
|
1059
|
+
var responseJson = this.mapFindAll(modelName, json);
|
1032
1060
|
var url = this.buildURL(modelName);
|
1033
1061
|
this.stubEndpointForHttpRequest(url, responseJson);
|
1034
1062
|
},
|
@@ -1070,9 +1098,8 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1070
1098
|
modelName = FactoryGuy.lookupModelForFixtureName(name);
|
1071
1099
|
}
|
1072
1100
|
|
1073
|
-
var responseJson = {};
|
1074
1101
|
var json = record.toJSON({includeId: true});
|
1075
|
-
responseJson
|
1102
|
+
var responseJson = this.mapFind(modelName, json);
|
1076
1103
|
var url = this.buildURL(modelName, record.id);
|
1077
1104
|
this.stubEndpointForHttpRequest(url, responseJson);
|
1078
1105
|
},
|
@@ -1118,8 +1145,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1118
1145
|
records = []
|
1119
1146
|
}
|
1120
1147
|
var json = records.map(function(record) {return record.toJSON({includeId: true})})
|
1121
|
-
var responseJson =
|
1122
|
-
responseJson[modelName.pluralize()] = json;
|
1148
|
+
var responseJson = this.mapFindAll(modelName, json);
|
1123
1149
|
var url = this.buildURL(modelName);
|
1124
1150
|
this.stubEndpointForHttpRequest(url, responseJson, {urlParams: searchParams});
|
1125
1151
|
},
|
@@ -579,8 +579,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
579
579
|
|
580
580
|
this.calculate = function() {
|
581
581
|
if (matchArgs) {
|
582
|
-
var
|
583
|
-
expectedRequest =
|
582
|
+
var tmpRecord = store.createRecord(modelName, matchArgs);
|
583
|
+
expectedRequest = tmpRecord.serialize(matchArgs);
|
584
|
+
tmpRecord.deleteRecord();
|
584
585
|
}
|
585
586
|
|
586
587
|
if (succeed) {
|
@@ -996,6 +997,34 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
996
997
|
var type = this.getStore().modelFor(typeName);
|
997
998
|
return this.getStore().adapterFor(type).buildURL(type.typeKey, id);
|
998
999
|
},
|
1000
|
+
/**
|
1001
|
+
Map many json objects to response json.
|
1002
|
+
|
1003
|
+
Allows custom serializing mappings and meta data to be added to requests.
|
1004
|
+
|
1005
|
+
@param {String} modelName model name
|
1006
|
+
@param {Object} json Json objects from records.map
|
1007
|
+
@return {Object} responseJson
|
1008
|
+
*/
|
1009
|
+
mapFindAll: function(modelName, json) {
|
1010
|
+
var responseJson = {};
|
1011
|
+
responseJson[modelName.pluralize()] = json;
|
1012
|
+
return responseJson;
|
1013
|
+
},
|
1014
|
+
/**
|
1015
|
+
Map single object to response json.
|
1016
|
+
|
1017
|
+
Allows custom serializing mappings and meta data to be added to requests.
|
1018
|
+
|
1019
|
+
@param {String} modelName model name
|
1020
|
+
@param {Object} json Json object from record.toJSON
|
1021
|
+
@return {Object} responseJson
|
1022
|
+
*/
|
1023
|
+
mapFind:function(modelName, json){
|
1024
|
+
var responseJson = {};
|
1025
|
+
responseJson[modelName.pluralize()] = json;
|
1026
|
+
return responseJson;
|
1027
|
+
},
|
999
1028
|
/**
|
1000
1029
|
Handling ajax GET for finding all records for a type of model.
|
1001
1030
|
You can mock failed find by passing in success argument as false.
|
@@ -1021,9 +1050,8 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1021
1050
|
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
1022
1051
|
var name = arguments[0];
|
1023
1052
|
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
1024
|
-
var responseJson = {};
|
1025
1053
|
var json = records.map(function(record) {return record.toJSON({includeId: true})});
|
1026
|
-
responseJson
|
1054
|
+
var responseJson = this.mapFindAll(modelName, json);
|
1027
1055
|
var url = this.buildURL(modelName);
|
1028
1056
|
this.stubEndpointForHttpRequest(url, responseJson);
|
1029
1057
|
},
|
@@ -1065,9 +1093,8 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1065
1093
|
modelName = FactoryGuy.lookupModelForFixtureName(name);
|
1066
1094
|
}
|
1067
1095
|
|
1068
|
-
var responseJson = {};
|
1069
1096
|
var json = record.toJSON({includeId: true});
|
1070
|
-
responseJson
|
1097
|
+
var responseJson = this.mapFind(modelName, json);
|
1071
1098
|
var url = this.buildURL(modelName, record.id);
|
1072
1099
|
this.stubEndpointForHttpRequest(url, responseJson);
|
1073
1100
|
},
|
@@ -1113,8 +1140,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1113
1140
|
records = []
|
1114
1141
|
}
|
1115
1142
|
var json = records.map(function(record) {return record.toJSON({includeId: true})})
|
1116
|
-
var responseJson =
|
1117
|
-
responseJson[modelName.pluralize()] = json;
|
1143
|
+
var responseJson = this.mapFindAll(modelName, json);
|
1118
1144
|
var url = this.buildURL(modelName);
|
1119
1145
|
this.stubEndpointForHttpRequest(url, responseJson, {urlParams: searchParams});
|
1120
1146
|
},
|
@@ -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={}}}};var MockCreateRequest=function(url,store,modelName,options){var succeed=options.succeed===undefined?true:options.succeed;var matchArgs=options.match;var returnArgs=options.returns;var responseJson={};var expectedRequest={};var modelType=store.modelFor(modelName);this.calculate=function(){if(matchArgs){var record=store.createRecord(modelName,matchArgs);expectedRequest=record.serialize()}if(succeed){var definition=FactoryGuy.modelDefinitions[modelName];if(responseJson[modelName]){responseJson[modelName]=$.extend({id:responseJson[modelName].id},matchArgs,returnArgs)}else{responseJson[modelName]=$.extend({id:definition.nextId()},matchArgs,returnArgs)}Ember.get(modelType,"relationshipsByName").forEach(function(relationship){if(relationship.kind=="belongsTo"){delete responseJson[modelName][relationship.key]}})}};this.match=function(matches){matchArgs=matches;this.calculate();return this};this.andReturn=function(returns){returnArgs=returns;this.calculate();return this};this.andFail=function(){succeed=false;return this};this.handler=function(settings){if(settings.url!=url||settings.type!="POST"){return false}if(matchArgs){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}};this.calculate();$.mockjax(this.handler)};(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 url=this.buildURL(modelName);var store=this.getStore();var opts=options===undefined?{}:options;return new MockCreateRequest(url,store,modelName,opts)},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={}}}};var MockCreateRequest=function(url,store,modelName,options){var succeed=options.succeed===undefined?true:options.succeed;var matchArgs=options.match;var returnArgs=options.returns;var responseJson={};var expectedRequest={};var modelType=store.modelFor(modelName);this.calculate=function(){if(matchArgs){var tmpRecord=store.createRecord(modelName,matchArgs);expectedRequest=tmpRecord.serialize(matchArgs);tmpRecord.deleteRecord()}if(succeed){var definition=FactoryGuy.modelDefinitions[modelName];if(responseJson[modelName]){responseJson[modelName]=$.extend({id:responseJson[modelName].id},matchArgs,returnArgs)}else{responseJson[modelName]=$.extend({id:definition.nextId()},matchArgs,returnArgs)}Ember.get(modelType,"relationshipsByName").forEach(function(relationship){if(relationship.kind=="belongsTo"){delete responseJson[modelName][relationship.key]}})}};this.match=function(matches){matchArgs=matches;this.calculate();return this};this.andReturn=function(returns){returnArgs=returns;this.calculate();return this};this.andFail=function(){succeed=false;return this};this.handler=function(settings){if(settings.url!=url||settings.type!="POST"){return false}if(matchArgs){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}};this.calculate();$.mockjax(this.handler)};(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)},mapFindAll:function(modelName,json){var responseJson={};responseJson[modelName.pluralize()]=json;return responseJson},mapFind:function(modelName,json){var responseJson={};responseJson[modelName.pluralize()]=json;return responseJson},handleFindAll:function(){var records=FactoryGuy.makeList.apply(FactoryGuy,arguments);var name=arguments[0];var modelName=FactoryGuy.lookupModelForFixtureName(name);var json=records.map(function(record){return record.toJSON({includeId:true})});var responseJson=this.mapFindAll(modelName,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 json=record.toJSON({includeId:true});var responseJson=this.mapFind(modelName,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=this.mapFindAll(modelName,json);var url=this.buildURL(modelName);this.stubEndpointForHttpRequest(url,responseJson,{urlParams:searchParams})},handleCreate:function(modelName,options){var url=this.buildURL(modelName);var store=this.getStore();var opts=options===undefined?{}:options;return new MockCreateRequest(url,store,modelName,opts)},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
@@ -74,6 +74,34 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
74
74
|
var type = this.getStore().modelFor(typeName);
|
75
75
|
return this.getStore().adapterFor(type).buildURL(type.typeKey, id);
|
76
76
|
},
|
77
|
+
/**
|
78
|
+
Map many json objects to response json.
|
79
|
+
|
80
|
+
Allows custom serializing mappings and meta data to be added to requests.
|
81
|
+
|
82
|
+
@param {String} modelName model name
|
83
|
+
@param {Object} json Json objects from records.map
|
84
|
+
@return {Object} responseJson
|
85
|
+
*/
|
86
|
+
mapFindAll: function(modelName, json) {
|
87
|
+
var responseJson = {};
|
88
|
+
responseJson[modelName.pluralize()] = json;
|
89
|
+
return responseJson;
|
90
|
+
},
|
91
|
+
/**
|
92
|
+
Map single object to response json.
|
93
|
+
|
94
|
+
Allows custom serializing mappings and meta data to be added to requests.
|
95
|
+
|
96
|
+
@param {String} modelName model name
|
97
|
+
@param {Object} json Json object from record.toJSON
|
98
|
+
@return {Object} responseJson
|
99
|
+
*/
|
100
|
+
mapFind:function(modelName, json){
|
101
|
+
var responseJson = {};
|
102
|
+
responseJson[modelName.pluralize()] = json;
|
103
|
+
return responseJson;
|
104
|
+
},
|
77
105
|
/**
|
78
106
|
Handling ajax GET for finding all records for a type of model.
|
79
107
|
You can mock failed find by passing in success argument as false.
|
@@ -99,9 +127,8 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
99
127
|
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
100
128
|
var name = arguments[0];
|
101
129
|
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
102
|
-
var responseJson = {};
|
103
130
|
var json = records.map(function(record) {return record.toJSON({includeId: true})});
|
104
|
-
responseJson
|
131
|
+
var responseJson = this.mapFindAll(modelName, json);
|
105
132
|
var url = this.buildURL(modelName);
|
106
133
|
this.stubEndpointForHttpRequest(url, responseJson);
|
107
134
|
},
|
@@ -143,9 +170,8 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
143
170
|
modelName = FactoryGuy.lookupModelForFixtureName(name);
|
144
171
|
}
|
145
172
|
|
146
|
-
var responseJson = {};
|
147
173
|
var json = record.toJSON({includeId: true});
|
148
|
-
responseJson
|
174
|
+
var responseJson = this.mapFind(modelName, json);
|
149
175
|
var url = this.buildURL(modelName, record.id);
|
150
176
|
this.stubEndpointForHttpRequest(url, responseJson);
|
151
177
|
},
|
@@ -191,8 +217,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
191
217
|
records = []
|
192
218
|
}
|
193
219
|
var json = records.map(function(record) {return record.toJSON({includeId: true})})
|
194
|
-
var responseJson =
|
195
|
-
responseJson[modelName.pluralize()] = json;
|
220
|
+
var responseJson = this.mapFindAll(modelName, json);
|
196
221
|
var url = this.buildURL(modelName);
|
197
222
|
this.stubEndpointForHttpRequest(url, responseJson, {urlParams: searchParams});
|
198
223
|
},
|
data/src/mock_create_request.js
CHANGED
@@ -8,8 +8,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
8
8
|
|
9
9
|
this.calculate = function() {
|
10
10
|
if (matchArgs) {
|
11
|
-
var
|
12
|
-
expectedRequest =
|
11
|
+
var tmpRecord = store.createRecord(modelName, matchArgs);
|
12
|
+
expectedRequest = tmpRecord.serialize(matchArgs);
|
13
|
+
tmpRecord.deleteRecord();
|
13
14
|
}
|
14
15
|
|
15
16
|
if (succeed) {
|
@@ -43,12 +43,13 @@ asyncTest("#handleCreate the basic", function() {
|
|
43
43
|
testHelper.handleCreate('profile', {
|
44
44
|
match: {description: customDescription}
|
45
45
|
})
|
46
|
-
|
46
|
+
ok(store.all('profile').get('content.length') == 0)
|
47
47
|
store.createRecord('profile', {
|
48
48
|
description: customDescription
|
49
49
|
}).save().then(function(profile) {
|
50
|
-
ok(profile
|
51
|
-
ok(profile
|
50
|
+
ok(store.all('profile').get('content.length') == 1, 'No extra records created')
|
51
|
+
ok(profile instanceof Profile, 'Creates the correct type of record')
|
52
|
+
ok(profile.get('description') == customDescription, 'Passes along the match attributes')
|
52
53
|
start();
|
53
54
|
});
|
54
55
|
});
|
@@ -569,6 +569,81 @@ var FactoryGuy = {
|
|
569
569
|
}
|
570
570
|
};
|
571
571
|
|
572
|
+
var MockCreateRequest = function(url, store, modelName, options) {
|
573
|
+
var succeed = options.succeed === undefined ? true : options.succeed;
|
574
|
+
var matchArgs = options.match;
|
575
|
+
var returnArgs = options.returns;
|
576
|
+
var responseJson = {};
|
577
|
+
var expectedRequest = {};
|
578
|
+
var modelType = store.modelFor(modelName);
|
579
|
+
|
580
|
+
this.calculate = function() {
|
581
|
+
if (matchArgs) {
|
582
|
+
var tmpRecord = store.createRecord(modelName, matchArgs);
|
583
|
+
expectedRequest = tmpRecord.serialize(matchArgs);
|
584
|
+
tmpRecord.deleteRecord();
|
585
|
+
}
|
586
|
+
|
587
|
+
if (succeed) {
|
588
|
+
var definition = FactoryGuy.modelDefinitions[modelName];
|
589
|
+
if (responseJson[modelName]) {
|
590
|
+
// already calculated once, keep the same id
|
591
|
+
responseJson[modelName] = $.extend({id: responseJson[modelName].id}, matchArgs, returnArgs);
|
592
|
+
} else {
|
593
|
+
responseJson[modelName] = $.extend({id: definition.nextId()}, matchArgs, returnArgs);
|
594
|
+
}
|
595
|
+
// Remove belongsTo associations since they will already be set when you called
|
596
|
+
// createRecord, so they don't need to be returned.
|
597
|
+
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
598
|
+
if (relationship.kind == 'belongsTo') {
|
599
|
+
delete responseJson[modelName][relationship.key];
|
600
|
+
}
|
601
|
+
})
|
602
|
+
}
|
603
|
+
|
604
|
+
}
|
605
|
+
|
606
|
+
this.match = function(matches) {
|
607
|
+
matchArgs = matches;
|
608
|
+
this.calculate();
|
609
|
+
return this;
|
610
|
+
}
|
611
|
+
|
612
|
+
this.andReturn = function(returns) {
|
613
|
+
returnArgs = returns;
|
614
|
+
this.calculate();
|
615
|
+
return this;
|
616
|
+
}
|
617
|
+
|
618
|
+
this.andFail = function() {
|
619
|
+
succeed = false;
|
620
|
+
return this;
|
621
|
+
}
|
622
|
+
|
623
|
+
this.handler = function(settings) {
|
624
|
+
if (settings.url != url || settings.type != 'POST') { return false}
|
625
|
+
|
626
|
+
if (matchArgs) {
|
627
|
+
var requestData = JSON.parse(settings.data)[modelName];
|
628
|
+
for (attribute in expectedRequest) {
|
629
|
+
if (expectedRequest[attribute] &&
|
630
|
+
requestData[attribute] != expectedRequest[attribute]) {
|
631
|
+
return false
|
632
|
+
}
|
633
|
+
}
|
634
|
+
}
|
635
|
+
var responseStatus = (succeed ? 200: 500);
|
636
|
+
return {
|
637
|
+
responseText: responseJson,
|
638
|
+
status: responseStatus
|
639
|
+
};
|
640
|
+
}
|
641
|
+
|
642
|
+
this.calculate();
|
643
|
+
|
644
|
+
$.mockjax(this.handler);
|
645
|
+
};
|
646
|
+
|
572
647
|
(function () {
|
573
648
|
DS.Store.reopen({
|
574
649
|
/**
|
@@ -922,6 +997,34 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
922
997
|
var type = this.getStore().modelFor(typeName);
|
923
998
|
return this.getStore().adapterFor(type).buildURL(type.typeKey, id);
|
924
999
|
},
|
1000
|
+
/**
|
1001
|
+
Map many json objects to response json.
|
1002
|
+
|
1003
|
+
Allows custom serializing mappings and meta data to be added to requests.
|
1004
|
+
|
1005
|
+
@param {String} modelName model name
|
1006
|
+
@param {Object} json Json objects from records.map
|
1007
|
+
@return {Object} responseJson
|
1008
|
+
*/
|
1009
|
+
mapFindAll: function(modelName, json) {
|
1010
|
+
var responseJson = {};
|
1011
|
+
responseJson[modelName.pluralize()] = json;
|
1012
|
+
return responseJson;
|
1013
|
+
},
|
1014
|
+
/**
|
1015
|
+
Map single object to response json.
|
1016
|
+
|
1017
|
+
Allows custom serializing mappings and meta data to be added to requests.
|
1018
|
+
|
1019
|
+
@param {String} modelName model name
|
1020
|
+
@param {Object} json Json object from record.toJSON
|
1021
|
+
@return {Object} responseJson
|
1022
|
+
*/
|
1023
|
+
mapFind:function(modelName, json){
|
1024
|
+
var responseJson = {};
|
1025
|
+
responseJson[modelName.pluralize()] = json;
|
1026
|
+
return responseJson;
|
1027
|
+
},
|
925
1028
|
/**
|
926
1029
|
Handling ajax GET for finding all records for a type of model.
|
927
1030
|
You can mock failed find by passing in success argument as false.
|
@@ -947,9 +1050,8 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
947
1050
|
var records = FactoryGuy.makeList.apply(FactoryGuy, arguments);
|
948
1051
|
var name = arguments[0];
|
949
1052
|
var modelName = FactoryGuy.lookupModelForFixtureName(name);
|
950
|
-
var responseJson = {};
|
951
1053
|
var json = records.map(function(record) {return record.toJSON({includeId: true})});
|
952
|
-
responseJson
|
1054
|
+
var responseJson = this.mapFindAll(modelName, json);
|
953
1055
|
var url = this.buildURL(modelName);
|
954
1056
|
this.stubEndpointForHttpRequest(url, responseJson);
|
955
1057
|
},
|
@@ -991,9 +1093,8 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
991
1093
|
modelName = FactoryGuy.lookupModelForFixtureName(name);
|
992
1094
|
}
|
993
1095
|
|
994
|
-
var responseJson = {};
|
995
1096
|
var json = record.toJSON({includeId: true});
|
996
|
-
responseJson
|
1097
|
+
var responseJson = this.mapFind(modelName, json);
|
997
1098
|
var url = this.buildURL(modelName, record.id);
|
998
1099
|
this.stubEndpointForHttpRequest(url, responseJson);
|
999
1100
|
},
|
@@ -1039,8 +1140,7 @@ var FactoryGuyTestMixin = Em.Mixin.create({
|
|
1039
1140
|
records = []
|
1040
1141
|
}
|
1041
1142
|
var json = records.map(function(record) {return record.toJSON({includeId: true})})
|
1042
|
-
var responseJson =
|
1043
|
-
responseJson[modelName.pluralize()] = json;
|
1143
|
+
var responseJson = this.mapFindAll(modelName, json);
|
1044
1144
|
var url = this.buildURL(modelName);
|
1045
1145
|
this.stubEndpointForHttpRequest(url, responseJson, {urlParams: searchParams});
|
1046
1146
|
},
|
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.7
|
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-23 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easily create Fixtures for Ember Data
|
15
15
|
email:
|