diff --git a/packages/ember-data/lib/serializers/embedded-records-mixin.js b/packages/ember-data/lib/serializers/embedded-records-mixin.js index 15d1764839f..b593cf10c76 100644 --- a/packages/ember-data/lib/serializers/embedded-records-mixin.js +++ b/packages/ember-data/lib/serializers/embedded-records-mixin.js @@ -402,7 +402,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({ @private */ _extractEmbeddedRecords: function(serializer, store, typeClass, partial) { - if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && this.get('isNewSerializerAPI')) { + if (this.get('isNewSerializerAPI')) { return _newExtractEmbeddedRecords.apply(this, arguments); } @@ -434,7 +434,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({ @private */ _extractEmbeddedHasMany: function(store, key, embeddedTypeClass, hash) { - if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && this.get('isNewSerializerAPI')) { + if (this.get('isNewSerializerAPI')) { return _newExtractEmbeddedHasMany.apply(this, arguments); } @@ -486,7 +486,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({ @private */ _extractEmbeddedBelongsTo: function(store, key, embeddedTypeClass, hash) { - if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && this.get('isNewSerializerAPI')) { + if (this.get('isNewSerializerAPI')) { return _newExtractEmbeddedBelongsTo.apply(this, arguments); } diff --git a/packages/ember-data/lib/serializers/json-serializer.js b/packages/ember-data/lib/serializers/json-serializer.js index 2ead6b69954..1d3dd6a49fb 100644 --- a/packages/ember-data/lib/serializers/json-serializer.js +++ b/packages/ember-data/lib/serializers/json-serializer.js @@ -483,7 +483,7 @@ export default Serializer.extend({ @return {Object} */ normalize: function(typeClass, hash) { - if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && this.get('isNewSerializerAPI')) { + if (this.get('isNewSerializerAPI')) { return _newNormalize.apply(this, arguments); } @@ -1495,7 +1495,7 @@ export default Serializer.extend({ @param {Object} payload */ extractMeta: function(store, typeClass, payload) { - if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && this.get('isNewSerializerAPI')) { + if (this.get('isNewSerializerAPI')) { return _newExtractMeta.apply(this, arguments); } diff --git a/packages/ember-data/lib/serializers/rest-serializer.js b/packages/ember-data/lib/serializers/rest-serializer.js index 085ad39b534..713f721137b 100644 --- a/packages/ember-data/lib/serializers/rest-serializer.js +++ b/packages/ember-data/lib/serializers/rest-serializer.js @@ -175,7 +175,7 @@ var RESTSerializer = JSONSerializer.extend({ @return {Object} */ normalize: function(typeClass, hash, prop) { - if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && this.get('isNewSerializerAPI')) { + if (this.get('isNewSerializerAPI')) { _newNormalize.apply(this, arguments); return this._super(...arguments); } @@ -651,7 +651,7 @@ var RESTSerializer = JSONSerializer.extend({ @param {Object} rawPayload */ pushPayload: function(store, rawPayload) { - if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && this.get('isNewSerializerAPI')) { + if (this.get('isNewSerializerAPI')) { _newPushPayload.apply(this, arguments); return; } diff --git a/packages/ember-data/lib/system/serializer.js b/packages/ember-data/lib/system/serializer.js index 06cc95e2942..de9d23c5fe4 100644 --- a/packages/ember-data/lib/system/serializer.js +++ b/packages/ember-data/lib/system/serializer.js @@ -28,11 +28,6 @@ var Serializer = Ember.Object.extend({ This is only to be used temporarily during the transition from the old serializer API to the new one. - To activate the new Serializer API you need to enable the feature flag - `ds-new-serializer-api`. - - http://guides.emberjs.com/v1.12.0/configuring-ember/feature-flags/ - This makes the store and the built-in serializers use the new Serializer API. diff --git a/packages/ember-data/lib/system/store/finders.js b/packages/ember-data/lib/system/store/finders.js index ad4c0779728..e9d9e4412ac 100644 --- a/packages/ember-data/lib/system/store/finders.js +++ b/packages/ember-data/lib/system/store/finders.js @@ -86,7 +86,7 @@ export function _findHasMany(adapter, store, internalModel, link, relationship) //TODO Use a non record creating push var records = pushPayload(store, payload); var recordArray = map(records, function(record) { return record._internalModel; }); - if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && serializer.get('isNewSerializerAPI')) { + if (serializer.get('isNewSerializerAPI')) { recordArray.meta = payload.meta; } return recordArray; diff --git a/packages/ember-data/lib/system/store/serializer-response.js b/packages/ember-data/lib/system/store/serializer-response.js index d1aba0fd0d0..c416813a298 100644 --- a/packages/ember-data/lib/system/store/serializer-response.js +++ b/packages/ember-data/lib/system/store/serializer-response.js @@ -4,9 +4,8 @@ var map = Ember.EnumerableUtils.map; /** This is a helper method that always returns a JSON-API Document. - If the feature flag `ds-new-serializer-api` is enabled and the current serializer - has `isNewSerializerAPI` set to `true` this helper calls `normalizeResponse` - instead of `extract`. + If the current serializer has `isNewSerializerAPI` set to `true` + this helper calls `normalizeResponse` instead of `extract`. All the built-in serializers get `isNewSerializerAPI` set to `true` automatically if the feature flag is enabled. @@ -21,12 +20,10 @@ var map = Ember.EnumerableUtils.map; @return {Object} JSON-API Document */ export function normalizeResponseHelper(serializer, store, modelClass, payload, id, requestType) { - if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && serializer.get('isNewSerializerAPI')) { + if (serializer.get('isNewSerializerAPI')) { return serializer.normalizeResponse(store, modelClass, payload, id, requestType); } else { - if (Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - Ember.deprecate('Your custom serializer uses the old version of the Serializer API, with `extract` hooks. Please upgrade your serializers to the new Serializer API using `normalizeResponse` hooks instead.'); - } + Ember.deprecate('Your custom serializer uses the old version of the Serializer API, with `extract` hooks. Please upgrade your serializers to the new Serializer API using `normalizeResponse` hooks instead.'); let serializerPayload = serializer.extract(store, modelClass, payload, id, requestType); return _normalizeSerializerPayload(modelClass, serializerPayload); } diff --git a/packages/ember-data/tests/integration/relationships/has-many-test.js b/packages/ember-data/tests/integration/relationships/has-many-test.js index 8c9fd1a6c68..4bd0482eeae 100644 --- a/packages/ember-data/tests/integration/relationships/has-many-test.js +++ b/packages/ember-data/tests/integration/relationships/has-many-test.js @@ -1446,97 +1446,94 @@ test("Model's belongsTo relationship should be created during 'get' method", fun }); }); -if (Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - - test("metadata is accessible when pushed as a meta property for a relationship", function() { - expect(1); - var book; - env.adapter.findHasMany = function() { - return resolve({}); - }; +test("metadata is accessible when pushed as a meta property for a relationship", function() { + expect(1); + var book; + env.adapter.findHasMany = function() { + return resolve({}); + }; - run(function() { - book = env.store.push('book', { id: 1, title: 'Sailing the Seven Seas', meta: { chapters: 'the lefkada sea' }, links: { chapters: '/chapters' } }); - }); + run(function() { + book = env.store.push('book', { id: 1, title: 'Sailing the Seven Seas', meta: { chapters: 'the lefkada sea' }, links: { chapters: '/chapters' } }); + }); - run(function() { - equal(book._internalModel._relationships.get('chapters').meta, 'the lefkada sea', 'meta is there'); - }); + run(function() { + equal(book._internalModel._relationships.get('chapters').meta, 'the lefkada sea', 'meta is there'); }); +}); - test("metadata is accessible when return from a fetchLink", function() { - expect(1); - env.registry.register('serializer:application', DS.RESTSerializer.extend({ isNewSerializerAPI: true })); +test("metadata is accessible when return from a fetchLink", function() { + expect(1); + env.registry.register('serializer:application', DS.RESTSerializer.extend({ isNewSerializerAPI: true })); - env.adapter.findHasMany = function() { - return resolve({ - meta: { - foo: 'bar' - }, - chapters: [ - { id: '2' }, - { id: '3' } - ] - }); - }; + env.adapter.findHasMany = function() { + return resolve({ + meta: { + foo: 'bar' + }, + chapters: [ + { id: '2' }, + { id: '3' } + ] + }); + }; - var book; + var book; - run(function() { - book = env.store.push('book', { id: 1, title: 'Sailing the Seven Seas', links: { chapters: '/chapters' } }); - }); + run(function() { + book = env.store.push('book', { id: 1, title: 'Sailing the Seven Seas', links: { chapters: '/chapters' } }); + }); - run(function() { - book.get('chapters').then(function(chapters) { - var meta = chapters.get('meta'); - equal(get(meta, 'foo'), 'bar', 'metadata is available'); - }); + run(function() { + book.get('chapters').then(function(chapters) { + var meta = chapters.get('meta'); + equal(get(meta, 'foo'), 'bar', 'metadata is available'); }); }); +}); - test("metadata should be reset between requests", function() { - var counter = 0; - env.registry.register('serializer:application', DS.RESTSerializer.extend({ isNewSerializerAPI: true })); +test("metadata should be reset between requests", function() { + var counter = 0; + env.registry.register('serializer:application', DS.RESTSerializer.extend({ isNewSerializerAPI: true })); - env.adapter.findHasMany = function() { - var data = { - meta: { - foo: 'bar' - }, - chapters: [ - { id: '2' }, - { id: '3' } - ] - }; + env.adapter.findHasMany = function() { + var data = { + meta: { + foo: 'bar' + }, + chapters: [ + { id: '2' }, + { id: '3' } + ] + }; - ok(true, 'findHasMany should be called twice'); + ok(true, 'findHasMany should be called twice'); - if (counter === 1) { - delete data.meta; - } + if (counter === 1) { + delete data.meta; + } - counter++; + counter++; - return resolve(data); - }; + return resolve(data); + }; - var book1, book2; + var book1, book2; - run(function() { - book1 = env.store.push('book', { id: 1, title: 'Sailing the Seven Seas', links: { chapters: 'chapters' } }); - book2 = env.store.push('book', { id: 2, title: 'Another book title', links: { chapters: 'chapters' } }); - }); + run(function() { + book1 = env.store.push('book', { id: 1, title: 'Sailing the Seven Seas', links: { chapters: 'chapters' } }); + book2 = env.store.push('book', { id: 2, title: 'Another book title', links: { chapters: 'chapters' } }); + }); - run(function() { - book1.get('chapters').then(function(chapters) { - var meta = chapters.get('meta'); - equal(get(meta, 'foo'), 'bar', 'metadata should available'); + run(function() { + book1.get('chapters').then(function(chapters) { + var meta = chapters.get('meta'); + equal(get(meta, 'foo'), 'bar', 'metadata should available'); - book2.get('chapters').then(function(chapters) { - var meta = chapters.get('meta'); - equal(meta, undefined, 'metadata should not be available'); - }); + book2.get('chapters').then(function(chapters) { + var meta = chapters.get('meta'); + equal(meta, undefined, 'metadata should not be available'); }); }); }); -} +}); diff --git a/packages/ember-data/tests/integration/serializers/embedded-records-mixin-new-test.js b/packages/ember-data/tests/integration/serializers/embedded-records-mixin-new-test.js index b1224e40dd3..741183b12f9 100644 --- a/packages/ember-data/tests/integration/serializers/embedded-records-mixin-new-test.js +++ b/packages/ember-data/tests/integration/serializers/embedded-records-mixin-new-test.js @@ -71,1130 +71,1126 @@ module("integration/embedded_records_mixin - EmbeddedRecordsMixin (new API)", { } }); -if (Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - - test("normalizeSingleResponse with embedded objects", function() { - env.registry.register('adapter:super-villain', DS.RESTAdapter); - env.registry.register('serializer:super-villain', TestSerializer.extend()); - env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - villains: { embedded: 'always' } - } - })); - - var serializer = env.container.lookup("serializer:home-planet"); - var json_hash = { - homePlanet: { - id: "1", - name: "Umber", - villains: [{ - id: "2", - firstName: "Tom", - lastName: "Dale" - }] - } - }; - var json; +test("normalizeSingleResponse with embedded objects", function() { + env.registry.register('adapter:super-villain', DS.RESTAdapter); + env.registry.register('serializer:super-villain', TestSerializer.extend()); + env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + villains: { embedded: 'always' } + } + })); + + var serializer = env.container.lookup("serializer:home-planet"); + var json_hash = { + homePlanet: { + id: "1", + name: "Umber", + villains: [{ + id: "2", + firstName: "Tom", + lastName: "Dale" + }] + } + }; + var json; - run(function() { - json = serializer.normalizeSingleResponse(env.store, HomePlanet, json_hash, '1', 'find'); - }); + run(function() { + json = serializer.normalizeSingleResponse(env.store, HomePlanet, json_hash, '1', 'find'); + }); - deepEqual(json, { - "data": { - "id": "1", - "type": "home-planet", - "attributes": { - "name": "Umber" - }, - "relationships": { - "villains": { - "data": [ - { "id": "2", "type": "super-villain" } - ] - } - } + deepEqual(json, { + "data": { + "id": "1", + "type": "home-planet", + "attributes": { + "name": "Umber" }, - "included": [ - { - "id": "2", - "type": "super-villain", - "attributes": { - "firstName": "Tom", - "lastName": "Dale" - }, - "relationships": {} + "relationships": { + "villains": { + "data": [ + { "id": "2", "type": "super-villain" } + ] } - ] - }); - }); - - test("normalizeSingleResponse with embedded objects inside embedded objects", function() { - env.registry.register('adapter:super-villain', DS.RESTAdapter); - env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - villains: { embedded: 'always' } - } - })); - env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - evilMinions: { embedded: 'always' } - } - })); - env.registry.register('serializer:evil-minion', TestSerializer); - - var serializer = env.container.lookup("serializer:home-planet"); - var json_hash = { - homePlanet: { - id: "1", - name: "Umber", - villains: [{ - id: "2", - firstName: "Tom", - lastName: "Dale", - evilMinions: [{ - id: "3", - name: "Alex" - }] - }] } - }; - var json; - - run(function() { - json = serializer.normalizeSingleResponse(env.store, HomePlanet, json_hash, '1', 'find'); - }); - - deepEqual(json, { - "data": { - "id": "1", - "type": "home-planet", - "attributes": { - "name": "Umber" - }, - "relationships": { - "villains": { - "data": [ - { "id": "2", "type": "super-villain" } - ] - } - } - }, - "included": [{ + }, + "included": [ + { "id": "2", "type": "super-villain", "attributes": { "firstName": "Tom", "lastName": "Dale" }, - "relationships": { - "evilMinions": { - "data": [ - { "id": "3", "type": "evil-minion" } - ] - } - } - }, { - "id": "3", - "type": "evil-minion", - "attributes": { - "name": "Alex" - }, "relationships": {} - }] - }); - }); - - test("normalizeSingleResponse with embedded objects of same type", function() { - env.registry.register('adapter:comment', DS.RESTAdapter); - env.registry.register('serializer:comment', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - children: { embedded: 'always' } } - })); + ] + }); +}); - var serializer = env.container.lookup("serializer:comment"); - var json_hash = { - comment: { - id: "1", - body: "Hello", - root: true, - children: [{ - id: "2", - body: "World", - root: false - }, - { +test("normalizeSingleResponse with embedded objects inside embedded objects", function() { + env.registry.register('adapter:super-villain', DS.RESTAdapter); + env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + villains: { embedded: 'always' } + } + })); + env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + evilMinions: { embedded: 'always' } + } + })); + env.registry.register('serializer:evil-minion', TestSerializer); + + var serializer = env.container.lookup("serializer:home-planet"); + var json_hash = { + homePlanet: { + id: "1", + name: "Umber", + villains: [{ + id: "2", + firstName: "Tom", + lastName: "Dale", + evilMinions: [{ id: "3", - body: "Foo", - root: false + name: "Alex" }] - } - }; - var json; - run(function() { - json = serializer.normalizeSingleResponse(env.store, Comment, json_hash, '1', 'find'); - }); - - deepEqual(json, { - "data": { - "id": "1", - "type": "comment", - "attributes": { - "body": "Hello", - "root": true - }, - "relationships": { - "children": { - "data": [ - { "id": "2", "type": "comment" }, - { "id": "3", "type": "comment" } - ] - } - } - }, - "included": [{ - "id": "2", - "type": "comment", - "attributes": { - "body": "World", - "root": false - }, - "relationships": {} - }, { - "id": "3", - "type": "comment", - "attributes": { - "body": "Foo", - "root": false - }, - "relationships": {} }] - }, "Primary record was correct"); - }); + } + }; + var json; - test("normalizeSingleResponse with embedded objects inside embedded objects of same type", function() { - env.registry.register('adapter:comment', DS.RESTAdapter); - env.registry.register('serializer:comment', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - children: { embedded: 'always' } - } - })); - - var serializer = env.container.lookup("serializer:comment"); - var json_hash = { - comment: { - id: "1", - body: "Hello", - root: true, - children: [{ - id: "2", - body: "World", - root: false, - children: [{ - id: "4", - body: "Another", - root: false - }] - }, - { - id: "3", - body: "Foo", - root: false - }] - } - }; - var json; - run(function() { - json = serializer.normalizeSingleResponse(env.store, Comment, json_hash, '1', 'find'); - }); + run(function() { + json = serializer.normalizeSingleResponse(env.store, HomePlanet, json_hash, '1', 'find'); + }); - deepEqual(json, { - "data": { - "id": "1", - "type": "comment", - "attributes": { - "body": "Hello", - "root": true - }, - "relationships": { - "children": { - "data": [ - { "id": "2", "type": "comment" }, - { "id": "3", "type": "comment" } - ] - } + deepEqual(json, { + "data": { + "id": "1", + "type": "home-planet", + "attributes": { + "name": "Umber" + }, + "relationships": { + "villains": { + "data": [ + { "id": "2", "type": "super-villain" } + ] } + } + }, + "included": [{ + "id": "2", + "type": "super-villain", + "attributes": { + "firstName": "Tom", + "lastName": "Dale" }, - "included": [{ - "id": "2", - "type": "comment", - "attributes": { - "body": "World", - "root": false - }, - "relationships": { - "children": { - "data": [ - { "id": "4", "type": "comment" } - ] - } + "relationships": { + "evilMinions": { + "data": [ + { "id": "3", "type": "evil-minion" } + ] } - }, { - "id": "4", - "type": "comment", - "attributes": { - "body": "Another", - "root": false - }, - "relationships": {} - }, { - "id": "3", - "type": "comment", - "attributes": { - "body": "Foo", - "root": false - }, - "relationships": {} - }] - }, "Primary record was correct"); + } + }, { + "id": "3", + "type": "evil-minion", + "attributes": { + "name": "Alex" + }, + "relationships": {} + }] }); +}); - test("normalizeSingleResponse with embedded objects of same type, but from separate attributes", function() { - HomePlanet.reopen({ - reformedVillains: DS.hasMany('superVillain', { inverse: null }) - }); +test("normalizeSingleResponse with embedded objects of same type", function() { + env.registry.register('adapter:comment', DS.RESTAdapter); + env.registry.register('serializer:comment', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + children: { embedded: 'always' } + } + })); + + var serializer = env.container.lookup("serializer:comment"); + var json_hash = { + comment: { + id: "1", + body: "Hello", + root: true, + children: [{ + id: "2", + body: "World", + root: false + }, + { + id: "3", + body: "Foo", + root: false + }] + } + }; + var json; + run(function() { + json = serializer.normalizeSingleResponse(env.store, Comment, json_hash, '1', 'find'); + }); - env.registry.register('adapter:home-planet', DS.RESTAdapter); - env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - villains: { embedded: 'always' }, - reformedVillains: { embedded: 'always' } + deepEqual(json, { + "data": { + "id": "1", + "type": "comment", + "attributes": { + "body": "Hello", + "root": true + }, + "relationships": { + "children": { + "data": [ + { "id": "2", "type": "comment" }, + { "id": "3", "type": "comment" } + ] + } } - })); - env.registry.register('serializer:super-villain', TestSerializer); + }, + "included": [{ + "id": "2", + "type": "comment", + "attributes": { + "body": "World", + "root": false + }, + "relationships": {} + }, { + "id": "3", + "type": "comment", + "attributes": { + "body": "Foo", + "root": false + }, + "relationships": {} + }] + }, "Primary record was correct"); +}); - var serializer = env.container.lookup("serializer:home-planet"); - var json_hash = { - homePlanet: { - id: "1", - name: "Earth", - villains: [{ - id: "1", - firstName: "Tom" - }, { - id: "3", - firstName: "Yehuda" - }], - reformedVillains: [{ - id: "2", - firstName: "Alex" - },{ +test("normalizeSingleResponse with embedded objects inside embedded objects of same type", function() { + env.registry.register('adapter:comment', DS.RESTAdapter); + env.registry.register('serializer:comment', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + children: { embedded: 'always' } + } + })); + + var serializer = env.container.lookup("serializer:comment"); + var json_hash = { + comment: { + id: "1", + body: "Hello", + root: true, + children: [{ + id: "2", + body: "World", + root: false, + children: [{ id: "4", - firstName: "Erik" + body: "Another", + root: false }] - } - }; - var json; - run(function() { - json = serializer.normalizeSingleResponse(env.store, HomePlanet, json_hash, '1', 'find'); - }); - - deepEqual(json, { - "data": { - "id": "1", - "type": "home-planet", - "attributes": { - "name": "Earth" - }, - "relationships": { - "villains": { - "data": [ - { "id": "1", "type": "super-villain" }, - { "id": "3", "type": "super-villain" } - ] - }, - "reformedVillains": { - "data": [ - { "id": "2", "type": "super-villain" }, - { "id": "4", "type": "super-villain" } - ] - } - } }, - "included": [{ - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Tom" - }, - "relationships": {} - }, { - "id": "3", - "type": "super-villain", - "attributes": { - "firstName": "Yehuda" - }, - "relationships": {} - }, { - "id": "2", - "type": "super-villain", - "attributes": { - "firstName": "Alex" - }, - "relationships": {} - }, { - "id": "4", - "type": "super-villain", - "attributes": { - "firstName": "Erik" - }, - "relationships": {} - }] - }, "Primary hash was correct"); + { + id: "3", + body: "Foo", + root: false + }] + } + }; + var json; + run(function() { + json = serializer.normalizeSingleResponse(env.store, Comment, json_hash, '1', 'find'); }); - test("normalizeArrayResponse with embedded objects", function() { - env.registry.register('adapter:super-villain', DS.RESTAdapter); - env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - villains: { embedded: 'always' } + deepEqual(json, { + "data": { + "id": "1", + "type": "comment", + "attributes": { + "body": "Hello", + "root": true + }, + "relationships": { + "children": { + "data": [ + { "id": "2", "type": "comment" }, + { "id": "3", "type": "comment" } + ] + } + } + }, + "included": [{ + "id": "2", + "type": "comment", + "attributes": { + "body": "World", + "root": false + }, + "relationships": { + "children": { + "data": [ + { "id": "4", "type": "comment" } + ] + } } - })); - env.registry.register('serializer:super-villain', TestSerializer); + }, { + "id": "4", + "type": "comment", + "attributes": { + "body": "Another", + "root": false + }, + "relationships": {} + }, { + "id": "3", + "type": "comment", + "attributes": { + "body": "Foo", + "root": false + }, + "relationships": {} + }] + }, "Primary record was correct"); +}); - var serializer = env.container.lookup("serializer:home-planet"); +test("normalizeSingleResponse with embedded objects of same type, but from separate attributes", function() { + HomePlanet.reopen({ + reformedVillains: DS.hasMany('superVillain', { inverse: null }) + }); - var json_hash = { - homePlanets: [{ + env.registry.register('adapter:home-planet', DS.RESTAdapter); + env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + villains: { embedded: 'always' }, + reformedVillains: { embedded: 'always' } + } + })); + env.registry.register('serializer:super-villain', TestSerializer); + + var serializer = env.container.lookup("serializer:home-planet"); + var json_hash = { + homePlanet: { + id: "1", + name: "Earth", + villains: [{ id: "1", - name: "Umber", - villains: [{ - id: "1", - firstName: "Tom", - lastName: "Dale" - }] - }] - }; - var array; - - run(function() { - array = serializer.normalizeArrayResponse(env.store, HomePlanet, json_hash, null, 'findAll'); - }); - - deepEqual(array, { - "data": [{ - "id": "1", - "type": "home-planet", - "attributes": { - "name": "Umber" - }, - "relationships": { - "villains": { - "data": [ - { "id": "1", "type": "super-villain" } - ] - } - } + firstName: "Tom" + }, { + id: "3", + firstName: "Yehuda" }], - "included": [{ - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Tom", - "lastName": "Dale" - }, - "relationships": {} + reformedVillains: [{ + id: "2", + firstName: "Alex" + },{ + id: "4", + firstName: "Erik" }] - }); + } + }; + var json; + run(function() { + json = serializer.normalizeSingleResponse(env.store, HomePlanet, json_hash, '1', 'find'); }); - test("normalizeArrayResponse with embedded objects with custom primary key", function() { - expect(1); - env.registry.register('adapter:super-villain', DS.RESTAdapter); - env.registry.register('serializer:super-villain', TestSerializer.extend({ - primaryKey: 'villain_id' - })); - env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - villains: { embedded: 'always' } + deepEqual(json, { + "data": { + "id": "1", + "type": "home-planet", + "attributes": { + "name": "Earth" + }, + "relationships": { + "villains": { + "data": [ + { "id": "1", "type": "super-villain" }, + { "id": "3", "type": "super-villain" } + ] + }, + "reformedVillains": { + "data": [ + { "id": "2", "type": "super-villain" }, + { "id": "4", "type": "super-villain" } + ] + } } - })); - - var serializer = env.container.lookup("serializer:home-planet"); + }, + "included": [{ + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Tom" + }, + "relationships": {} + }, { + "id": "3", + "type": "super-villain", + "attributes": { + "firstName": "Yehuda" + }, + "relationships": {} + }, { + "id": "2", + "type": "super-villain", + "attributes": { + "firstName": "Alex" + }, + "relationships": {} + }, { + "id": "4", + "type": "super-villain", + "attributes": { + "firstName": "Erik" + }, + "relationships": {} + }] + }, "Primary hash was correct"); +}); - var json_hash = { - homePlanets: [{ +test("normalizeArrayResponse with embedded objects", function() { + env.registry.register('adapter:super-villain', DS.RESTAdapter); + env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + villains: { embedded: 'always' } + } + })); + env.registry.register('serializer:super-villain', TestSerializer); + + var serializer = env.container.lookup("serializer:home-planet"); + + var json_hash = { + homePlanets: [{ + id: "1", + name: "Umber", + villains: [{ id: "1", - name: "Umber", - villains: [{ - villain_id: "2", - firstName: "Alex", - lastName: "Baizeau" - }] + firstName: "Tom", + lastName: "Dale" }] - }; - var array; - - run(function() { - array = serializer.normalizeArrayResponse(env.store, HomePlanet, json_hash, null, 'findAll'); - }); + }] + }; + var array; - deepEqual(array, { - "data": [{ - "id": "1", - "type": "home-planet", - "attributes": { - "name": "Umber" - }, - "relationships": { - "villains": { - "data": [ - { "id": "2", "type": "super-villain" } - ] - } - } - }], - "included": [{ - "id": "2", - "type": "super-villain", - "attributes": { - "firstName": "Alex", - "lastName": "Baizeau" - }, - "relationships": {} - }] - }); + run(function() { + array = serializer.normalizeArrayResponse(env.store, HomePlanet, json_hash, null, 'findAll'); }); - test("normalizeArrayResponse with embedded objects with identical relationship and attribute key ", function() { - expect(1); - env.registry.register('adapter:super-villain', DS.RESTAdapter); - env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - villains: { embedded: 'always' } + deepEqual(array, { + "data": [{ + "id": "1", + "type": "home-planet", + "attributes": { + "name": "Umber" }, - //Makes the keyForRelationship and keyForAttribute collide. - keyForRelationship: function(key, type) { - return this.keyForAttribute(key, type); + "relationships": { + "villains": { + "data": [ + { "id": "1", "type": "super-villain" } + ] + } } - })); - env.registry.register('serializer:super-villain', TestSerializer); - - var serializer = env.container.lookup("serializer:home-planet"); + }], + "included": [{ + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Tom", + "lastName": "Dale" + }, + "relationships": {} + }] + }); +}); - var json_hash = { - homePlanets: [{ - id: "1", - name: "Umber", - villains: [{ - id: "1", - firstName: "Alex", - lastName: "Baizeau" - }] +test("normalizeArrayResponse with embedded objects with custom primary key", function() { + expect(1); + env.registry.register('adapter:super-villain', DS.RESTAdapter); + env.registry.register('serializer:super-villain', TestSerializer.extend({ + primaryKey: 'villain_id' + })); + env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + villains: { embedded: 'always' } + } + })); + + var serializer = env.container.lookup("serializer:home-planet"); + + var json_hash = { + homePlanets: [{ + id: "1", + name: "Umber", + villains: [{ + villain_id: "2", + firstName: "Alex", + lastName: "Baizeau" }] - }; - var array; + }] + }; + var array; - run(function() { - array = serializer.normalizeArrayResponse(env.store, HomePlanet, json_hash, null, 'findAll'); - }); + run(function() { + array = serializer.normalizeArrayResponse(env.store, HomePlanet, json_hash, null, 'findAll'); + }); - deepEqual(array, { - "data": [{ - "id": "1", - "type": "home-planet", - "attributes": { - "name": "Umber" - }, - "relationships": { - "villains": { - "data": [ - { "id": "1", "type": "super-villain" } - ] - } + deepEqual(array, { + "data": [{ + "id": "1", + "type": "home-planet", + "attributes": { + "name": "Umber" + }, + "relationships": { + "villains": { + "data": [ + { "id": "2", "type": "super-villain" } + ] } - }], - "included": [{ - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Alex", - "lastName": "Baizeau" - }, - "relationships": {} - }] - }); - }); - test("normalizeArrayResponse with embedded objects of same type as primary type", function() { - env.registry.register('adapter:comment', DS.RESTAdapter); - env.registry.register('serializer:comment', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - children: { embedded: 'always' } } - })); - - var serializer = env.container.lookup("serializer:comment"); + }], + "included": [{ + "id": "2", + "type": "super-villain", + "attributes": { + "firstName": "Alex", + "lastName": "Baizeau" + }, + "relationships": {} + }] + }); +}); - var json_hash = { - comments: [{ +test("normalizeArrayResponse with embedded objects with identical relationship and attribute key ", function() { + expect(1); + env.registry.register('adapter:super-villain', DS.RESTAdapter); + env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + villains: { embedded: 'always' } + }, + //Makes the keyForRelationship and keyForAttribute collide. + keyForRelationship: function(key, type) { + return this.keyForAttribute(key, type); + } + })); + env.registry.register('serializer:super-villain', TestSerializer); + + var serializer = env.container.lookup("serializer:home-planet"); + + var json_hash = { + homePlanets: [{ + id: "1", + name: "Umber", + villains: [{ id: "1", - body: "Hello", - root: true, - children: [{ - id: "2", - body: "World", - root: false - }, - { - id: "3", - body: "Foo", - root: false - }] + firstName: "Alex", + lastName: "Baizeau" }] - }; - var array; + }] + }; + var array; - run(function() { - array = serializer.normalizeArrayResponse(env.store, Comment, json_hash, null, 'findAll'); - }); + run(function() { + array = serializer.normalizeArrayResponse(env.store, HomePlanet, json_hash, null, 'findAll'); + }); - deepEqual(array, { - "data": [{ - "id": "1", - "type": "comment", - "attributes": { - "body": "Hello", - "root": true - }, - "relationships": { - "children": { - "data": [ - { "id": "2", "type": "comment" }, - { "id": "3", "type": "comment" } - ] - } + deepEqual(array, { + "data": [{ + "id": "1", + "type": "home-planet", + "attributes": { + "name": "Umber" + }, + "relationships": { + "villains": { + "data": [ + { "id": "1", "type": "super-villain" } + ] } - }], - "included": [{ - "id": "2", - "type": "comment", - "attributes": { - "body": "World", - "root": false - }, - "relationships": {} - }, { - "id": "3", - "type": "comment", - "attributes": { - "body": "Foo", - "root": false - }, - "relationships": {} - }] - }, "Primary array is correct"); + } + }], + "included": [{ + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Alex", + "lastName": "Baizeau" + }, + "relationships": {} + }] + }); +}); +test("normalizeArrayResponse with embedded objects of same type as primary type", function() { + env.registry.register('adapter:comment', DS.RESTAdapter); + env.registry.register('serializer:comment', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + children: { embedded: 'always' } + } + })); + + var serializer = env.container.lookup("serializer:comment"); + + var json_hash = { + comments: [{ + id: "1", + body: "Hello", + root: true, + children: [{ + id: "2", + body: "World", + root: false + }, + { + id: "3", + body: "Foo", + root: false + }] + }] + }; + var array; + + run(function() { + array = serializer.normalizeArrayResponse(env.store, Comment, json_hash, null, 'findAll'); }); - test("normalizeArrayResponse with embedded objects of same type, but from separate attributes", function() { - HomePlanet.reopen({ - reformedVillains: DS.hasMany('superVillain') - }); - - env.registry.register('adapter:home-planet', DS.RESTAdapter); - env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - villains: { embedded: 'always' }, - reformedVillains: { embedded: 'always' } + deepEqual(array, { + "data": [{ + "id": "1", + "type": "comment", + "attributes": { + "body": "Hello", + "root": true + }, + "relationships": { + "children": { + "data": [ + { "id": "2", "type": "comment" }, + { "id": "3", "type": "comment" } + ] + } } - })); - env.registry.register('serializer:super-villain', TestSerializer); + }], + "included": [{ + "id": "2", + "type": "comment", + "attributes": { + "body": "World", + "root": false + }, + "relationships": {} + }, { + "id": "3", + "type": "comment", + "attributes": { + "body": "Foo", + "root": false + }, + "relationships": {} + }] + }, "Primary array is correct"); +}); + +test("normalizeArrayResponse with embedded objects of same type, but from separate attributes", function() { + HomePlanet.reopen({ + reformedVillains: DS.hasMany('superVillain') + }); - var serializer = env.container.lookup("serializer:home-planet"); - var json_hash = { - homePlanets: [{ + env.registry.register('adapter:home-planet', DS.RESTAdapter); + env.registry.register('serializer:home-planet', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + villains: { embedded: 'always' }, + reformedVillains: { embedded: 'always' } + } + })); + env.registry.register('serializer:super-villain', TestSerializer); + + var serializer = env.container.lookup("serializer:home-planet"); + var json_hash = { + homePlanets: [{ + id: "1", + name: "Earth", + villains: [{ id: "1", - name: "Earth", - villains: [{ - id: "1", - firstName: "Tom" - },{ - id: "3", - firstName: "Yehuda" - }], - reformedVillains: [{ - id: "2", - firstName: "Alex" - },{ - id: "4", - firstName: "Erik" - }] + firstName: "Tom" },{ + id: "3", + firstName: "Yehuda" + }], + reformedVillains: [{ id: "2", - name: "Mars", - villains: [{ - id: "1", - firstName: "Tom" - },{ - id: "3", - firstName: "Yehuda" - }], - reformedVillains: [{ - id: "5", - firstName: "Peter" - },{ - id: "6", - firstName: "Trek" - }] + firstName: "Alex" + },{ + id: "4", + firstName: "Erik" + }] + },{ + id: "2", + name: "Mars", + villains: [{ + id: "1", + firstName: "Tom" + },{ + id: "3", + firstName: "Yehuda" + }], + reformedVillains: [{ + id: "5", + firstName: "Peter" + },{ + id: "6", + firstName: "Trek" }] - }; + }] + }; - var json; - run(function() { - json = serializer.normalizeArrayResponse(env.store, HomePlanet, json_hash, null, 'findAll'); - }); + var json; + run(function() { + json = serializer.normalizeArrayResponse(env.store, HomePlanet, json_hash, null, 'findAll'); + }); - deepEqual(json, { - "data": [{ - "id": "1", - "type": "home-planet", - "attributes": { - "name": "Earth" + deepEqual(json, { + "data": [{ + "id": "1", + "type": "home-planet", + "attributes": { + "name": "Earth" + }, + "relationships": { + "reformedVillains": { + "data": [ + { "id": "2", "type": "super-villain" }, + { "id": "4", "type": "super-villain" } + ] }, - "relationships": { - "reformedVillains": { - "data": [ - { "id": "2", "type": "super-villain" }, - { "id": "4", "type": "super-villain" } - ] - }, - "villains": { - "data": [ - { "id": "1", "type": "super-villain" }, - { "id": "3", "type": "super-villain" } - ] - } + "villains": { + "data": [ + { "id": "1", "type": "super-villain" }, + { "id": "3", "type": "super-villain" } + ] } - }, { - "id": "2", - "type": "home-planet", - "attributes": { - "name": "Mars" + } + }, { + "id": "2", + "type": "home-planet", + "attributes": { + "name": "Mars" + }, + "relationships": { + "reformedVillains": { + "data": [ + { "id": "5", "type": "super-villain" }, + { "id": "6", "type": "super-villain" } + ] }, - "relationships": { - "reformedVillains": { - "data": [ - { "id": "5", "type": "super-villain" }, - { "id": "6", "type": "super-villain" } - ] - }, - "villains": { - "data": [ - { "id": "1", "type": "super-villain" }, - { "id": "3", "type": "super-villain" } - ] - } + "villains": { + "data": [ + { "id": "1", "type": "super-villain" }, + { "id": "3", "type": "super-villain" } + ] } - }], - "included": [{ - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Tom" - }, - "relationships": {} - }, { - "id": "3", - "type": "super-villain", - "attributes": { - "firstName": "Yehuda" - }, - "relationships": {} - }, { - "id": "2", - "type": "super-villain", - "attributes": { - "firstName": "Alex" - }, - "relationships": {} - }, { - "id": "4", - "type": "super-villain", - "attributes": { - "firstName": "Erik" - }, - "relationships": {} - }, { - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Tom" - }, - "relationships": {} - }, { - "id": "3", - "type": "super-villain", - "attributes": { - "firstName": "Yehuda" + } + }], + "included": [{ + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Tom" + }, + "relationships": {} + }, { + "id": "3", + "type": "super-villain", + "attributes": { + "firstName": "Yehuda" + }, + "relationships": {} + }, { + "id": "2", + "type": "super-villain", + "attributes": { + "firstName": "Alex" + }, + "relationships": {} + }, { + "id": "4", + "type": "super-villain", + "attributes": { + "firstName": "Erik" + }, + "relationships": {} + }, { + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Tom" + }, + "relationships": {} + }, { + "id": "3", + "type": "super-villain", + "attributes": { + "firstName": "Yehuda" + }, + "relationships": {} + }, { + "id": "5", + "type": "super-villain", + "attributes": { + "firstName": "Peter" + }, + "relationships": {} + }, { + "id": "6", + "type": "super-villain", + "attributes": { + "firstName": "Trek" + }, + "relationships": {} + }] + }, "Primary array was correct"); +}); + +test("normalizeSingleResponse with embedded object (belongsTo relationship)", function() { + env.registry.register('adapter:super-villain', DS.RESTAdapter); + env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + secretLab: { embedded: 'always' } + } + })); + //env.registry.register('serializer:secret-lab', TestSerializer); + + var serializer = env.container.lookup("serializer:super-villain"); + + var json_hash = { + superVillain: { + id: "1", + firstName: "Tom", + lastName: "Dale", + homePlanet: "123", + evilMinions: ["1", "2", "3"], + secretLab: { + minionCapacity: 5000, + vicinity: "California, USA", + id: "101" + }, + secretWeapons: [] + } + }; + var json; + + run(function() { + json = serializer.normalizeSingleResponse(env.store, SuperVillain, json_hash, '1', 'find'); + }); + + deepEqual(json, { + "data": { + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Tom", + "lastName": "Dale" + }, + "relationships": { + "evilMinions": { + "data": [ + { "id": "1", "type": "evil-minion" }, + { "id": "2", "type": "evil-minion" }, + { "id": "3", "type": "evil-minion" } + ] }, - "relationships": {} - }, { - "id": "5", - "type": "super-villain", - "attributes": { - "firstName": "Peter" + "homePlanet": { + "data": { "id": "123", "type": "home-planet" } }, - "relationships": {} - }, { - "id": "6", - "type": "super-villain", - "attributes": { - "firstName": "Trek" + "secretLab": { + "data": { "id": "101", "type": "secret-lab" } }, - "relationships": {} - }] - }, "Primary array was correct"); - }); - - test("normalizeSingleResponse with embedded object (belongsTo relationship)", function() { - env.registry.register('adapter:super-villain', DS.RESTAdapter); - env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - secretLab: { embedded: 'always' } + "secretWeapons": { + "data": [] + } } - })); - //env.registry.register('serializer:secret-lab', TestSerializer); - - var serializer = env.container.lookup("serializer:super-villain"); + }, + "included": [{ + "id": "101", + "type": "secret-lab", + "attributes": { + "minionCapacity": 5000, + "vicinity": "California, USA" + }, + "relationships": {} + }] + }); +}); - var json_hash = { +test("normalizeSingleResponse with multiply-nested belongsTo", function() { + env.registry.register('adapter:evil-minion', DS.RESTAdapter); + env.registry.register('serializer:evil-minion', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + superVillain: { embedded: 'always' } + } + })); + env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + homePlanet: { embedded: 'always' } + } + })); + + var serializer = env.container.lookup("serializer:evil-minion"); + var json_hash = { + evilMinion: { + id: "1", + name: "Alex", superVillain: { id: "1", firstName: "Tom", lastName: "Dale", - homePlanet: "123", - evilMinions: ["1", "2", "3"], - secretLab: { - minionCapacity: 5000, - vicinity: "California, USA", - id: "101" - }, - secretWeapons: [] + evilMinions: ["1"], + homePlanet: { + id: "1", + name: "Umber", + villains: ["1"] + } } - }; - var json; + } + }; + var json; - run(function() { - json = serializer.normalizeSingleResponse(env.store, SuperVillain, json_hash, '1', 'find'); - }); + run(function() { + json = serializer.normalizeSingleResponse(env.store, EvilMinion, json_hash, '1', 'find'); + }); - deepEqual(json, { - "data": { - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Tom", - "lastName": "Dale" - }, - "relationships": { - "evilMinions": { - "data": [ - { "id": "1", "type": "evil-minion" }, - { "id": "2", "type": "evil-minion" }, - { "id": "3", "type": "evil-minion" } - ] - }, - "homePlanet": { - "data": { "id": "123", "type": "home-planet" } - }, - "secretLab": { - "data": { "id": "101", "type": "secret-lab" } - }, - "secretWeapons": { - "data": [] - } + deepEqual(json, { + "data": { + "id": "1", + "type": "evil-minion", + "attributes": { + "name": "Alex" + }, + "relationships": { + "superVillain": { + "data": { "id": "1", "type": "super-villain" } } + } + }, + "included": [{ + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Tom", + "lastName": "Dale" }, - "included": [{ - "id": "101", - "type": "secret-lab", - "attributes": { - "minionCapacity": 5000, - "vicinity": "California, USA" + "relationships": { + "evilMinions": { + "data": [ + { "id": "1", "type": "evil-minion" } + ] }, - "relationships": {} - }] - }); - }); - - test("normalizeSingleResponse with multiply-nested belongsTo", function() { - env.registry.register('adapter:evil-minion', DS.RESTAdapter); - env.registry.register('serializer:evil-minion', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - superVillain: { embedded: 'always' } - } - })); - env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - homePlanet: { embedded: 'always' } + "homePlanet": { + "data": { "id": "1", "type": "home-planet" } + } } - })); - - var serializer = env.container.lookup("serializer:evil-minion"); - var json_hash = { - evilMinion: { - id: "1", - name: "Alex", - superVillain: { - id: "1", - firstName: "Tom", - lastName: "Dale", - evilMinions: ["1"], - homePlanet: { - id: "1", - name: "Umber", - villains: ["1"] - } + }, { + "id": "1", + "type": "home-planet", + "attributes": { + "name": "Umber" + }, + "relationships": { + "villains": { + "data": [ + { "id": "1", "type": "super-villain" } + ] } } - }; - var json; + }] + }, "Primary hash was correct"); +}); - run(function() { - json = serializer.normalizeSingleResponse(env.store, EvilMinion, json_hash, '1', 'find'); - }); +test("normalizeSingleResponse with polymorphic hasMany", function() { + SuperVillain.reopen({ + secretWeapons: DS.hasMany("secretWeapon", { polymorphic: true }) + }); - deepEqual(json, { - "data": { - "id": "1", - "type": "evil-minion", - "attributes": { - "name": "Alex" - }, - "relationships": { - "superVillain": { - "data": { "id": "1", "type": "super-villain" } - } - } - }, - "included": [{ - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Tom", - "lastName": "Dale" - }, - "relationships": { - "evilMinions": { - "data": [ - { "id": "1", "type": "evil-minion" } - ] - }, - "homePlanet": { - "data": { "id": "1", "type": "home-planet" } - } - } - }, { - "id": "1", - "type": "home-planet", - "attributes": { - "name": "Umber" + env.registry.register('adapter:super-villain', DS.RESTAdapter); + env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + secretWeapons: { embedded: 'always' } + } + })); + var serializer = env.container.lookup("serializer:super-villain"); + + var json_hash = { + superVillain: { + id: "1", + firstName: "Tom", + lastName: "Dale", + secretWeapons: [ + { + id: "1", + type: "LightSaber", + name: "Tom's LightSaber", + color: "Red" }, - "relationships": { - "villains": { - "data": [ - { "id": "1", "type": "super-villain" } - ] - } + { + id: "1", + type: "SecretWeapon", + name: "The Death Star" } - }] - }, "Primary hash was correct"); - }); + ] + } + }; + var json; - test("normalizeSingleResponse with polymorphic hasMany", function() { - SuperVillain.reopen({ - secretWeapons: DS.hasMany("secretWeapon", { polymorphic: true }) - }); + run(function() { + json = serializer.normalizeSingleResponse(env.store, SuperVillain, json_hash, '1', 'findAll'); + }); - env.registry.register('adapter:super-villain', DS.RESTAdapter); - env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - secretWeapons: { embedded: 'always' } + deepEqual(json, { + "data": { + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Tom", + "lastName": "Dale" + }, + "relationships": { + "secretWeapons": { + "data": [ + { "id": "1", "type": "light-saber" }, + { "id": "1", "type": "secret-weapon" } + ] + } } - })); - var serializer = env.container.lookup("serializer:super-villain"); + }, + "included": [{ + "id": "1", + "type": "light-saber", + "attributes": { + "color": "Red", + "name": "Tom's LightSaber" + }, + "relationships": {} + }, { + "id": "1", + "type": "secret-weapon", + "attributes": { + "name": "The Death Star" + }, + "relationships": {} + }] + }, "Primary hash was correct"); +}); - var json_hash = { - superVillain: { +test("normalizeSingleResponse with polymorphic belongsTo", function() { + SuperVillain.reopen({ + secretLab: DS.belongsTo("secretLab", { polymorphic: true }) + }); + + env.registry.register('adapter:super-villain', DS.RESTAdapter); + env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + secretLab: { embedded: 'always' } + } + })); + var serializer = env.container.lookup("serializer:super-villain"); + + var json_hash = { + superVillain: { + id: "1", + firstName: "Tom", + lastName: "Dale", + secretLab: { id: "1", - firstName: "Tom", - lastName: "Dale", - secretWeapons: [ - { - id: "1", - type: "LightSaber", - name: "Tom's LightSaber", - color: "Red" - }, - { - id: "1", - type: "SecretWeapon", - name: "The Death Star" - } - ] + type: "bat-cave", + infiltrated: true } - }; - var json; + } + }; - run(function() { - json = serializer.normalizeSingleResponse(env.store, SuperVillain, json_hash, '1', 'findAll'); - }); + var json; - deepEqual(json, { - "data": { - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Tom", - "lastName": "Dale" - }, - "relationships": { - "secretWeapons": { - "data": [ - { "id": "1", "type": "light-saber" }, - { "id": "1", "type": "secret-weapon" } - ] - } - } - }, - "included": [{ - "id": "1", - "type": "light-saber", - "attributes": { - "color": "Red", - "name": "Tom's LightSaber" - }, - "relationships": {} - }, { - "id": "1", - "type": "secret-weapon", - "attributes": { - "name": "The Death Star" - }, - "relationships": {} - }] - }, "Primary hash was correct"); + run(function() { + json = serializer.normalizeSingleResponse(env.store, SuperVillain, json_hash, '1', 'find'); }); - test("normalizeSingleResponse with polymorphic belongsTo", function() { - SuperVillain.reopen({ - secretLab: DS.belongsTo("secretLab", { polymorphic: true }) - }); - - env.registry.register('adapter:super-villain', DS.RESTAdapter); - env.registry.register('serializer:super-villain', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - secretLab: { embedded: 'always' } + deepEqual(json, { + "data": { + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Tom", + "lastName": "Dale" + }, + "relationships": { + "secretLab": { + "data": { "id": "1", "type": "bat-cave" } + } } - })); - var serializer = env.container.lookup("serializer:super-villain"); + }, + "included": [{ + "id": "1", + "type": "bat-cave", + "attributes": { + "infiltrated": true + }, + "relationships": {} + }] + }, "Primary has was correct"); +}); - var json_hash = { +test("normalize with custom belongsTo primary key", function() { + env.registry.register('adapter:evil-minion', DS.RESTAdapter); + env.registry.register('serializer:evil-minion', TestSerializer.extend(DS.EmbeddedRecordsMixin, { + attrs: { + superVillain: { embedded: 'always' } + } + })); + env.registry.register('serializer:super-villain', TestSerializer.extend({ + primaryKey: 'custom' + })); + + var serializer = env.container.lookup("serializer:evil-minion"); + var json_hash = { + evil_minion: { + id: "1", + name: "Alex", superVillain: { - id: "1", + custom: "1", firstName: "Tom", - lastName: "Dale", - secretLab: { - id: "1", - type: "bat-cave", - infiltrated: true - } + lastName: "Dale" } - }; + } + }; + var json; - var json; - - run(function() { - json = serializer.normalizeSingleResponse(env.store, SuperVillain, json_hash, '1', 'find'); - }); - - deepEqual(json, { - "data": { - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Tom", - "lastName": "Dale" - }, - "relationships": { - "secretLab": { - "data": { "id": "1", "type": "bat-cave" } - } - } - }, - "included": [{ - "id": "1", - "type": "bat-cave", - "attributes": { - "infiltrated": true - }, - "relationships": {} - }] - }, "Primary has was correct"); + run(function() { + json = serializer.normalizeResponse(env.store, EvilMinion, json_hash, '1', 'find'); }); - test("normalize with custom belongsTo primary key", function() { - env.registry.register('adapter:evil-minion', DS.RESTAdapter); - env.registry.register('serializer:evil-minion', TestSerializer.extend(DS.EmbeddedRecordsMixin, { - attrs: { - superVillain: { embedded: 'always' } - } - })); - env.registry.register('serializer:super-villain', TestSerializer.extend({ - primaryKey: 'custom' - })); - - var serializer = env.container.lookup("serializer:evil-minion"); - var json_hash = { - evil_minion: { - id: "1", - name: "Alex", - superVillain: { - custom: "1", - firstName: "Tom", - lastName: "Dale" + deepEqual(json, { + "data": { + "id": "1", + "type": "evil-minion", + "attributes": { + "name": "Alex" + }, + "relationships": { + "superVillain": { + "data": { "id": "1", "type": "super-villain" } } } - }; - var json; - - run(function() { - json = serializer.normalizeResponse(env.store, EvilMinion, json_hash, '1', 'find'); - }); - - deepEqual(json, { - "data": { - "id": "1", - "type": "evil-minion", - "attributes": { - "name": "Alex" - }, - "relationships": { - "superVillain": { - "data": { "id": "1", "type": "super-villain" } - } - } + }, + "included": [{ + "id": "1", + "type": "super-villain", + "attributes": { + "firstName": "Tom", + "lastName": "Dale" }, - "included": [{ - "id": "1", - "type": "super-villain", - "attributes": { - "firstName": "Tom", - "lastName": "Dale" - }, - "relationships": {} - }] - }, "Primary hash was correct"); - }); - -} + "relationships": {} + }] + }, "Primary hash was correct"); +}); diff --git a/packages/ember-data/tests/integration/serializers/json-serializer-new-test.js b/packages/ember-data/tests/integration/serializers/json-serializer-new-test.js index 23411fbd046..aaee993ef4e 100644 --- a/packages/ember-data/tests/integration/serializers/json-serializer-new-test.js +++ b/packages/ember-data/tests/integration/serializers/json-serializer-new-test.js @@ -34,158 +34,154 @@ module("integration/serializer/json - JSONSerializer (new API)", { } }); -if (Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - - test("normalizeArrayResponse normalizes each record in the array", function() { - var postNormalizeCount = 0; - var posts = [ - { id: "1", title: "Rails is omakase" }, - { id: "2", title: "Another Post" } - ]; - - env.registry.register('serializer:post', TestSerializer.extend({ - normalize: function () { - postNormalizeCount++; - return this._super.apply(this, arguments); - } - })); - - run(function() { - env.container.lookup("serializer:post").normalizeArrayResponse(env.store, Post, posts, null, 'findAll'); - }); - equal(postNormalizeCount, 2, "two posts are normalized"); +test("normalizeArrayResponse normalizes each record in the array", function() { + var postNormalizeCount = 0; + var posts = [ + { id: "1", title: "Rails is omakase" }, + { id: "2", title: "Another Post" } + ]; + + env.registry.register('serializer:post', TestSerializer.extend({ + normalize: function () { + postNormalizeCount++; + return this._super.apply(this, arguments); + } + })); + + run(function() { + env.container.lookup("serializer:post").normalizeArrayResponse(env.store, Post, posts, null, 'findAll'); }); + equal(postNormalizeCount, 2, "two posts are normalized"); +}); - test('Serializer should respect the attrs hash when extracting records', function() { - env.registry.register("serializer:post", TestSerializer.extend({ - attrs: { - title: "title_payload_key", - comments: { key: 'my_comments' } - } - })); - - var jsonHash = { - id: "1", - title_payload_key: "Rails is omakase", - my_comments: [1, 2] - }; +test('Serializer should respect the attrs hash when extracting records', function() { + env.registry.register("serializer:post", TestSerializer.extend({ + attrs: { + title: "title_payload_key", + comments: { key: 'my_comments' } + } + })); - var post = env.container.lookup("serializer:post").normalizeSingleResponse(env.store, Post, jsonHash, '1', 'find'); + var jsonHash = { + id: "1", + title_payload_key: "Rails is omakase", + my_comments: [1, 2] + }; - equal(post.data.attributes.title, "Rails is omakase"); - deepEqual(post.data.relationships.comments.data, [{ id: "1", type: "comment" }, { id: "2", type: "comment" }]); - }); + var post = env.container.lookup("serializer:post").normalizeSingleResponse(env.store, Post, jsonHash, '1', 'find'); - test('normalizeSingleResponse should extract meta using extractMeta', function() { - env.registry.register("serializer:post", TestSerializer.extend({ - extractMeta: function(store, modelClass, payload) { - let meta = this._super(...arguments); - meta.authors.push('Tomhuda'); - return meta; - } - })); - - var jsonHash = { - id: "1", - title_payload_key: "Rails is omakase", - my_comments: [1, 2], - meta: { - authors: ['Tomster'] - } - }; - - var post = env.container.lookup("serializer:post").normalizeSingleResponse(env.store, Post, jsonHash, '1', 'find'); - - deepEqual(post.meta.authors, ['Tomster', 'Tomhuda']); - }); + equal(post.data.attributes.title, "Rails is omakase"); + deepEqual(post.data.relationships.comments.data, [{ id: "1", type: "comment" }, { id: "2", type: "comment" }]); +}); - test("Serializer should respect the primaryKey attribute when extracting records", function() { - env.registry.register('serializer:post', TestSerializer.extend({ - primaryKey: '_ID_' - })); +test('normalizeSingleResponse should extract meta using extractMeta', function() { + env.registry.register("serializer:post", TestSerializer.extend({ + extractMeta: function(store, modelClass, payload) { + let meta = this._super(...arguments); + meta.authors.push('Tomhuda'); + return meta; + } + })); + + var jsonHash = { + id: "1", + title_payload_key: "Rails is omakase", + my_comments: [1, 2], + meta: { + authors: ['Tomster'] + } + }; + + var post = env.container.lookup("serializer:post").normalizeSingleResponse(env.store, Post, jsonHash, '1', 'find'); + + deepEqual(post.meta.authors, ['Tomster', 'Tomhuda']); +}); - var jsonHash = { "_ID_": 1, title: "Rails is omakase" }; +test("Serializer should respect the primaryKey attribute when extracting records", function() { + env.registry.register('serializer:post', TestSerializer.extend({ + primaryKey: '_ID_' + })); - run(function() { - post = env.container.lookup("serializer:post").normalizeSingleResponse(env.store, Post, jsonHash, '1', 'find'); - }); + var jsonHash = { "_ID_": 1, title: "Rails is omakase" }; - equal(post.data.id, "1"); - equal(post.data.attributes.title, "Rails is omakase"); + run(function() { + post = env.container.lookup("serializer:post").normalizeSingleResponse(env.store, Post, jsonHash, '1', 'find'); }); - test("Serializer should respect keyForAttribute when extracting records", function() { - env.registry.register('serializer:post', TestSerializer.extend({ - keyForAttribute: function(key) { - return key.toUpperCase(); - } - })); - - var jsonHash = { id: 1, TITLE: 'Rails is omakase' }; + equal(post.data.id, "1"); + equal(post.data.attributes.title, "Rails is omakase"); +}); - post = env.container.lookup("serializer:post").normalize(Post, jsonHash); +test("Serializer should respect keyForAttribute when extracting records", function() { + env.registry.register('serializer:post', TestSerializer.extend({ + keyForAttribute: function(key) { + return key.toUpperCase(); + } + })); - equal(post.data.id, "1"); - equal(post.data.attributes.title, "Rails is omakase"); - }); + var jsonHash = { id: 1, TITLE: 'Rails is omakase' }; - test("Serializer should respect keyForRelationship when extracting records", function() { - env.registry.register('serializer:post', TestSerializer.extend({ - keyForRelationship: function(key, type) { - return key.toUpperCase(); - } - })); + post = env.container.lookup("serializer:post").normalize(Post, jsonHash); - var jsonHash = { id: 1, title: 'Rails is omakase', COMMENTS: ['1'] }; + equal(post.data.id, "1"); + equal(post.data.attributes.title, "Rails is omakase"); +}); - post = env.container.lookup("serializer:post").normalize(Post, jsonHash); +test("Serializer should respect keyForRelationship when extracting records", function() { + env.registry.register('serializer:post', TestSerializer.extend({ + keyForRelationship: function(key, type) { + return key.toUpperCase(); + } + })); - deepEqual(post.data.relationships.comments.data, [{ id: "1", type: "comment" }]); - }); + var jsonHash = { id: 1, title: 'Rails is omakase', COMMENTS: ['1'] }; - test("Calling normalize should normalize the payload (only the passed keys)", function () { - expect(1); - var Person = DS.Model.extend({ - posts: DS.hasMany('post') - }); - env.registry.register('serializer:post', TestSerializer.extend({ - attrs: { - notInHash: 'aCustomAttrNotInHash', - inHash: 'aCustomAttrInHash' - } - })); + post = env.container.lookup("serializer:post").normalize(Post, jsonHash); - env.registry.register('model:person', Person); + deepEqual(post.data.relationships.comments.data, [{ id: "1", type: "comment" }]); +}); - Post.reopen({ - content: DS.attr('string'), - author: DS.belongsTo('person'), - notInHash: DS.attr('string'), - inHash: DS.attr('string') - }); +test("Calling normalize should normalize the payload (only the passed keys)", function () { + expect(1); + var Person = DS.Model.extend({ + posts: DS.hasMany('post') + }); + env.registry.register('serializer:post', TestSerializer.extend({ + attrs: { + notInHash: 'aCustomAttrNotInHash', + inHash: 'aCustomAttrInHash' + } + })); + + env.registry.register('model:person', Person); + + Post.reopen({ + content: DS.attr('string'), + author: DS.belongsTo('person'), + notInHash: DS.attr('string'), + inHash: DS.attr('string') + }); - var normalizedPayload = env.container.lookup("serializer:post").normalize(Post, { - id: '1', - title: 'Ember rocks', - author: 1, - aCustomAttrInHash: 'blah' - }); + var normalizedPayload = env.container.lookup("serializer:post").normalize(Post, { + id: '1', + title: 'Ember rocks', + author: 1, + aCustomAttrInHash: 'blah' + }); - deepEqual(normalizedPayload, { - "data": { - "id": "1", - "type": "post", - "attributes": { - "inHash": "blah", - "title": "Ember rocks" - }, - "relationships": { - "author": { - "data": { "id": "1", "type": "person" } - } + deepEqual(normalizedPayload, { + "data": { + "id": "1", + "type": "post", + "attributes": { + "inHash": "blah", + "title": "Ember rocks" + }, + "relationships": { + "author": { + "data": { "id": "1", "type": "person" } } } - }); + } }); - -} +}); diff --git a/packages/ember-data/tests/integration/serializers/rest-serializer-new-test.js b/packages/ember-data/tests/integration/serializers/rest-serializer-new-test.js index 866322b4c25..6c38fb5e8b1 100644 --- a/packages/ember-data/tests/integration/serializers/rest-serializer-new-test.js +++ b/packages/ember-data/tests/integration/serializers/rest-serializer-new-test.js @@ -56,359 +56,355 @@ module("integration/serializer/rest - RESTSerializer (new API)", { } }); -if (Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - - test("normalizeSingleResponse with custom modelNameFromPayloadKey", function() { - expect(1); - - env.restNewSerializer.modelNameFromPayloadKey = function(root) { - var camelized = Ember.String.camelize(root); - return Ember.String.singularize(camelized); - }; - - var jsonHash = { - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }], - super_villains: [{ id: "1", firstName: "Tom", lastName: "Dale", homePlanet: "1" }] - }; - var array; - - run(function() { - array = env.container.lookup("serializer:application").normalizeSingleResponse(env.store, HomePlanet, jsonHash, '1', 'find'); - }); +test("normalizeSingleResponse with custom modelNameFromPayloadKey", function() { + expect(1); + + env.restNewSerializer.modelNameFromPayloadKey = function(root) { + var camelized = Ember.String.camelize(root); + return Ember.String.singularize(camelized); + }; + + var jsonHash = { + home_planets: [{ id: "1", name: "Umber", superVillains: [1] }], + super_villains: [{ id: "1", firstName: "Tom", lastName: "Dale", homePlanet: "1" }] + }; + var array; + + run(function() { + array = env.container.lookup("serializer:application").normalizeSingleResponse(env.store, HomePlanet, jsonHash, '1', 'find'); + }); - deepEqual(array, { - data: { - id: '1', - type: 'home-planet', - attributes: { - name: 'Umber' - }, - relationships: { - superVillains: { - data: [{ id: '1', type: 'super-villain' }] - } + deepEqual(array, { + data: { + id: '1', + type: 'home-planet', + attributes: { + name: 'Umber' + }, + relationships: { + superVillains: { + data: [{ id: '1', type: 'super-villain' }] } + } + }, + included: [{ + id: '1', + type: 'super-villain', + attributes: { + firstName: 'Tom', + lastName: 'Dale' }, - included: [{ - id: '1', - type: 'super-villain', - attributes: { - firstName: 'Tom', - lastName: 'Dale' - }, - relationships: { - homePlanet: { - data: { id: '1', type: 'home-planet' } - } + relationships: { + homePlanet: { + data: { id: '1', type: 'home-planet' } } - }] - }); + } + }] }); +}); - test('normalizeArrayResponse should extract meta using extractMeta', function() { - env.registry.register("serializer:home-planet", TestSerializer.extend({ - extractMeta: function(store, modelClass, payload) { - let meta = this._super(...arguments); - meta.authors.push('Tomhuda'); - return meta; - } - })); +test('normalizeArrayResponse should extract meta using extractMeta', function() { + env.registry.register("serializer:home-planet", TestSerializer.extend({ + extractMeta: function(store, modelClass, payload) { + let meta = this._super(...arguments); + meta.authors.push('Tomhuda'); + return meta; + } + })); - var jsonHash = { - meta: { authors: ['Tomster'] }, - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }] - }; + var jsonHash = { + meta: { authors: ['Tomster'] }, + home_planets: [{ id: "1", name: "Umber", superVillains: [1] }] + }; - var json = env.container.lookup("serializer:home-planet").normalizeArrayResponse(env.store, HomePlanet, jsonHash, null, 'findAll'); + var json = env.container.lookup("serializer:home-planet").normalizeArrayResponse(env.store, HomePlanet, jsonHash, null, 'findAll'); - deepEqual(json.meta.authors, ['Tomster', 'Tomhuda']); - }); + deepEqual(json.meta.authors, ['Tomster', 'Tomhuda']); +}); - test("normalizeArrayResponse warning with custom modelNameFromPayloadKey", function() { - var homePlanets; - env.restNewSerializer.modelNameFromPayloadKey = function(root) { - //return some garbage that won"t resolve in the container - return "garbage"; - }; - - var jsonHash = { - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }] - }; - - warns(function() { - env.restNewSerializer.normalizeArrayResponse(env.store, HomePlanet, jsonHash, null, 'findAll'); - }, /Encountered "home_planets" in payload, but no model was found for model name "garbage"/); - - // should not warn if a model is found. - env.restNewSerializer.modelNameFromPayloadKey = function(root) { - return Ember.String.camelize(Ember.String.singularize(root)); - }; - - jsonHash = { - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }] - }; - - noWarns(function() { - run(function() { - homePlanets = env.restNewSerializer.normalizeArrayResponse(env.store, HomePlanet, jsonHash, null, 'findAll'); - }); - }); +test("normalizeArrayResponse warning with custom modelNameFromPayloadKey", function() { + var homePlanets; + env.restNewSerializer.modelNameFromPayloadKey = function(root) { + //return some garbage that won"t resolve in the container + return "garbage"; + }; - equal(homePlanets.data.length, 1); - equal(homePlanets.data[0].attributes.name, "Umber"); - deepEqual(homePlanets.data[0].relationships.superVillains.data, [{ id: '1', type: 'super-villain' }]); - }); + var jsonHash = { + home_planets: [{ id: "1", name: "Umber", superVillains: [1] }] + }; - test("normalizeSingleResponse warning with custom modelNameFromPayloadKey", function() { - var homePlanet; - var oldModelNameFromPayloadKey = env.restNewSerializer.modelNameFromPayloadKey; - env.restNewSerializer.modelNameFromPayloadKey = function(root) { - //return some garbage that won"t resolve in the container - return "garbage"; - }; - - var jsonHash = { - home_planet: { id: "1", name: "Umber", superVillains: [1] } - }; - - warns(Ember.run.bind(null, function() { - run(function() { - env.restNewSerializer.normalizeSingleResponse(env.store, HomePlanet, jsonHash, '1', 'find'); - }); - }), /Encountered "home_planet" in payload, but no model was found for model name "garbage"/); - - // should not warn if a model is found. - env.restNewSerializer.modelNameFromPayloadKey = oldModelNameFromPayloadKey; - jsonHash = { - home_planet: { id: "1", name: "Umber", superVillains: [1] } - }; - - noWarns(function() { - run(function() { - homePlanet = env.restNewSerializer.normalizeSingleResponse(env.store, HomePlanet, jsonHash, 1, 'find'); - }); - }); + warns(function() { + env.restNewSerializer.normalizeArrayResponse(env.store, HomePlanet, jsonHash, null, 'findAll'); + }, /Encountered "home_planets" in payload, but no model was found for model name "garbage"/); - equal(homePlanet.data.attributes.name, "Umber"); - deepEqual(homePlanet.data.relationships.superVillains.data, [{ id: '1', type: 'super-villain' }]); - }); + // should not warn if a model is found. + env.restNewSerializer.modelNameFromPayloadKey = function(root) { + return Ember.String.camelize(Ember.String.singularize(root)); + }; - test("normalizeResponse can load secondary records of the same type without affecting the query count", function() { - var jsonHash = { - comments: [{ id: "1", body: "Parent Comment", root: true, children: [2, 3] }], - _comments: [ - { id: "2", body: "Child Comment 1", root: false }, - { id: "3", body: "Child Comment 2", root: false } - ] - }; - var array; + jsonHash = { + home_planets: [{ id: "1", name: "Umber", superVillains: [1] }] + }; + noWarns(function() { run(function() { - array = env.restNewSerializer.normalizeResponse(env.store, Comment, jsonHash, '1', 'find'); + homePlanets = env.restNewSerializer.normalizeArrayResponse(env.store, HomePlanet, jsonHash, null, 'findAll'); }); + }); - deepEqual(array, { - "data": { - "id": "1", - "type": "comment", - "attributes": { - "body": "Parent Comment", - "root": true - }, - "relationships": { - "children": { - "data": [ - { "id": "2", "type": "comment" }, - { "id": "3", "type": "comment" } - ] - } - } - }, - "included": [{ - "id": "2", - "type": "comment", - "attributes": { - "body": "Child Comment 1", - "root": false - }, - "relationships": {} - }, { - "id": "3", - "type": "comment", - "attributes": { - "body": "Child Comment 2", - "root": false - }, - "relationships": {} - }] - }); + equal(homePlanets.data.length, 1); + equal(homePlanets.data[0].attributes.name, "Umber"); + deepEqual(homePlanets.data[0].relationships.superVillains.data, [{ id: '1', type: 'super-villain' }]); +}); - // normalizeResponse does not push records to the store - //equal(env.store.recordForId("comment", "2").get("body"), "Child Comment 1", "Secondary records are in the store"); - //equal(env.store.recordForId("comment", "3").get("body"), "Child Comment 2", "Secondary records are in the store"); - }); +test("normalizeSingleResponse warning with custom modelNameFromPayloadKey", function() { + var homePlanet; + var oldModelNameFromPayloadKey = env.restNewSerializer.modelNameFromPayloadKey; + env.restNewSerializer.modelNameFromPayloadKey = function(root) { + //return some garbage that won"t resolve in the container + return "garbage"; + }; - test("normalizeSingleResponse loads secondary records with correct serializer", function() { - var superVillainNormalizeCount = 0; + var jsonHash = { + home_planet: { id: "1", name: "Umber", superVillains: [1] } + }; - env.registry.register('serializer:super-villain', TestSerializer.extend({ - normalize: function() { - superVillainNormalizeCount++; - return this._super.apply(this, arguments); - } - })); + warns(Ember.run.bind(null, function() { + run(function() { + env.restNewSerializer.normalizeSingleResponse(env.store, HomePlanet, jsonHash, '1', 'find'); + }); + }), /Encountered "home_planet" in payload, but no model was found for model name "garbage"/); - var jsonHash = { - evilMinion: { id: "1", name: "Tom Dale", superVillain: 1 }, - superVillains: [{ id: "1", firstName: "Yehuda", lastName: "Katz", homePlanet: "1" }] - }; + // should not warn if a model is found. + env.restNewSerializer.modelNameFromPayloadKey = oldModelNameFromPayloadKey; + jsonHash = { + home_planet: { id: "1", name: "Umber", superVillains: [1] } + }; + noWarns(function() { run(function() { - env.restNewSerializer.normalizeSingleResponse(env.store, EvilMinion, jsonHash, '1', 'find'); + homePlanet = env.restNewSerializer.normalizeSingleResponse(env.store, HomePlanet, jsonHash, 1, 'find'); }); + }); - equal(superVillainNormalizeCount, 1, "superVillain is normalized once"); + equal(homePlanet.data.attributes.name, "Umber"); + deepEqual(homePlanet.data.relationships.superVillains.data, [{ id: '1', type: 'super-villain' }]); +}); + +test("normalizeResponse can load secondary records of the same type without affecting the query count", function() { + var jsonHash = { + comments: [{ id: "1", body: "Parent Comment", root: true, children: [2, 3] }], + _comments: [ + { id: "2", body: "Child Comment 1", root: false }, + { id: "3", body: "Child Comment 2", root: false } + ] + }; + var array; + + run(function() { + array = env.restNewSerializer.normalizeResponse(env.store, Comment, jsonHash, '1', 'find'); }); - test("normalizeSingleResponse returns null if payload contains null", function() { - expect(1); + deepEqual(array, { + "data": { + "id": "1", + "type": "comment", + "attributes": { + "body": "Parent Comment", + "root": true + }, + "relationships": { + "children": { + "data": [ + { "id": "2", "type": "comment" }, + { "id": "3", "type": "comment" } + ] + } + } + }, + "included": [{ + "id": "2", + "type": "comment", + "attributes": { + "body": "Child Comment 1", + "root": false + }, + "relationships": {} + }, { + "id": "3", + "type": "comment", + "attributes": { + "body": "Child Comment 2", + "root": false + }, + "relationships": {} + }] + }); - var jsonHash = { - evilMinion: null - }; - var value; + // normalizeResponse does not push records to the store + //equal(env.store.recordForId("comment", "2").get("body"), "Child Comment 1", "Secondary records are in the store"); + //equal(env.store.recordForId("comment", "3").get("body"), "Child Comment 2", "Secondary records are in the store"); +}); - run(function() { - value = env.restNewSerializer.normalizeSingleResponse(env.store, EvilMinion, jsonHash, null, 'find'); - }); +test("normalizeSingleResponse loads secondary records with correct serializer", function() { + var superVillainNormalizeCount = 0; - deepEqual(value, { data: null, included: [] }, "returned value is null"); - }); + env.registry.register('serializer:super-villain', TestSerializer.extend({ + normalize: function() { + superVillainNormalizeCount++; + return this._super.apply(this, arguments); + } + })); - test("normalizeArrayResponse loads secondary records with correct serializer", function() { - var superVillainNormalizeCount = 0; + var jsonHash = { + evilMinion: { id: "1", name: "Tom Dale", superVillain: 1 }, + superVillains: [{ id: "1", firstName: "Yehuda", lastName: "Katz", homePlanet: "1" }] + }; - env.registry.register('serializer:super-villain', TestSerializer.extend({ - normalize: function() { - superVillainNormalizeCount++; - return this._super.apply(this, arguments); - } - })); + run(function() { + env.restNewSerializer.normalizeSingleResponse(env.store, EvilMinion, jsonHash, '1', 'find'); + }); + + equal(superVillainNormalizeCount, 1, "superVillain is normalized once"); +}); - var jsonHash = { - evilMinions: [{ id: "1", name: "Tom Dale", superVillain: 1 }], - superVillains: [{ id: "1", firstName: "Yehuda", lastName: "Katz", homePlanet: "1" }] - }; +test("normalizeSingleResponse returns null if payload contains null", function() { + expect(1); - run(function() { - env.restNewSerializer.normalizeArrayResponse(env.store, EvilMinion, jsonHash, null, 'findAll'); - }); + var jsonHash = { + evilMinion: null + }; + var value; - equal(superVillainNormalizeCount, 1, "superVillain is normalized once"); + run(function() { + value = env.restNewSerializer.normalizeSingleResponse(env.store, EvilMinion, jsonHash, null, 'find'); }); - test('normalizeHash normalizes specific parts of the payload', function() { - env.registry.register('serializer:application', TestSerializer.extend({ - normalizeHash: { - homePlanets: function(hash) { - hash.id = hash._id; - delete hash._id; - return hash; - } - } - })); + deepEqual(value, { data: null, included: [] }, "returned value is null"); +}); - var jsonHash = { - homePlanets: [{ _id: "1", name: "Umber", superVillains: [1] }] - }; - var array; +test("normalizeArrayResponse loads secondary records with correct serializer", function() { + var superVillainNormalizeCount = 0; - run(function() { - array = env.restNewSerializer.normalizeArrayResponse(env.store, HomePlanet, jsonHash, null, 'findAll'); - }); + env.registry.register('serializer:super-villain', TestSerializer.extend({ + normalize: function() { + superVillainNormalizeCount++; + return this._super.apply(this, arguments); + } + })); - deepEqual(array, { - "data": [{ - "id": "1", - "type": "home-planet", - "attributes": { - "name": "Umber" - }, - "relationships": { - "superVillains": { - "data": [ - { "id": "1", "type": "super-villain" } - ] - } - } - }], - "included": [] - }); + var jsonHash = { + evilMinions: [{ id: "1", name: "Tom Dale", superVillain: 1 }], + superVillains: [{ id: "1", firstName: "Yehuda", lastName: "Katz", homePlanet: "1" }] + }; + run(function() { + env.restNewSerializer.normalizeArrayResponse(env.store, EvilMinion, jsonHash, null, 'findAll'); }); - test('normalizeHash works with transforms', function() { - env.registry.register('serializer:application', TestSerializer.extend({ - normalizeHash: { - evilMinions: function(hash) { - hash.condition = hash._condition; - delete hash._condition; - return hash; - } + equal(superVillainNormalizeCount, 1, "superVillain is normalized once"); +}); + +test('normalizeHash normalizes specific parts of the payload', function() { + env.registry.register('serializer:application', TestSerializer.extend({ + normalizeHash: { + homePlanets: function(hash) { + hash.id = hash._id; + delete hash._id; + return hash; } - })); - - env.registry.register('transform:condition', DS.Transform.extend({ - deserialize: function(serialized) { - if (serialized === 1) { - return "healing"; - } else { - return "unknown"; - } + } + })); + + var jsonHash = { + homePlanets: [{ _id: "1", name: "Umber", superVillains: [1] }] + }; + var array; + + run(function() { + array = env.restNewSerializer.normalizeArrayResponse(env.store, HomePlanet, jsonHash, null, 'findAll'); + }); + + deepEqual(array, { + "data": [{ + "id": "1", + "type": "home-planet", + "attributes": { + "name": "Umber" }, - serialize: function(deserialized) { - if (deserialized === "healing") { - return 1; - } else { - return 2; + "relationships": { + "superVillains": { + "data": [ + { "id": "1", "type": "super-villain" } + ] } } - })); - - EvilMinion.reopen({ condition: DS.attr('condition') }); + }], + "included": [] + }); - var jsonHash = { - evilMinions: [{ id: "1", name: "Tom Dale", superVillain: 1, _condition: 1 }] - }; - var array; +}); - run(function() { - array = env.restNewSerializer.normalizeArrayResponse(env.store, EvilMinion, jsonHash, null, 'findAll'); - }); +test('normalizeHash works with transforms', function() { + env.registry.register('serializer:application', TestSerializer.extend({ + normalizeHash: { + evilMinions: function(hash) { + hash.condition = hash._condition; + delete hash._condition; + return hash; + } + } + })); + + env.registry.register('transform:condition', DS.Transform.extend({ + deserialize: function(serialized) { + if (serialized === 1) { + return "healing"; + } else { + return "unknown"; + } + }, + serialize: function(deserialized) { + if (deserialized === "healing") { + return 1; + } else { + return 2; + } + } + })); - equal(array.data[0].attributes.condition, "healing"); - }); + EvilMinion.reopen({ condition: DS.attr('condition') }); - test('normalize should allow for different levels of normalization', function() { - env.registry.register('serializer:application', TestSerializer.extend({ - attrs: { - superVillain: 'is_super_villain' - }, - keyForAttribute: function(attr) { - return Ember.String.decamelize(attr); - } - })); + var jsonHash = { + evilMinions: [{ id: "1", name: "Tom Dale", superVillain: 1, _condition: 1 }] + }; + var array; - var jsonHash = { - evilMinions: [{ id: "1", name: "Tom Dale", is_super_villain: 1 }] - }; - var array; + run(function() { + array = env.restNewSerializer.normalizeArrayResponse(env.store, EvilMinion, jsonHash, null, 'findAll'); + }); - run(function() { - array = env.restNewSerializer.normalizeArrayResponse(env.store, EvilMinion, jsonHash, null, 'findAll'); - }); + equal(array.data[0].attributes.condition, "healing"); +}); - equal(array.data[0].relationships.superVillain.data.id, 1); +test('normalize should allow for different levels of normalization', function() { + env.registry.register('serializer:application', TestSerializer.extend({ + attrs: { + superVillain: 'is_super_villain' + }, + keyForAttribute: function(attr) { + return Ember.String.decamelize(attr); + } + })); + + var jsonHash = { + evilMinions: [{ id: "1", name: "Tom Dale", is_super_villain: 1 }] + }; + var array; + + run(function() { + array = env.restNewSerializer.normalizeArrayResponse(env.store, EvilMinion, jsonHash, null, 'findAll'); }); -} + equal(array.data[0].relationships.superVillain.data.id, 1); +}); diff --git a/packages/ember-data/tests/integration/serializers/rest-serializer-test.js b/packages/ember-data/tests/integration/serializers/rest-serializer-test.js index 0870b6514ae..595ae16b625 100644 --- a/packages/ember-data/tests/integration/serializers/rest-serializer-test.js +++ b/packages/ember-data/tests/integration/serializers/rest-serializer-test.js @@ -57,74 +57,6 @@ test("modelNameFromPayloadKey returns always same modelName even for uncountable equal(env.restSerializer.modelNameFromPayloadKey('multi-words'), expectedModelName); }); -if (!Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - test("extractArray with custom modelNameFromPayloadKey", function() { - env.restSerializer.modelNameFromPayloadKey = function(root) { - var camelized = Ember.String.camelize(root); - return Ember.String.singularize(camelized); - }; - - var jsonHash = { - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }], - super_villains: [{ id: "1", firstName: "Tom", lastName: "Dale", homePlanet: "1" }] - }; - var array; - - run(function() { - array = env.restSerializer.extractArray(env.store, HomePlanet, jsonHash); - }); - - deepEqual(array, [{ - id: "1", - name: "Umber", - superVillains: [1] - }]); - - run(function() { - env.store.find('super-villain', 1).then(function(minion) { - equal(minion.get('firstName'), "Tom"); - }); - }); - }); -} - -if (!Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - test("extractArray warning with custom modelNameFromPayloadKey", function() { - var homePlanets; - env.restSerializer.modelNameFromPayloadKey = function(root) { - //return some garbage that won"t resolve in the container - return "garbage"; - }; - - var jsonHash = { - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }] - }; - - warns(function() { - env.restSerializer.extractArray(env.store, HomePlanet, jsonHash); - }, /Encountered "home_planets" in payload, but no model was found for model name "garbage"/); - - // should not warn if a model is found. - env.restSerializer.modelNameFromPayloadKey = function(root) { - return Ember.String.camelize(Ember.String.singularize(root)); - }; - - jsonHash = { - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }] - }; - - noWarns(function() { - run(function() { - homePlanets = Ember.A(env.restSerializer.extractArray(env.store, HomePlanet, jsonHash)); - }); - }); - - equal(get(homePlanets, "length"), 1); - equal(get(homePlanets, "firstObject.name"), "Umber"); - deepEqual(get(homePlanets, "firstObject.superVillains"), [1]); - }); -} - test("extractSingle warning with custom modelNameFromPayloadKey", function() { var homePlanet; env.restSerializer.modelNameFromPayloadKey = function(root) { @@ -161,120 +93,6 @@ test("extractSingle warning with custom modelNameFromPayloadKey", function() { deepEqual(get(homePlanet, "superVillains"), [1]); }); -if (!Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - test("pushPayload - single record payload - warning with custom modelNameFromPayloadKey", function() { - var homePlanet; - var HomePlanetRestSerializer = DS.RESTSerializer.extend({ - modelNameFromPayloadKey: function(root) { - //return some garbage that won"t resolve in the container - if (root === "home_planet") { - return "garbage"; - } else { - return Ember.String.singularize(Ember.String.camelize(root)); - } - } - }); - - env.registry.register("serializer:home-planet", HomePlanetRestSerializer); - - var jsonHash = { - home_planet: { id: "1", name: "Umber", superVillains: [1] }, - super_villains: [{ id: "1", firstName: "Stanley" }] - }; - - warns(function() { - run(function() { - env.store.pushPayload('home-planet', jsonHash); - }); - }, /Encountered "home_planet" in payload, but no model was found for model name "garbage"/); - - // assert non-warned records get pushed into store correctly - var superVillain = env.store.peekRecord('super-villain', "1"); - equal(get(superVillain, "firstName"), "Stanley"); - - // Serializers are singletons, so that"s why we use the store which - // looks at the container to look it up - env.store.serializerFor('home-planet').reopen({ - modelNameFromPayloadKey: function(root) { - // should not warn if a model is found. - return Ember.String.camelize(Ember.String.singularize(root)); - } - }); - - jsonHash = { - home_planet: { id: "1", name: "Umber", superVillains: [1] }, - super_villains: [{ id: "1", firstName: "Stanley" }] - }; - - noWarns(function() { - run(function() { - env.store.pushPayload('home-planet', jsonHash); - homePlanet = env.store.peekRecord('home-planet', "1"); - }); - }); - - equal(get(homePlanet, "name"), "Umber"); - deepEqual(get(homePlanet, "superVillains.firstObject.firstName"), "Stanley"); - }); -} - -if (!Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - test("pushPayload - multiple record payload (extractArray) - warning with custom modelNameFromPayloadKey", function() { - var homePlanet; - var HomePlanetRestSerializer = DS.RESTSerializer.extend({ - modelNameFromPayloadKey: function(root) { - //return some garbage that won"t resolve in the container - if (root === "home_planets") { - return "garbage"; - } else { - return Ember.String.singularize(Ember.String.camelize(root)); - } - } - }); - - env.registry.register("serializer:home-planet", HomePlanetRestSerializer); - - var jsonHash = { - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }], - super_villains: [{ id: "1", firstName: "Stanley" }] - }; - - warns(function() { - run(function() { - env.store.pushPayload('home-planet', jsonHash); - }); - }, /Encountered "home_planets" in payload, but no model was found for model name "garbage"/); - - // assert non-warned records get pushed into store correctly - var superVillain = env.store.peekRecord('super-villain', "1"); - equal(get(superVillain, "firstName"), "Stanley"); - - // Serializers are singletons, so that"s why we use the store which - // looks at the container to look it up - env.store.serializerFor('home-planet').reopen({ - modelNameFromPayloadKey: function(root) { - // should not warn if a model is found. - return Ember.String.camelize(Ember.String.singularize(root)); - } - }); - - jsonHash = { - home_planets: [{ id: "1", name: "Umber", superVillains: [1] }], - super_villains: [{ id: "1", firstName: "Stanley" }] - }; - - noWarns(function() { - run(function() { - env.store.pushPayload('home-planet', jsonHash); - homePlanet = env.store.peekRecord('home-planet', "1"); - }); - }); - - equal(get(homePlanet, "name"), "Umber"); - deepEqual(get(homePlanet, "superVillains.firstObject.firstName"), "Stanley"); - }); -} - test("serialize polymorphicType", function() { var tom, ray; run(function() { @@ -339,35 +157,6 @@ test("serialize polymorphic when associated object is null", function() { deepEqual(json["evilMinionType"], null); }); -if (!Ember.FEATURES.isEnabled('ds-new-serializer-api')) { - test("extractArray can load secondary records of the same type without affecting the query count", function() { - var jsonHash = { - comments: [{ id: "1", body: "Parent Comment", root: true, children: [2, 3] }], - _comments: [ - { id: "2", body: "Child Comment 1", root: false }, - { id: "3", body: "Child Comment 2", root: false } - ] - }; - var array; - - run(function() { - array = env.restSerializer.extractArray(env.store, Comment, jsonHash); - }); - - deepEqual(array, [{ - "id": "1", - "body": "Parent Comment", - "root": true, - "children": [2, 3] - }]); - - equal(array.length, 1, "The query count is unaffected"); - - equal(env.store.recordForId('comment', "2").get("body"), "Child Comment 1", "Secondary records are in the store"); - equal(env.store.recordForId('comment', "3").get("body"), "Child Comment 2", "Secondary records are in the store"); - }); -} - test("extractSingle loads secondary records with correct serializer", function() { var superVillainNormalizeCount = 0;