Skip to content

Commit

Permalink
fixing mockCreate(model) issues by no longer returning attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
danielspaniel committed Dec 26, 2017
1 parent 4deeba7 commit 887e7cd
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 106 deletions.
20 changes: 13 additions & 7 deletions addon/builder/fixture-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class {
@returns {*} new converted fixture
*/
convertForMake(modelName, fixture) {
let converter = new JSONAPIFixtureConverter(this.store, { transformKeys: false });
let converter = new JSONAPIFixtureConverter(this.store, {transformKeys: false});
return converter.convert(modelName, fixture);
}

Expand All @@ -92,15 +92,21 @@ export default class {
@returns {{}} JSONAPI formatted errors
*/
convertResponseErrors(object) {
let jsonAPIErrors = [];
Ember.assert('[ember-data-factory-guy] Your error response must have an errors key. The errors hash format is: {errors: {name: ["name too short"]}}', object.errors);
let errors = object.errors;
let jsonAPIErrors = [],
{errors} = object;

Ember.assert(
`[ember-data-factory-guy] Your error response must have an errors key.
The errors hash format is: {errors: {name: ["name too short"]}}`,
errors
);

for (let key in errors) {
let description = Ember.typeOf(errors[key]) === "array" ? errors[key][0] : errors[key],
source = { pointer: "data/attributes/" + key },
newError = { detail: description, title: "invalid " + key, source: source };
source = {pointer: "data/attributes/" + key},
newError = {detail: description, title: "invalid " + key, source: source};
jsonAPIErrors.push(newError);
}
return { errors: jsonAPIErrors };
return {errors: jsonAPIErrors};
}
}
11 changes: 2 additions & 9 deletions addon/mocks/exposed-request-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,10 @@ export function mockQueryRecord(modelName, queryParams) {
@param {String} modelName name of model you're creating like 'profile' for Profile
*/
export function mockCreate(...args) {

let model, modelName, attrs = {};
let model, modelName;
if (args[0] instanceof Model) {
model = args[0];
modelName = model.constructor.modelName;
// need (rest style) object with attributes.
// convert the json if it is json-api to this style
let json = model.toJSON(),
builder = FactoryGuy.fixtureBuilder(modelName);
builder.wrapPayload(modelName, json);
attrs = json.get();
} else {
if (typeof args[0] === "string") {
[modelName] = args;
Expand All @@ -316,7 +309,7 @@ export function mockCreate(...args) {

Ember.assert(`[ember-data-factory-guy] To mockUpdate pass in a model instance or a modelName`, modelName);

return new MockCreateRequest(modelName, {model}).returns({attrs});
return new MockCreateRequest(modelName, {model});
}

/**
Expand Down
Loading

0 comments on commit 887e7cd

Please sign in to comment.