Skip to content

Commit

Permalink
[CHORE] pushPayload test for serializer encapsulation side quest (#6554)
Browse files Browse the repository at this point in the history
* [CHORE] pushPayload test for serializer encapsulation side quest

* do not duplicate test in debug
  • Loading branch information
Gaurav0 authored and runspired committed Oct 8, 2019
1 parent 835fd28 commit 7d2b188
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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 } from 'qunit';
import testInDebug from 'serializer-encapsulation-test-app/tests/helpers/test-in-debug';

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/);
});
});
4 changes: 4 additions & 0 deletions packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 7d2b188

Please sign in to comment.