Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHORE] pushPayload test for serializer encapsulation side quest #6554

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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