Skip to content

Commit

Permalink
adding test and merging fix for self referential parent/child buildList
Browse files Browse the repository at this point in the history
  • Loading branch information
danielspaniel committed Nov 16, 2016
1 parent ac5fec3 commit 874737d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 49 deletions.
12 changes: 11 additions & 1 deletion tests/dummy/app/tests/factories/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ import FactoryGuy from 'ember-data-factory-guy';

FactoryGuy.define("group", {
sequences: {
name: function(num) {return 'Group' + num;}
name: (num)=>`Group ${num}`
},
default: {
type: "Group",
name: FactoryGuy.generate('name')
},
traits: {
primary: {
name: 'Primary Group',
group: FactoryGuy.belongsTo('group', 'parent')
},
parent: {
name: 'Parent Group',
group: null
}
}
});
122 changes: 74 additions & 48 deletions tests/unit/rest-adapter-test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import Ember from 'ember';
import {moduleFor, test} from 'ember-qunit';
import FactoryGuy, { build, buildList, make, makeList, mockCreate, mockFindRecord, mockFindAll, manualSetup } from 'ember-data-factory-guy';
import FactoryGuy, {build, buildList, make, makeList, mockCreate, mockFindRecord, mockFindAll, manualSetup} from 'ember-data-factory-guy';

import SharedAdapterBehavior from './shared-adapter-tests';
import SharedFactoryGuyTestHelperBehavior from './shared-factory-guy-test-helper-tests';
import { inlineSetup } from '../helpers/utility-methods';
import {inlineSetup} from '../helpers/utility-methods';

let serializer = 'DS.RESTSerializer';
let serializerType = '-rest';

SharedAdapterBehavior.all(serializer, serializerType);

