Skip to content

Commit

Permalink
Extract non-polymorphic relationships by ID (fix #166)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmelvin0 committed Dec 28, 2015
1 parent a11021b commit 3d1fae5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
8 changes: 7 additions & 1 deletion addon/jsonapi-fixture-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ var JSONAPIFixtureConverter = function (store) {
relationships[relationship.key] = {data: normalizeJSONAPIAssociation(data, relationship)};
} else if (Ember.typeOf(belongsToRecord) === 'instance') {
relationships[relationship.key] = {data: normalizeJSONAPIAssociation(belongsToRecord, relationship)};
} else if (typeof belongsToRecord === 'string' || typeof belongsToRecord === 'number') {
Ember.assert('Polymorphic relationships cannot be specified by ID', !isPolymorphic);
relationships[relationship.key] = {data: normalizeJSONAPIAssociation({id: belongsToRecord, type: relationship.type}, relationship)};
}
} else if (relationship.kind === 'hasMany') {
var hasManyRecords = fixture[relationship.key];
Expand All @@ -142,6 +145,9 @@ var JSONAPIFixtureConverter = function (store) {
return normalizeJSONAPIAssociation(data, relationship);
} else if (Ember.typeOf(hasManyRecord) === 'instance') {
return normalizeJSONAPIAssociation(hasManyRecord, relationship);
} else if (typeof hasManyRecord === 'string' || typeof hasManyRecord === 'number') {
Ember.assert('Polymorphic relationships cannot be specified by ID', !isPolymorphic);
return normalizeJSONAPIAssociation({id: hasManyRecord, type: relationship.type}, relationship);
}
});
relationships[relationship.key] = {data: records};
Expand All @@ -154,4 +160,4 @@ var JSONAPIFixtureConverter = function (store) {

};

export default JSONAPIFixtureConverter;
export default JSONAPIFixtureConverter;
34 changes: 33 additions & 1 deletion tests/unit/shared-factory-guy-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,38 @@ SharedBehavior.makeTests = function () {
});
});

test("hasMany associations passed to make", function () {
var project1 = make('project', {id: 1, title: 'Project One'});
var project2 = make('project', {id: 2, title: 'Project Two'});
var user = make('user', {projects: [1, 2]});
equal(project2.get('user'), user);
equal(user.get('projects').objectAt(0), project1);
equal(user.get('projects.lastObject.title'), 'Project Two');
});

test("belongsTo association pass to make", function () {
var user = make('user', {id: 1});
var project = make('project', {title: 'The Project', user: 1});
equal(project.get('user'), user);
equal(user.get('projects').objectAt(0), project);
equal(user.get('projects.firstObject.title'), 'The Project');
});

test("hasMany associations passed to make throws error with polymorphic relationships", function () {
var smallHat = make('small-hat', {id: 1});
var bigHat = make('big-hat', {id: 2});
throws(function () {
var user = make('user', {hats: [1, 2]});
});
});

test("belongsTo association passed to make does not work with polymorphic relationship", function () {
var parentHat = make('hat', {id: 1});
throws(function () {
var childHat = make('hat', {hat: 1});
});
});

};

SharedBehavior.makeListTests = function () {
Expand Down Expand Up @@ -414,4 +446,4 @@ SharedBehavior.buildListJSONAPITests = function () {
};


export default SharedBehavior;
export default SharedBehavior;

0 comments on commit 3d1fae5

Please sign in to comment.