Skip to content

Commit

Permalink
Merge pull request #63 from pedrokiefer/handleUpdate
Browse files Browse the repository at this point in the history
[WIP] Handle update
  • Loading branch information
danielspaniel committed Feb 23, 2015
2 parents aca03e7 + bb7242e commit 42d3295
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 107 deletions.
3 changes: 3 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = function(grunt) {
'src/model_definition.js',
'src/factory_guy.js',
'src/mock_create_request.js',
'src/mock_update_request.js',
'src/store.js',
'src/factory_guy_test_mixin.js',
],
Expand All @@ -23,6 +24,7 @@ module.exports = function(grunt) {
'src/model_definition.js',
'src/factory_guy.js',
'src/mock_create_request.js',
'src/mock_update_request.js',
'src/store.js',
'src/factory_guy_test_mixin.js',
'src/epilogue.js'
Expand All @@ -40,6 +42,7 @@ module.exports = function(grunt) {
'src/model_definition.js',
'src/factory_guy.js',
'src/mock_create_request.js',
'src/mock_update_request.js',
'src/store.js',
'src/factory_guy_test_mixin.js',
'bower_components/jquery-mockjax/jquery.mockjax.js'],
Expand Down
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,17 @@ chainable methods, or options hash.
##### handleUpdate
- Use chainable methods to build response:
- andFail - request should fail, use options argument to pass status and response text
- andSucceed - update should succeed, this is the default behavior, use this after a ```andFail``` call
- handleUpdate(model)
- handleUpdate(modelType, id)
- Use hash of options to build response:
- status - HTTP status code, defaults to 200.
- response - what the server has responded, only used on failure cases, default is empty on failure and model json on succees.
- succeed - indicates if the resquest should succeed, defaults to true.
- need to wrap tests using handleUpdate with: Ember.run.function() { 'your test' })
*success case is the default*
Expand All @@ -834,15 +843,54 @@ chainable methods, or options hash.
profile.save() //=> will succeed
````

######Using chainable methods

*mocking a failed update*

```javascript
var profile = FactoryGuy.make('profile');
// set the succeed flag to 'false'
testHelper.handleUpdate('profile', profile.id).andFail({status: 422, response: "{error: 'Invalid data'}"});
// or
testHelper.handleUpdate(profile).andFail({status: 422, response: "{error: 'Invalid data'}"});
profile.set('description', 'bad value');
profile.save() //=> will fail
````
*mocking a failed update and retry with succees*
```javascript
var profile = FactoryGuy.make('profile');

// set the succeed flag to 'false'
var mockUpdate = testHelper.handleUpdate('profile', profile.id);
// or
var mockUpdate = testHelper.handleUpdate(profile);

mockUpdate.andFail({status: 422, response: "{error: 'Invalid data'}"});

profile.set('description', 'bad value');
profile.save() //=> will fail

// Some logic for retrying...

mockUpdate.andSucceed();
profile.save() //=> will succeed!
````

######Using hash of options

*mocking a failed update*

```javascript
var profile = FactoryGuy.make('profile');
// set the succeed flag to 'false'
testHelper.handleUpdate('profile', profile.id, false);
testHelper.handleUpdate('profile', profile.id, {succeed: false});
// or
testHelper.handleUpdate(profile, false);
testHelper.handleUpdate(profile, {succeed: false});
profile.set('description', 'bad value');
profile.save() //=> will fail
Expand Down
84 changes: 67 additions & 17 deletions dist/amd/factory-guy.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,55 @@ var MockCreateRequest = function(url, store, modelName, options) {
$.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({
/**
Expand Down Expand Up @@ -1196,34 +1245,35 @@ var FactoryGuyTestMixin = Em.Mixin.create({
Handling ajax PUT ( update record ) for a model type. You can mock
failed update by passing in success argument as false.
@param {String} type model type like 'user' for User model
@param {String} type model type like 'user' for User model, or a model instance
@param {String} id id of record to update
@param {Boolean} succeed optional flag to indicate if the request
should succeed ( default is true )
@param {Object} options options object
*/
handleUpdate: function () {
var args = Array.prototype.slice.call(arguments)
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;

var options = {};
if (args.length > 1 && typeof args[args.length - 1] === 'object') {
options = args.pop();
}
Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0)
var type, id;

var model, type, id;
var store = this.getStore();

if (args[0] instanceof DS.Model) {
var model = args[0];
type = model.constructor.typeKey;
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)
this.stubEndpointForHttpRequest(this.buildURL(type, id), {}, {
type: 'PUT',
status: succeed ? 200 : 500
});
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);
},
/**
Handling ajax DELETE ( delete record ) for a model type. You can mock
Expand Down
84 changes: 67 additions & 17 deletions dist/ember-data-factory-guy.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,55 @@ var MockCreateRequest = function(url, store, modelName, options) {
$.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({
/**
Expand Down Expand Up @@ -1191,34 +1240,35 @@ var FactoryGuyTestMixin = Em.Mixin.create({
Handling ajax PUT ( update record ) for a model type. You can mock
failed update by passing in success argument as false.
@param {String} type model type like 'user' for User model
@param {String} type model type like 'user' for User model, or a model instance
@param {String} id id of record to update
@param {Boolean} succeed optional flag to indicate if the request
should succeed ( default is true )
@param {Object} options options object
*/
handleUpdate: function () {
var args = Array.prototype.slice.call(arguments)
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;

var options = {};
if (args.length > 1 && typeof args[args.length - 1] === 'object') {
options = args.pop();
}
Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0)
var type, id;

var model, type, id;
var store = this.getStore();

if (args[0] instanceof DS.Model) {
var model = args[0];
type = model.constructor.typeKey;
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)
this.stubEndpointForHttpRequest(this.buildURL(type, id), {}, {
type: 'PUT',
status: succeed ? 200 : 500
});
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);
},
/**
Handling ajax DELETE ( delete record ) for a model type. You can mock
Expand Down
2 changes: 1 addition & 1 deletion dist/ember-data-factory-guy.min.js

Large diffs are not rendered by default.

35 changes: 18 additions & 17 deletions src/factory_guy_test_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,34 +268,35 @@ var FactoryGuyTestMixin = Em.Mixin.create({
Handling ajax PUT ( update record ) for a model type. You can mock
failed update by passing in success argument as false.
@param {String} type model type like 'user' for User model
@param {String} type model type like 'user' for User model, or a model instance
@param {String} id id of record to update
@param {Boolean} succeed optional flag to indicate if the request
should succeed ( default is true )
@param {Object} options options object
*/
handleUpdate: function () {
var args = Array.prototype.slice.call(arguments)
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;

var options = {};
if (args.length > 1 && typeof args[args.length - 1] === 'object') {
options = args.pop();
}
Ember.assert("To handleUpdate pass in a model instance or a type and an id",args.length>0)
var type, id;

var model, type, id;
var store = this.getStore();

if (args[0] instanceof DS.Model) {
var model = args[0];
type = model.constructor.typeKey;
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)
this.stubEndpointForHttpRequest(this.buildURL(type, id), {}, {
type: 'PUT',
status: succeed ? 200 : 500
});
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);
},
/**
Handling ajax DELETE ( delete record ) for a model type. You can mock
Expand Down
Loading

0 comments on commit 42d3295

Please sign in to comment.