SharedFactoryGuyTestHelperBehavior.mockFindRecordSideloadingTests(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockFindAllSideloadingTests(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockFindRecordSideloadingTests(serializer, serializerType);
SharedFactoryGuyTestHelperBehavior.mockFindAllSideloadingTests(serializer, serializerType);

SharedFactoryGuyTestHelperBehavior.mockFindRecordEmbeddedTests(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockFindAllEmbeddedTests(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockFindRecordEmbeddedTests(serializer, serializerType);
SharedFactoryGuyTestHelperBehavior.mockFindAllEmbeddedTests(serializer, serializerType);

SharedFactoryGuyTestHelperBehavior.mockQueryMetaTests(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockQueryMetaTests(serializer, serializerType);

SharedFactoryGuyTestHelperBehavior.mockUpdateWithErrorMessages(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockUpdateReturnsAssociations(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockUpdateReturnsEmbeddedAssociations(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockUpdateWithErrorMessages(serializer, serializerType);
SharedFactoryGuyTestHelperBehavior.mockUpdateReturnsAssociations(serializer, serializerType);
SharedFactoryGuyTestHelperBehavior.mockUpdateReturnsEmbeddedAssociations(serializer, serializerType);

SharedFactoryGuyTestHelperBehavior.mockCreateReturnsAssociations(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockCreateReturnsEmbeddedAssociations(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockCreateFailsWithErrorResponse(serializer,serializerType);
SharedFactoryGuyTestHelperBehavior.mockCreateReturnsAssociations(serializer, serializerType);
SharedFactoryGuyTestHelperBehavior.mockCreateReturnsEmbeddedAssociations(serializer, serializerType);
SharedFactoryGuyTestHelperBehavior.mockCreateFailsWithErrorResponse(serializer, serializerType);

moduleFor('serializer:application', `${serializer} #mockCreate custom`, inlineSetup(serializerType));

Expand Down Expand Up @@ -77,7 +77,7 @@ test("returns an attribute for a key", function(assert) {

test("returns a relationship with a key", function(assert) {
let user = build('user', 'with_company');
assert.deepEqual(user.get('company'), {id: 1, type: 'company'});
assert.deepEqual(user.get('company'), { id: 1, type: 'company' });
});

moduleFor('serializer:application', `${serializer} FactoryGuy#buildList get`, inlineSetup(serializerType));
Expand All @@ -97,7 +97,7 @@ test("returns an attribute with a key", function(assert) {

test("returns a relationship with an index and key", function(assert) {
let user = buildList('user', 2, 'with_company');
assert.deepEqual(user.get(1).company, {id: 2, type: 'company'});
assert.deepEqual(user.get(1).company, { id: 2, type: 'company' });
});

// model fragments
Expand All @@ -119,6 +119,46 @@ test("with model fragment returns an attribute with a key", function(assert) {

moduleFor('serializer:application', `${serializer} FactoryGuy#build custom`, inlineSetup(serializerType));

test("serializes custom attributes types", function(assert) {
let info = { first: 1 };
let buildJson = build('user', { info: info });
buildJson.unwrap();

let expectedJson = {
user: {
id: 1,
name: 'User1',
style: "normal",
info: '{"first":1}'
}
};

assert.deepEqual(buildJson, expectedJson);
});

test("uses serializers payloadKeyFromModelName function", function(assert) {
let serializer = FactoryGuy.store.serializerFor('application');
let savedPayloadKeyFromModelNameFn = serializer.payloadKeyFromModelName;
serializer.payloadKeyFromModelName = function() {
return "dude";
};

let buildJson = build('user');
buildJson.unwrap();

let expectedJson = {
dude: {
id: 1,
name: 'User1',
style: "normal"
}
};

assert.deepEqual(buildJson, expectedJson);

serializer.payloadKeyFromModelName = savedPayloadKeyFromModelNameFn;
});

test("sideloads belongsTo records which are built from fixture definition that just has empty object {}", function(assert) {
let buildJson = build('user', 'with_company');
buildJson.unwrap();
Expand Down Expand Up @@ -623,7 +663,7 @@ test("using custom serializer with property forbidden for serialization", functi
serialize: false
}
};
let profile = build('profile', 'with_created_at', {created_at: date});
let profile = build('profile', 'with_created_at', { created_at: date });
assert.equal(profile.get("created_at"), date.toJSON());
});

Expand All @@ -635,7 +675,7 @@ test("with model that has primaryKey defined in serializer ( FactoryGuy sets pri
});

test("with model that has primaryKey defined in serializer ( user sets primaryKey value )", function(assert) {
let cat = build('cat', {catId: 'meow1'});
let cat = build('cat', { catId: 'meow1' });

assert.equal(cat.get('id'), 'meow1');
});
Expand Down Expand Up @@ -729,42 +769,28 @@ test("sideloads hasMany records", function(assert) {
assert.deepEqual(buildJson, expectedJson);
});

test("serializes custom attributes types", function(assert) {
let info = { first: 1 };
let buildJson = build('user', { info: info });
buildJson.unwrap();

let expectedJson = {
user: {
id: 1,
name: 'User1',
style: "normal",
info: '{"first":1}'
}
};

assert.deepEqual(buildJson, expectedJson);
});

test("uses serializers payloadKeyFromModelName function", function(assert) {
let serializer = FactoryGuy.store.serializerFor('application');
let savedPayloadKeyFromModelNameFn = serializer.payloadKeyFromModelName;
serializer.payloadKeyFromModelName = function() {
return "dude";
};

let buildJson = build('user');
test("handles self referential parent child relationship", function(assert) {
let buildJson = buildList('group', 'primary');
buildJson.unwrap();

let expectedJson = {
dude: {
id: 1,
name: 'User1',
style: "normal"
}
groups: [
{
group: 2,
id: 1,
name: 'Primary Group',
type: "Group"
},
{
group: undefined,
id: 2,
name: 'Parent Group',
type: "Group"
}
]
};

assert.deepEqual(buildJson, expectedJson);
});

serializer.payloadKeyFromModelName = savedPayloadKeyFromModelNameFn;
});

0 comments on commit 874737d

Please sign in to comment.