From 814d32c8ecf41e16bee9a65805f3b47adff0cf07 Mon Sep 17 00:00:00 2001 From: Gaurav Munjal Date: Thu, 3 Oct 2019 21:54:52 -0400 Subject: [PATCH 1/2] [CHORE] pushPayload test for serializer encapsulation side quest --- .../tests/integration/push-payload-test.js | 115 ++++++++++++++++++ .../store/addon/-private/system/core-store.ts | 4 + 2 files changed, 119 insertions(+) create mode 100644 packages/-serializer-encapsulation-test-app/tests/integration/push-payload-test.js diff --git a/packages/-serializer-encapsulation-test-app/tests/integration/push-payload-test.js b/packages/-serializer-encapsulation-test-app/tests/integration/push-payload-test.js new file mode 100644 index 00000000000..22e3f225ed6 --- /dev/null +++ b/packages/-serializer-encapsulation-test-app/tests/integration/push-payload-test.js @@ -0,0 +1,115 @@ +import Model, { attr } from '@ember-data/model'; +import Store from 'serializer-encapsulation-test-app/services/store'; +import EmberObject from '@ember/object'; +import { setupTest } from 'ember-qunit'; +import { module, test, skip } from 'qunit'; +import { DEBUG } from '@glimmer/env'; + +// we fail smoke test if we import this +function testInDebug() { + if (DEBUG) { + test(...arguments); + } else { + skip(...arguments); + } +} + +class Person extends Model { + @attr + firstName; + + @attr + lastName; + + // override to not call serialize() + toJSON() { + const { id, firstName, lastName } = this; + return { + id, + type: this.constructor.modelName, + attributes: { + firstName, + lastName, + }, + }; + } +} + +module('integration/push-payload - pushPayload method forwards to Serializer#pushPayload', function(hooks) { + setupTest(hooks); + + hooks.beforeEach(function(assert) { + this.owner.register('service:store', Store); + this.owner.register('model:person', Person); + }); + + test('Store#pushPayload calls Serializer#pushPayload', async function(assert) { + let pushPayloadCalled = 0; + + class TestMinimumSerializer extends EmberObject { + pushPayload(store, rawPayload) { + pushPayloadCalled++; + + assert.deepEqual(rawPayload, { + id: '1', + type: 'person', + attributes: { + firstName: 'John', + lastName: 'Smith', + }, + }); + + store.push({ + data: rawPayload, + }); + } + } + this.owner.register('serializer:application', TestMinimumSerializer); + + const store = this.owner.lookup('service:store'); + + store.pushPayload('person', { + id: '1', + type: 'person', + attributes: { + firstName: 'John', + lastName: 'Smith', + }, + }); + let person = store.peekRecord('person', '1'); + + assert.equal(pushPayloadCalled, 1, 'pushPayload called once'); + assert.deepEqual( + person.toJSON(), + { + id: '1', + type: 'person', + attributes: { + firstName: 'John', + lastName: 'Smith', + }, + }, + 'normalized payload is correct' + ); + }); + + testInDebug('Store#pushPayload throws an error if Serializer#pushPayload is not implemented', async function(assert) { + class TestMinimumSerializer extends EmberObject {} + this.owner.register('serializer:application', TestMinimumSerializer); + + const store = this.owner.lookup('service:store'); + + assert.expectAssertion(() => { + store.pushPayload('person', { + data: { + id: '1', + type: 'person', + attributes: { + firstName: 'John', + lastName: 'Smith', + }, + }, + }); + }, /You must define a pushPayload method in your serializer in order to call store.pushPayload/); + }); +}); diff --git a/packages/store/addon/-private/system/core-store.ts b/packages/store/addon/-private/system/core-store.ts index c0c4ffdc6c7..83fbb6666ec 100644 --- a/packages/store/addon/-private/system/core-store.ts +++ b/packages/store/addon/-private/system/core-store.ts @@ -3001,6 +3001,10 @@ abstract class CoreStore extends Service { let normalizedModelName = normalizeModelName(modelName); serializer = this.serializerFor(normalizedModelName); } + assert( + `You must define a pushPayload method in your serializer in order to call store.pushPayload`, + serializer.pushPayload + ); serializer.pushPayload(this, payload); } From 22a35bd45a4c3ecd610467158bbabc73bef63770 Mon Sep 17 00:00:00 2001 From: Gaurav Munjal Date: Thu, 3 Oct 2019 22:00:08 -0400 Subject: [PATCH 2/2] do not duplicate test in debug --- .../tests/helpers/test-in-debug.js | 10 ++++++++++ .../tests/integration/normalize-test.js | 13 ++----------- .../tests/integration/push-payload-test.js | 13 ++----------- 3 files changed, 14 insertions(+), 22 deletions(-) create mode 100644 packages/-serializer-encapsulation-test-app/tests/helpers/test-in-debug.js diff --git a/packages/-serializer-encapsulation-test-app/tests/helpers/test-in-debug.js b/packages/-serializer-encapsulation-test-app/tests/helpers/test-in-debug.js new file mode 100644 index 00000000000..949b64daac2 --- /dev/null +++ b/packages/-serializer-encapsulation-test-app/tests/helpers/test-in-debug.js @@ -0,0 +1,10 @@ +import { DEBUG } from '@glimmer/env'; +import { test, skip } from 'qunit'; + +export default function testInDebug() { + if (DEBUG) { + test(...arguments); + } else { + skip(...arguments); + } +} diff --git a/packages/-serializer-encapsulation-test-app/tests/integration/normalize-test.js b/packages/-serializer-encapsulation-test-app/tests/integration/normalize-test.js index dab6fc665c1..9de120f7c12 100644 --- a/packages/-serializer-encapsulation-test-app/tests/integration/normalize-test.js +++ b/packages/-serializer-encapsulation-test-app/tests/integration/normalize-test.js @@ -2,17 +2,8 @@ import Model, { attr } from '@ember-data/model'; import Store from 'serializer-encapsulation-test-app/services/store'; import EmberObject from '@ember/object'; import { setupTest } from 'ember-qunit'; -import { module, test, skip } from 'qunit'; -import { DEBUG } from '@glimmer/env'; - -// we fail smoke test if we import this -function testInDebug() { - if (DEBUG) { - test(...arguments); - } else { - skip(...arguments); - } -} +import { module, test } from 'qunit'; +import testInDebug from 'serializer-encapsulation-test-app/tests/helpers/test-in-debug'; class Person extends Model { @attr diff --git a/packages/-serializer-encapsulation-test-app/tests/integration/push-payload-test.js b/packages/-serializer-encapsulation-test-app/tests/integration/push-payload-test.js index 22e3f225ed6..488f80ddc6b 100644 --- a/packages/-serializer-encapsulation-test-app/tests/integration/push-payload-test.js +++ b/packages/-serializer-encapsulation-test-app/tests/integration/push-payload-test.js @@ -2,17 +2,8 @@ import Model, { attr } from '@ember-data/model'; import Store from 'serializer-encapsulation-test-app/services/store'; import EmberObject from '@ember/object'; import { setupTest } from 'ember-qunit'; -import { module, test, skip } from 'qunit'; -import { DEBUG } from '@glimmer/env'; - -// we fail smoke test if we import this -function testInDebug() { - if (DEBUG) { - test(...arguments); - } else { - skip(...arguments); - } -} +import { module, test } from 'qunit'; +import testInDebug from 'serializer-encapsulation-test-app/tests/helpers/test-in-debug'; class Person extends Model { @attr