ember-data-factory-guy 0.9.10 → 0.9.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bower.json +1 -1
- data/dist/amd/factory-guy.js +63 -52
- data/dist/ember-data-factory-guy.js +63 -52
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/package.json +1 -1
- data/src/factory_guy.js +4 -7
- data/src/mock_create_request.js +42 -29
- data/src/mock_update_request.js +14 -14
- data/src/model_definition.js +3 -2
- data/tests/factory_guy_test.js +4 -4
- data/tests/factory_guy_test_mixin_test.js +31 -1
- data/tests/index.html +4 -4
- data/vendor/assets/javascripts/ember_data_factory_guy.js +69 -56
- 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: e855e2ca1b850446f44ca658109ecb36dadee68d
|
4
|
+
data.tar.gz: 45a768f6054ac593b17264a108dbf4bf7d576fa3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27d190e0cb7914401149537061468ad75398c39618620d61360ae1819db71bf881c42aa0472bbff30900a2b5efee76396459720f8685717bf3e54fff99b5cade
|
7
|
+
data.tar.gz: 90e4cb0bc2b6613363aa8f0f0dbb13436367cf800f6cb2876429b94b9e5599c1302c51578997fa4ebf7d77498cf39725fe4934d35575cd77d809e90018785b8e
|
data/bower.json
CHANGED
data/dist/amd/factory-guy.js
CHANGED
@@ -98,8 +98,9 @@ var ModelDefinition = function (model, config) {
|
|
98
98
|
// If it's an object and it's a model association attribute, build the json
|
99
99
|
// for the association and replace the attribute with that json
|
100
100
|
if (FactoryGuy.getStore()) {
|
101
|
-
|
102
|
-
|
101
|
+
var relationship = FactoryGuy.getAttributeRelationship(this.model, attribute);
|
102
|
+
if (relationship) {
|
103
|
+
fixture[attribute] = FactoryGuy.build(relationship.typeKey, fixture[attribute]);
|
103
104
|
}
|
104
105
|
} else {
|
105
106
|
// For legacy reasons, if the store is not set in FactoryGuy, keep
|
@@ -222,11 +223,7 @@ var FactoryGuy = {
|
|
222
223
|
@param {Object} config your model definition
|
223
224
|
*/
|
224
225
|
define: function (model, config) {
|
225
|
-
|
226
|
-
this.modelDefinitions[model].merge(config);
|
227
|
-
} else {
|
228
|
-
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
229
|
-
}
|
226
|
+
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
230
227
|
},
|
231
228
|
/**
|
232
229
|
Setting the store so FactoryGuy can do some model introspection.
|
@@ -245,14 +242,15 @@ var FactoryGuy = {
|
|
245
242
|
@param {String} attribute attribute you want to check
|
246
243
|
@returns {Boolean} true if the attribute is a relationship, false if not
|
247
244
|
*/
|
248
|
-
|
245
|
+
getAttributeRelationship: function(typeName, attribute) {
|
249
246
|
if (!this.store) {
|
250
247
|
Ember.debug("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures")
|
251
248
|
// The legacy value was true.
|
252
249
|
return true;
|
253
250
|
}
|
254
251
|
var model = this.store.modelFor(typeName);
|
255
|
-
|
252
|
+
var relationship = model.typeForRelationship(attribute);
|
253
|
+
return !!relationship ? relationship : null;
|
256
254
|
},
|
257
255
|
/**
|
258
256
|
Used in model definitions to declare use of a sequence. For example:
|
@@ -575,6 +573,7 @@ var FactoryGuy = {
|
|
575
573
|
};
|
576
574
|
|
577
575
|
var MockCreateRequest = function(url, store, modelName, options) {
|
576
|
+
var status = options.status;
|
578
577
|
var succeed = options.succeed === undefined ? true : options.succeed;
|
579
578
|
var matchArgs = options.match;
|
580
579
|
var returnArgs = options.returns;
|
@@ -584,6 +583,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
584
583
|
|
585
584
|
this.calculate = function() {
|
586
585
|
if (matchArgs) {
|
586
|
+
// although not ideal to create and delete a record, using this technique to
|
587
|
+
// get properly serialized payload.
|
588
|
+
// TODO: Need to figure out how to use serializer without a record
|
587
589
|
var tmpRecord = store.createRecord(modelName, matchArgs);
|
588
590
|
expectedRequest = tmpRecord.serialize(matchArgs);
|
589
591
|
tmpRecord.deleteRecord();
|
@@ -591,12 +593,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
591
593
|
|
592
594
|
if (succeed) {
|
593
595
|
var definition = FactoryGuy.modelDefinitions[modelName];
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
} else {
|
598
|
-
responseJson[modelName] = $.extend({id: definition.nextId()}, matchArgs, returnArgs);
|
599
|
-
}
|
596
|
+
// If already calculated once, keep the same id
|
597
|
+
var id = responseJson[modelName] ? responseJson[modelName].id : definition.nextId();
|
598
|
+
responseJson[modelName] = $.extend({id: id}, matchArgs, returnArgs);
|
600
599
|
// Remove belongsTo associations since they will already be set when you called
|
601
600
|
// createRecord, so they don't need to be returned.
|
602
601
|
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
@@ -604,84 +603,96 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
604
603
|
delete responseJson[modelName][relationship.key];
|
605
604
|
}
|
606
605
|
})
|
606
|
+
} else {
|
607
|
+
this.andFail(options);
|
607
608
|
}
|
608
|
-
|
609
|
-
}
|
609
|
+
};
|
610
610
|
|
611
611
|
this.match = function(matches) {
|
612
612
|
matchArgs = matches;
|
613
613
|
this.calculate();
|
614
614
|
return this;
|
615
|
-
}
|
615
|
+
};
|
616
616
|
|
617
617
|
this.andReturn = function(returns) {
|
618
618
|
returnArgs = returns;
|
619
619
|
this.calculate();
|
620
620
|
return this;
|
621
|
-
}
|
621
|
+
};
|
622
622
|
|
623
|
-
this.andFail = function() {
|
624
|
-
|
625
|
-
|
626
|
-
|
623
|
+
this.andFail = function(options) {
|
624
|
+
options = options || {}
|
625
|
+
succeed = false;
|
626
|
+
status = options.status || 500;
|
627
|
+
if (options.response) {
|
628
|
+
responseJson = options.response;
|
629
|
+
}
|
630
|
+
return this;
|
631
|
+
};
|
627
632
|
|
628
633
|
this.handler = function(settings) {
|
629
|
-
if (
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
634
|
+
if (succeed) {
|
635
|
+
if (matchArgs) {
|
636
|
+
var requestData = JSON.parse(settings.data)[modelName];
|
637
|
+
for (var attribute in expectedRequest) {
|
638
|
+
if (expectedRequest[attribute] &&
|
639
|
+
requestData[attribute] != expectedRequest[attribute]) {
|
640
|
+
return false;
|
641
|
+
}
|
637
642
|
}
|
638
643
|
}
|
644
|
+
this.status = 200;
|
645
|
+
} else {
|
646
|
+
this.status = status;
|
639
647
|
}
|
640
|
-
|
641
|
-
|
642
|
-
responseText: responseJson,
|
643
|
-
status: responseStatus
|
644
|
-
};
|
645
|
-
}
|
648
|
+
this.responseText = responseJson;
|
649
|
+
};
|
646
650
|
|
647
651
|
this.calculate();
|
648
652
|
|
649
|
-
|
653
|
+
var requestConfig = {
|
654
|
+
url: url,
|
655
|
+
dataType: 'json',
|
656
|
+
type: 'POST',
|
657
|
+
response: this.handler
|
658
|
+
};
|
659
|
+
|
660
|
+
$.mockjax(requestConfig);
|
650
661
|
};
|
651
662
|
|
652
663
|
var MockUpdateRequest = function(url, model, mapFind, options) {
|
653
664
|
var status = options.status || 200;
|
654
665
|
var succeed = true;
|
655
|
-
|
666
|
+
var response = null;
|
656
667
|
|
657
|
-
|
658
|
-
|
659
|
-
|
668
|
+
if ('succeed' in options) {
|
669
|
+
succeed = options.succeed;
|
670
|
+
}
|
660
671
|
|
661
|
-
|
662
|
-
|
663
|
-
|
672
|
+
if ('response' in options) {
|
673
|
+
response = options.response;
|
674
|
+
}
|
664
675
|
|
665
676
|
this.andSucceed = function(options) {
|
666
|
-
|
677
|
+
succeed = true;
|
667
678
|
return this;
|
668
679
|
};
|
669
680
|
|
670
681
|
this.andFail = function(options) {
|
671
682
|
succeed = false;
|
672
683
|
status = options.status || 500;
|
673
|
-
|
674
|
-
|
675
|
-
|
684
|
+
if ('response' in options) {
|
685
|
+
response = options.response;
|
686
|
+
}
|
676
687
|
return this;
|
677
688
|
};
|
678
689
|
|
679
690
|
this.handler = function(settings) {
|
680
691
|
if (!succeed) {
|
681
692
|
this.status = status;
|
682
|
-
|
683
|
-
|
684
|
-
|
693
|
+
if (response !== null) {
|
694
|
+
this.responseText = response;
|
695
|
+
}
|
685
696
|
} else {
|
686
697
|
var json = model.toJSON({includeId: true});
|
687
698
|
this.responseText = mapFind(model.constructor.typeKey, json);
|
@@ -93,8 +93,9 @@ var ModelDefinition = function (model, config) {
|
|
93
93
|
// If it's an object and it's a model association attribute, build the json
|
94
94
|
// for the association and replace the attribute with that json
|
95
95
|
if (FactoryGuy.getStore()) {
|
96
|
-
|
97
|
-
|
96
|
+
var relationship = FactoryGuy.getAttributeRelationship(this.model, attribute);
|
97
|
+
if (relationship) {
|
98
|
+
fixture[attribute] = FactoryGuy.build(relationship.typeKey, fixture[attribute]);
|
98
99
|
}
|
99
100
|
} else {
|
100
101
|
// For legacy reasons, if the store is not set in FactoryGuy, keep
|
@@ -217,11 +218,7 @@ var FactoryGuy = {
|
|
217
218
|
@param {Object} config your model definition
|
218
219
|
*/
|
219
220
|
define: function (model, config) {
|
220
|
-
|
221
|
-
this.modelDefinitions[model].merge(config);
|
222
|
-
} else {
|
223
|
-
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
224
|
-
}
|
221
|
+
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
225
222
|
},
|
226
223
|
/**
|
227
224
|
Setting the store so FactoryGuy can do some model introspection.
|
@@ -240,14 +237,15 @@ var FactoryGuy = {
|
|
240
237
|
@param {String} attribute attribute you want to check
|
241
238
|
@returns {Boolean} true if the attribute is a relationship, false if not
|
242
239
|
*/
|
243
|
-
|
240
|
+
getAttributeRelationship: function(typeName, attribute) {
|
244
241
|
if (!this.store) {
|
245
242
|
Ember.debug("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures")
|
246
243
|
// The legacy value was true.
|
247
244
|
return true;
|
248
245
|
}
|
249
246
|
var model = this.store.modelFor(typeName);
|
250
|
-
|
247
|
+
var relationship = model.typeForRelationship(attribute);
|
248
|
+
return !!relationship ? relationship : null;
|
251
249
|
},
|
252
250
|
/**
|
253
251
|
Used in model definitions to declare use of a sequence. For example:
|
@@ -570,6 +568,7 @@ var FactoryGuy = {
|
|
570
568
|
};
|
571
569
|
|
572
570
|
var MockCreateRequest = function(url, store, modelName, options) {
|
571
|
+
var status = options.status;
|
573
572
|
var succeed = options.succeed === undefined ? true : options.succeed;
|
574
573
|
var matchArgs = options.match;
|
575
574
|
var returnArgs = options.returns;
|
@@ -579,6 +578,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
579
578
|
|
580
579
|
this.calculate = function() {
|
581
580
|
if (matchArgs) {
|
581
|
+
// although not ideal to create and delete a record, using this technique to
|
582
|
+
// get properly serialized payload.
|
583
|
+
// TODO: Need to figure out how to use serializer without a record
|
582
584
|
var tmpRecord = store.createRecord(modelName, matchArgs);
|
583
585
|
expectedRequest = tmpRecord.serialize(matchArgs);
|
584
586
|
tmpRecord.deleteRecord();
|
@@ -586,12 +588,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
586
588
|
|
587
589
|
if (succeed) {
|
588
590
|
var definition = FactoryGuy.modelDefinitions[modelName];
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
} else {
|
593
|
-
responseJson[modelName] = $.extend({id: definition.nextId()}, matchArgs, returnArgs);
|
594
|
-
}
|
591
|
+
// If already calculated once, keep the same id
|
592
|
+
var id = responseJson[modelName] ? responseJson[modelName].id : definition.nextId();
|
593
|
+
responseJson[modelName] = $.extend({id: id}, matchArgs, returnArgs);
|
595
594
|
// Remove belongsTo associations since they will already be set when you called
|
596
595
|
// createRecord, so they don't need to be returned.
|
597
596
|
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
@@ -599,84 +598,96 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
599
598
|
delete responseJson[modelName][relationship.key];
|
600
599
|
}
|
601
600
|
})
|
601
|
+
} else {
|
602
|
+
this.andFail(options);
|
602
603
|
}
|
603
|
-
|
604
|
-
}
|
604
|
+
};
|
605
605
|
|
606
606
|
this.match = function(matches) {
|
607
607
|
matchArgs = matches;
|
608
608
|
this.calculate();
|
609
609
|
return this;
|
610
|
-
}
|
610
|
+
};
|
611
611
|
|
612
612
|
this.andReturn = function(returns) {
|
613
613
|
returnArgs = returns;
|
614
614
|
this.calculate();
|
615
615
|
return this;
|
616
|
-
}
|
616
|
+
};
|
617
617
|
|
618
|
-
this.andFail = function() {
|
619
|
-
|
620
|
-
|
621
|
-
|
618
|
+
this.andFail = function(options) {
|
619
|
+
options = options || {}
|
620
|
+
succeed = false;
|
621
|
+
status = options.status || 500;
|
622
|
+
if (options.response) {
|
623
|
+
responseJson = options.response;
|
624
|
+
}
|
625
|
+
return this;
|
626
|
+
};
|
622
627
|
|
623
628
|
this.handler = function(settings) {
|
624
|
-
if (
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
629
|
+
if (succeed) {
|
630
|
+
if (matchArgs) {
|
631
|
+
var requestData = JSON.parse(settings.data)[modelName];
|
632
|
+
for (var attribute in expectedRequest) {
|
633
|
+
if (expectedRequest[attribute] &&
|
634
|
+
requestData[attribute] != expectedRequest[attribute]) {
|
635
|
+
return false;
|
636
|
+
}
|
632
637
|
}
|
633
638
|
}
|
639
|
+
this.status = 200;
|
640
|
+
} else {
|
641
|
+
this.status = status;
|
634
642
|
}
|
635
|
-
|
636
|
-
|
637
|
-
responseText: responseJson,
|
638
|
-
status: responseStatus
|
639
|
-
};
|
640
|
-
}
|
643
|
+
this.responseText = responseJson;
|
644
|
+
};
|
641
645
|
|
642
646
|
this.calculate();
|
643
647
|
|
644
|
-
|
648
|
+
var requestConfig = {
|
649
|
+
url: url,
|
650
|
+
dataType: 'json',
|
651
|
+
type: 'POST',
|
652
|
+
response: this.handler
|
653
|
+
};
|
654
|
+
|
655
|
+
$.mockjax(requestConfig);
|
645
656
|
};
|
646
657
|
|
647
658
|
var MockUpdateRequest = function(url, model, mapFind, options) {
|
648
659
|
var status = options.status || 200;
|
649
660
|
var succeed = true;
|
650
|
-
|
661
|
+
var response = null;
|
651
662
|
|
652
|
-
|
653
|
-
|
654
|
-
|
663
|
+
if ('succeed' in options) {
|
664
|
+
succeed = options.succeed;
|
665
|
+
}
|
655
666
|
|
656
|
-
|
657
|
-
|
658
|
-
|
667
|
+
if ('response' in options) {
|
668
|
+
response = options.response;
|
669
|
+
}
|
659
670
|
|
660
671
|
this.andSucceed = function(options) {
|
661
|
-
|
672
|
+
succeed = true;
|
662
673
|
return this;
|
663
674
|
};
|
664
675
|
|
665
676
|
this.andFail = function(options) {
|
666
677
|
succeed = false;
|
667
678
|
status = options.status || 500;
|
668
|
-
|
669
|
-
|
670
|
-
|
679
|
+
if ('response' in options) {
|
680
|
+
response = options.response;
|
681
|
+
}
|
671
682
|
return this;
|
672
683
|
};
|
673
684
|
|
674
685
|
this.handler = function(settings) {
|
675
686
|
if (!succeed) {
|
676
687
|
this.status = status;
|
677
|
-
|
678
|
-
|
679
|
-
|
688
|
+
if (response !== null) {
|
689
|
+
this.responseText = response;
|
690
|
+
}
|
680
691
|
} else {
|
681
692
|
var json = model.toJSON({includeId: true});
|
682
693
|
this.responseText = mapFind(model.constructor.typeKey, json);
|
@@ -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 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(var 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)};var MockUpdateRequest=function(url,model,mapFind,options){var status=options.status||200;var succeed=true;var response=null;if("succeed"in options){succeed=options.succeed}if("response"in options){response=options.response}this.andSucceed=function(options){succeed=true;return this};this.andFail=function(options){succeed=false;status=options.status||500;if("response"in options){response=options.response}return this};this.handler=function(settings){if(!succeed){this.status=status;if(response!==null){this.responseText=response}}else{var json=model.toJSON({includeId:true});this.responseText=mapFind(model.constructor.typeKey,json);this.status=200}};var requestConfig={url:url,dataType:"json",type:"PUT",response:this.handler};$.mockjax(requestConfig)};(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 options={};if(args.length>1&&typeof args[args.length-1]==="object"){options=args.pop()}var model,type,id;var store=this.getStore();if(args[0]instanceof DS.Model){model=args[0];id=model.id;type=model.constructor.typeKey}else if(typeof args[0]=="string"&&typeof parseInt(args[1])=="number"){type=args[0];id=args[1];model=store.getById(type,id)}Ember.assert("To handleUpdate pass in a model instance or a type and an id",type&&id);var url=this.buildURL(type,id);return new MockUpdateRequest(url,model,this.mapFind,options)},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()){var relationship=FactoryGuy.getAttributeRelationship(this.model,attribute);if(relationship){fixture[attribute]=FactoryGuy.build(relationship.typeKey,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){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},getAttributeRelationship: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);var relationship=model.typeForRelationship(attribute);return!!relationship?relationship:null},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 status=options.status;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];var id=responseJson[modelName]?responseJson[modelName].id:definition.nextId();responseJson[modelName]=$.extend({id:id},matchArgs,returnArgs);Ember.get(modelType,"relationshipsByName").forEach(function(relationship){if(relationship.kind=="belongsTo"){delete responseJson[modelName][relationship.key]}})}else{this.andFail(options)}};this.match=function(matches){matchArgs=matches;this.calculate();return this};this.andReturn=function(returns){returnArgs=returns;this.calculate();return this};this.andFail=function(options){options=options||{};succeed=false;status=options.status||500;if(options.response){responseJson=options.response}return this};this.handler=function(settings){if(succeed){if(matchArgs){var requestData=JSON.parse(settings.data)[modelName];for(var attribute in expectedRequest){if(expectedRequest[attribute]&&requestData[attribute]!=expectedRequest[attribute]){return false}}}this.status=200}else{this.status=status}this.responseText=responseJson};this.calculate();var requestConfig={url:url,dataType:"json",type:"POST",response:this.handler};$.mockjax(requestConfig)};var MockUpdateRequest=function(url,model,mapFind,options){var status=options.status||200;var succeed=true;var response=null;if("succeed"in options){succeed=options.succeed}if("response"in options){response=options.response}this.andSucceed=function(options){succeed=true;return this};this.andFail=function(options){succeed=false;status=options.status||500;if("response"in options){response=options.response}return this};this.handler=function(settings){if(!succeed){this.status=status;if(response!==null){this.responseText=response}}else{var json=model.toJSON({includeId:true});this.responseText=mapFind(model.constructor.typeKey,json);this.status=200}};var requestConfig={url:url,dataType:"json",type:"PUT",response:this.handler};$.mockjax(requestConfig)};(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 options={};if(args.length>1&&typeof args[args.length-1]==="object"){options=args.pop()}var model,type,id;var store=this.getStore();if(args[0]instanceof DS.Model){model=args[0];id=model.id;type=model.constructor.typeKey}else if(typeof args[0]=="string"&&typeof parseInt(args[1])=="number"){type=args[0];id=args[1];model=store.getById(type,id)}Ember.assert("To handleUpdate pass in a model instance or a type and an id",type&&id);var url=this.buildURL(type,id);return new MockUpdateRequest(url,model,this.mapFind,options)},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
data/src/factory_guy.js
CHANGED
@@ -39,11 +39,7 @@ var FactoryGuy = {
|
|
39
39
|
@param {Object} config your model definition
|
40
40
|
*/
|
41
41
|
define: function (model, config) {
|
42
|
-
|
43
|
-
this.modelDefinitions[model].merge(config);
|
44
|
-
} else {
|
45
|
-
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
46
|
-
}
|
42
|
+
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
47
43
|
},
|
48
44
|
/**
|
49
45
|
Setting the store so FactoryGuy can do some model introspection.
|
@@ -62,14 +58,15 @@ var FactoryGuy = {
|
|
62
58
|
@param {String} attribute attribute you want to check
|
63
59
|
@returns {Boolean} true if the attribute is a relationship, false if not
|
64
60
|
*/
|
65
|
-
|
61
|
+
getAttributeRelationship: function(typeName, attribute) {
|
66
62
|
if (!this.store) {
|
67
63
|
Ember.debug("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures")
|
68
64
|
// The legacy value was true.
|
69
65
|
return true;
|
70
66
|
}
|
71
67
|
var model = this.store.modelFor(typeName);
|
72
|
-
|
68
|
+
var relationship = model.typeForRelationship(attribute);
|
69
|
+
return !!relationship ? relationship : null;
|
73
70
|
},
|
74
71
|
/**
|
75
72
|
Used in model definitions to declare use of a sequence. For example:
|
data/src/mock_create_request.js
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
var MockCreateRequest = function(url, store, modelName, options) {
|
2
|
+
var status = options.status;
|
2
3
|
var succeed = options.succeed === undefined ? true : options.succeed;
|
3
4
|
var matchArgs = options.match;
|
4
5
|
var returnArgs = options.returns;
|
@@ -8,6 +9,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
8
9
|
|
9
10
|
this.calculate = function() {
|
10
11
|
if (matchArgs) {
|
12
|
+
// although not ideal to create and delete a record, using this technique to
|
13
|
+
// get properly serialized payload.
|
14
|
+
// TODO: Need to figure out how to use serializer without a record
|
11
15
|
var tmpRecord = store.createRecord(modelName, matchArgs);
|
12
16
|
expectedRequest = tmpRecord.serialize(matchArgs);
|
13
17
|
tmpRecord.deleteRecord();
|
@@ -15,12 +19,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
15
19
|
|
16
20
|
if (succeed) {
|
17
21
|
var definition = FactoryGuy.modelDefinitions[modelName];
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
} else {
|
22
|
-
responseJson[modelName] = $.extend({id: definition.nextId()}, matchArgs, returnArgs);
|
23
|
-
}
|
22
|
+
// If already calculated once, keep the same id
|
23
|
+
var id = responseJson[modelName] ? responseJson[modelName].id : definition.nextId();
|
24
|
+
responseJson[modelName] = $.extend({id: id}, matchArgs, returnArgs);
|
24
25
|
// Remove belongsTo associations since they will already be set when you called
|
25
26
|
// createRecord, so they don't need to be returned.
|
26
27
|
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
@@ -28,47 +29,59 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
28
29
|
delete responseJson[modelName][relationship.key];
|
29
30
|
}
|
30
31
|
})
|
32
|
+
} else {
|
33
|
+
this.andFail(options);
|
31
34
|
}
|
32
|
-
|
33
|
-
}
|
35
|
+
};
|
34
36
|
|
35
37
|
this.match = function(matches) {
|
36
38
|
matchArgs = matches;
|
37
39
|
this.calculate();
|
38
40
|
return this;
|
39
|
-
}
|
41
|
+
};
|
40
42
|
|
41
43
|
this.andReturn = function(returns) {
|
42
44
|
returnArgs = returns;
|
43
45
|
this.calculate();
|
44
46
|
return this;
|
45
|
-
}
|
47
|
+
};
|
46
48
|
|
47
|
-
this.andFail = function() {
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
this.andFail = function(options) {
|
50
|
+
options = options || {}
|
51
|
+
succeed = false;
|
52
|
+
status = options.status || 500;
|
53
|
+
if (options.response) {
|
54
|
+
responseJson = options.response;
|
55
|
+
}
|
56
|
+
return this;
|
57
|
+
};
|
51
58
|
|
52
59
|
this.handler = function(settings) {
|
53
|
-
if (
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
60
|
+
if (succeed) {
|
61
|
+
if (matchArgs) {
|
62
|
+
var requestData = JSON.parse(settings.data)[modelName];
|
63
|
+
for (var attribute in expectedRequest) {
|
64
|
+
if (expectedRequest[attribute] &&
|
65
|
+
requestData[attribute] != expectedRequest[attribute]) {
|
66
|
+
return false;
|
67
|
+
}
|
61
68
|
}
|
62
69
|
}
|
70
|
+
this.status = 200;
|
71
|
+
} else {
|
72
|
+
this.status = status;
|
63
73
|
}
|
64
|
-
|
65
|
-
|
66
|
-
responseText: responseJson,
|
67
|
-
status: responseStatus
|
68
|
-
};
|
69
|
-
}
|
74
|
+
this.responseText = responseJson;
|
75
|
+
};
|
70
76
|
|
71
77
|
this.calculate();
|
72
78
|
|
73
|
-
|
79
|
+
var requestConfig = {
|
80
|
+
url: url,
|
81
|
+
dataType: 'json',
|
82
|
+
type: 'POST',
|
83
|
+
response: this.handler
|
84
|
+
};
|
85
|
+
|
86
|
+
$.mockjax(requestConfig);
|
74
87
|
};
|
data/src/mock_update_request.js
CHANGED
@@ -1,36 +1,36 @@
|
|
1
1
|
var MockUpdateRequest = function(url, model, mapFind, options) {
|
2
2
|
var status = options.status || 200;
|
3
3
|
var succeed = true;
|
4
|
-
|
4
|
+
var response = null;
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
if ('succeed' in options) {
|
7
|
+
succeed = options.succeed;
|
8
|
+
}
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
if ('response' in options) {
|
11
|
+
response = options.response;
|
12
|
+
}
|
13
13
|
|
14
14
|
this.andSucceed = function(options) {
|
15
|
-
|
15
|
+
succeed = true;
|
16
16
|
return this;
|
17
17
|
};
|
18
18
|
|
19
19
|
this.andFail = function(options) {
|
20
20
|
succeed = false;
|
21
21
|
status = options.status || 500;
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
if ('response' in options) {
|
23
|
+
response = options.response;
|
24
|
+
}
|
25
25
|
return this;
|
26
26
|
};
|
27
27
|
|
28
28
|
this.handler = function(settings) {
|
29
29
|
if (!succeed) {
|
30
30
|
this.status = status;
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
if (response !== null) {
|
32
|
+
this.responseText = response;
|
33
|
+
}
|
34
34
|
} else {
|
35
35
|
var json = model.toJSON({includeId: true});
|
36
36
|
this.responseText = mapFind(model.constructor.typeKey, json);
|
data/src/model_definition.js
CHANGED
@@ -72,8 +72,9 @@ var ModelDefinition = function (model, config) {
|
|
72
72
|
// If it's an object and it's a model association attribute, build the json
|
73
73
|
// for the association and replace the attribute with that json
|
74
74
|
if (FactoryGuy.getStore()) {
|
75
|
-
|
76
|
-
|
75
|
+
var relationship = FactoryGuy.getAttributeRelationship(this.model, attribute);
|
76
|
+
if (relationship) {
|
77
|
+
fixture[attribute] = FactoryGuy.build(relationship.typeKey, fixture[attribute]);
|
77
78
|
}
|
78
79
|
} else {
|
79
80
|
// For legacy reasons, if the store is not set in FactoryGuy, keep
|
data/tests/factory_guy_test.js
CHANGED
@@ -195,12 +195,12 @@ test("#buildList creates list of fixtures", function() {
|
|
195
195
|
});
|
196
196
|
|
197
197
|
|
198
|
-
test("#
|
198
|
+
test("#getAttributeRelationship", function() {
|
199
199
|
FactoryGuy.setStore(store);
|
200
200
|
var typeName = 'user'
|
201
|
-
equal(FactoryGuy.
|
202
|
-
equal(FactoryGuy.
|
203
|
-
equal(FactoryGuy.
|
201
|
+
equal(FactoryGuy.getAttributeRelationship(typeName,'company').typeKey,'company');
|
202
|
+
equal(FactoryGuy.getAttributeRelationship(typeName,'hats').typeKey,'hat');
|
203
|
+
equal(FactoryGuy.getAttributeRelationship(typeName,'name'),null);
|
204
204
|
});
|
205
205
|
|
206
206
|
|
@@ -403,6 +403,22 @@ asyncTest("#handleCreate failure", function() {
|
|
403
403
|
});
|
404
404
|
});
|
405
405
|
|
406
|
+
asyncTest("#handleCreate failure with status code 422 and errors in response", function() {
|
407
|
+
Em.run(function() {
|
408
|
+
testHelper.handleCreate('profile', {succeed: false, status: 422, response: {errors: {description: ['bad']}} } )
|
409
|
+
|
410
|
+
store.createRecord('profile').save()
|
411
|
+
.then(
|
412
|
+
function() {},
|
413
|
+
function(reason) {
|
414
|
+
equal(reason.errors.description, ['bad']+'');
|
415
|
+
ok(true)
|
416
|
+
start();
|
417
|
+
}
|
418
|
+
)
|
419
|
+
});
|
420
|
+
});
|
421
|
+
|
406
422
|
|
407
423
|
asyncTest("#handleCreate match but still fail", function() {
|
408
424
|
Em.run(function() {
|
@@ -543,7 +559,7 @@ asyncTest("#handleCreate failure with andFail method", function() {
|
|
543
559
|
});
|
544
560
|
|
545
561
|
|
546
|
-
asyncTest("#handleCreate match but still fail with
|
562
|
+
asyncTest("#handleCreate match but still fail with andFail method", function() {
|
547
563
|
Em.run(function() {
|
548
564
|
var description = "special description"
|
549
565
|
|
@@ -560,7 +576,21 @@ asyncTest("#handleCreate match but still fail with chaining methods", function()
|
|
560
576
|
});
|
561
577
|
});
|
562
578
|
|
579
|
+
asyncTest("#handleCreate failure with status code 422 and errors in response with andFail method", function() {
|
580
|
+
Em.run(function() {
|
581
|
+
testHelper.handleCreate('profile').andFail({status: 422, response: {errors: {description: ['bad']}}});
|
563
582
|
|
583
|
+
store.createRecord('profile').save()
|
584
|
+
.then(
|
585
|
+
function() {},
|
586
|
+
function(reason) {
|
587
|
+
equal(reason.errors.description, ['bad']+'');
|
588
|
+
ok(true)
|
589
|
+
start();
|
590
|
+
}
|
591
|
+
)
|
592
|
+
});
|
593
|
+
});
|
564
594
|
|
565
595
|
|
566
596
|
|
data/tests/index.html
CHANGED
@@ -27,11 +27,11 @@
|
|
27
27
|
</head>
|
28
28
|
<body>
|
29
29
|
<div id="qunit"></div> <!-- QUnit fills this with results, etc -->
|
30
|
-
|
31
|
-
|
30
|
+
<!--<script src='active_model_adapter_factory_test.js'></script>-->
|
31
|
+
<!--<script src='rest_adapter_factory_test.js'></script>-->
|
32
32
|
<!--<script src='fixture_adapter_factory_test.js'></script>-->
|
33
|
-
|
34
|
-
|
33
|
+
<!--<script src='store_test.js'></script>-->
|
34
|
+
<!--<script src='factory_guy_test.js'></script>-->
|
35
35
|
<script src='factory_guy_test_mixin_test.js'></script>
|
36
36
|
<div id='qunit-fixture'>
|
37
37
|
|
@@ -93,8 +93,9 @@ var ModelDefinition = function (model, config) {
|
|
93
93
|
// If it's an object and it's a model association attribute, build the json
|
94
94
|
// for the association and replace the attribute with that json
|
95
95
|
if (FactoryGuy.getStore()) {
|
96
|
-
|
97
|
-
|
96
|
+
var relationship = FactoryGuy.getAttributeRelationship(this.model, attribute);
|
97
|
+
if (relationship) {
|
98
|
+
fixture[attribute] = FactoryGuy.build(relationship.typeKey, fixture[attribute]);
|
98
99
|
}
|
99
100
|
} else {
|
100
101
|
// For legacy reasons, if the store is not set in FactoryGuy, keep
|
@@ -217,11 +218,7 @@ var FactoryGuy = {
|
|
217
218
|
@param {Object} config your model definition
|
218
219
|
*/
|
219
220
|
define: function (model, config) {
|
220
|
-
|
221
|
-
this.modelDefinitions[model].merge(config);
|
222
|
-
} else {
|
223
|
-
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
224
|
-
}
|
221
|
+
this.modelDefinitions[model] = new ModelDefinition(model, config);
|
225
222
|
},
|
226
223
|
/**
|
227
224
|
Setting the store so FactoryGuy can do some model introspection.
|
@@ -240,14 +237,15 @@ var FactoryGuy = {
|
|
240
237
|
@param {String} attribute attribute you want to check
|
241
238
|
@returns {Boolean} true if the attribute is a relationship, false if not
|
242
239
|
*/
|
243
|
-
|
240
|
+
getAttributeRelationship: function(typeName, attribute) {
|
244
241
|
if (!this.store) {
|
245
242
|
Ember.debug("FactoryGuy does not have the application's store. Use FactoryGuy.setStore(store) before making any fixtures")
|
246
243
|
// The legacy value was true.
|
247
244
|
return true;
|
248
245
|
}
|
249
246
|
var model = this.store.modelFor(typeName);
|
250
|
-
|
247
|
+
var relationship = model.typeForRelationship(attribute);
|
248
|
+
return !!relationship ? relationship : null;
|
251
249
|
},
|
252
250
|
/**
|
253
251
|
Used in model definitions to declare use of a sequence. For example:
|
@@ -570,6 +568,7 @@ var FactoryGuy = {
|
|
570
568
|
};
|
571
569
|
|
572
570
|
var MockCreateRequest = function(url, store, modelName, options) {
|
571
|
+
var status = options.status;
|
573
572
|
var succeed = options.succeed === undefined ? true : options.succeed;
|
574
573
|
var matchArgs = options.match;
|
575
574
|
var returnArgs = options.returns;
|
@@ -579,6 +578,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
579
578
|
|
580
579
|
this.calculate = function() {
|
581
580
|
if (matchArgs) {
|
581
|
+
// although not ideal to create and delete a record, using this technique to
|
582
|
+
// get properly serialized payload.
|
583
|
+
// TODO: Need to figure out how to use serializer without a record
|
582
584
|
var tmpRecord = store.createRecord(modelName, matchArgs);
|
583
585
|
expectedRequest = tmpRecord.serialize(matchArgs);
|
584
586
|
tmpRecord.deleteRecord();
|
@@ -586,12 +588,9 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
586
588
|
|
587
589
|
if (succeed) {
|
588
590
|
var definition = FactoryGuy.modelDefinitions[modelName];
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
} else {
|
593
|
-
responseJson[modelName] = $.extend({id: definition.nextId()}, matchArgs, returnArgs);
|
594
|
-
}
|
591
|
+
// If already calculated once, keep the same id
|
592
|
+
var id = responseJson[modelName] ? responseJson[modelName].id : definition.nextId();
|
593
|
+
responseJson[modelName] = $.extend({id: id}, matchArgs, returnArgs);
|
595
594
|
// Remove belongsTo associations since they will already be set when you called
|
596
595
|
// createRecord, so they don't need to be returned.
|
597
596
|
Ember.get(modelType, 'relationshipsByName').forEach(function (relationship) {
|
@@ -599,84 +598,96 @@ var MockCreateRequest = function(url, store, modelName, options) {
|
|
599
598
|
delete responseJson[modelName][relationship.key];
|
600
599
|
}
|
601
600
|
})
|
601
|
+
} else {
|
602
|
+
this.andFail(options);
|
602
603
|
}
|
603
|
-
|
604
|
-
}
|
604
|
+
};
|
605
605
|
|
606
606
|
this.match = function(matches) {
|
607
607
|
matchArgs = matches;
|
608
608
|
this.calculate();
|
609
609
|
return this;
|
610
|
-
}
|
610
|
+
};
|
611
611
|
|
612
612
|
this.andReturn = function(returns) {
|
613
613
|
returnArgs = returns;
|
614
614
|
this.calculate();
|
615
615
|
return this;
|
616
|
-
}
|
616
|
+
};
|
617
617
|
|
618
|
-
this.andFail = function() {
|
619
|
-
|
620
|
-
|
621
|
-
|
618
|
+
this.andFail = function(options) {
|
619
|
+
options = options || {}
|
620
|
+
succeed = false;
|
621
|
+
status = options.status || 500;
|
622
|
+
if (options.response) {
|
623
|
+
responseJson = options.response;
|
624
|
+
}
|
625
|
+
return this;
|
626
|
+
};
|
622
627
|
|
623
628
|
this.handler = function(settings) {
|
624
|
-
if (
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
629
|
+
if (succeed) {
|
630
|
+
if (matchArgs) {
|
631
|
+
var requestData = JSON.parse(settings.data)[modelName];
|
632
|
+
for (var attribute in expectedRequest) {
|
633
|
+
if (expectedRequest[attribute] &&
|
634
|
+
requestData[attribute] != expectedRequest[attribute]) {
|
635
|
+
return false;
|
636
|
+
}
|
632
637
|
}
|
633
638
|
}
|
639
|
+
this.status = 200;
|
640
|
+
} else {
|
641
|
+
this.status = status;
|
634
642
|
}
|
635
|
-
|
636
|
-
|
637
|
-
responseText: responseJson,
|
638
|
-
status: responseStatus
|
639
|
-
};
|
640
|
-
}
|
643
|
+
this.responseText = responseJson;
|
644
|
+
};
|
641
645
|
|
642
646
|
this.calculate();
|
643
647
|
|
644
|
-
|
648
|
+
var requestConfig = {
|
649
|
+
url: url,
|
650
|
+
dataType: 'json',
|
651
|
+
type: 'POST',
|
652
|
+
response: this.handler
|
653
|
+
};
|
654
|
+
|
655
|
+
$.mockjax(requestConfig);
|
645
656
|
};
|
646
657
|
|
647
658
|
var MockUpdateRequest = function(url, model, mapFind, options) {
|
648
659
|
var status = options.status || 200;
|
649
660
|
var succeed = true;
|
650
|
-
|
661
|
+
var response = null;
|
651
662
|
|
652
|
-
|
653
|
-
|
654
|
-
|
663
|
+
if ('succeed' in options) {
|
664
|
+
succeed = options.succeed;
|
665
|
+
}
|
655
666
|
|
656
|
-
|
657
|
-
|
658
|
-
|
667
|
+
if ('response' in options) {
|
668
|
+
response = options.response;
|
669
|
+
}
|
659
670
|
|
660
671
|
this.andSucceed = function(options) {
|
661
|
-
|
672
|
+
succeed = true;
|
662
673
|
return this;
|
663
674
|
};
|
664
675
|
|
665
676
|
this.andFail = function(options) {
|
666
677
|
succeed = false;
|
667
678
|
status = options.status || 500;
|
668
|
-
|
669
|
-
|
670
|
-
|
679
|
+
if ('response' in options) {
|
680
|
+
response = options.response;
|
681
|
+
}
|
671
682
|
return this;
|
672
683
|
};
|
673
684
|
|
674
685
|
this.handler = function(settings) {
|
675
686
|
if (!succeed) {
|
676
687
|
this.status = status;
|
677
|
-
|
678
|
-
|
679
|
-
|
688
|
+
if (response !== null) {
|
689
|
+
this.responseText = response;
|
690
|
+
}
|
680
691
|
} else {
|
681
692
|
var json = model.toJSON({includeId: true});
|
682
693
|
this.responseText = mapFind(model.constructor.typeKey, json);
|
@@ -1411,10 +1422,12 @@ if (FactoryGuy !== undefined) {
|
|
1411
1422
|
return null;
|
1412
1423
|
}
|
1413
1424
|
}
|
1414
|
-
|
1415
1425
|
// Inspect the data submitted in the request (either POST body or GET query string)
|
1416
1426
|
if ( handler.data ) {
|
1417
|
-
|
1427
|
+
// console.log('request.data', requestSettings.data )
|
1428
|
+
// console.log('handler.data', handler.data )
|
1429
|
+
// console.log('data equal', isMockDataEqual(handler.data, requestSettings.data) )
|
1430
|
+
if ( ! requestSettings.data || !isMockDataEqual(handler.data, requestSettings.data) ) {
|
1418
1431
|
// They're not identical, do not mock this request
|
1419
1432
|
return null;
|
1420
1433
|
}
|
@@ -1425,7 +1438,6 @@ if (FactoryGuy !== undefined) {
|
|
1425
1438
|
// The request type doesn't match (GET vs. POST)
|
1426
1439
|
return null;
|
1427
1440
|
}
|
1428
|
-
|
1429
1441
|
return handler;
|
1430
1442
|
}
|
1431
1443
|
|
@@ -1789,6 +1801,7 @@ if (FactoryGuy !== undefined) {
|
|
1789
1801
|
}
|
1790
1802
|
|
1791
1803
|
mockHandler = getMockForRequest( mockHandlers[k], requestSettings );
|
1804
|
+
|
1792
1805
|
if(!mockHandler) {
|
1793
1806
|
// No valid mock found for this request
|
1794
1807
|
continue;
|
@@ -1840,7 +1853,7 @@ if (FactoryGuy !== undefined) {
|
|
1840
1853
|
}
|
1841
1854
|
|
1842
1855
|
copyUrlParameters(mockHandler, origSettings);
|
1843
|
-
|
1856
|
+
//console.log('here copyUrlParameters', 'mockHandler=>',mockHandler, 'requestSettings=>',requestSettings, 'origSettings=>',origSettings)
|
1844
1857
|
(function(mockHandler, requestSettings, origSettings, origHandler) {
|
1845
1858
|
|
1846
1859
|
mockRequest = _ajax.call($, $.extend(true, {}, origSettings, {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ember-data-factory-guy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.11
|
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-
|
12
|
+
date: 2015-03-19 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easily create Fixtures for Ember Data
|
15
15
|
email:
|