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

ISSUE-6012: test: added test for meta property and bug fix. #6640

Merged
merged 1 commit into from
Oct 25, 2019
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
71 changes: 70 additions & 1 deletion packages/-ember-data/tests/unit/promise-proxies-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Promise as EmberPromise } from 'rsvp';
import { A } from '@ember/array';

import Model, { belongsTo } from '@ember-data/model';
import Adapter from '@ember-data/adapter';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import { setupTest } from 'ember-qunit';
import { module, test } from 'qunit';

import DS from 'ember-data';
Expand Down Expand Up @@ -110,3 +113,69 @@ module('PromiseManyArray', function() {
});
});
});

module('unit/PromiseBelongsTo', function(hooks) {
setupTest(hooks);

class Parent extends Model {
@belongsTo('child', { async: true })
child;
}
class Child extends Model {
@belongsTo('parent', { async: false })
parent;
}
class ChildAdapter extends Adapter {
findRecord(store, type, id, snapshot) {
const ChildRecord = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know if accessing meta ends up triggering this request? We should add an assertion to codify the current behavior so we at least know if/when we change it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in the console it was trying to fetch the record but the test would fail because the endpoint doesn't exist so i just mocked it.
I added some debuggers in and it appears that findRecord runs before we check the meta, so i guess the parent.child bit causes the request to trigger.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'd expect. We should add a test so that this behavior is explicitly covered. It's not necessarily a good behavior, but knowing that's what we do now is important to refactoring later :)

data: {
id: '1',
type: 'child',
relationships: {
parent: {
data: {
id: 1,
type: 'parent',
},
},
},
},
};
return EmberPromise.resolve(ChildRecord);
}
}

test('meta property exists', function(assert) {
const { owner } = this;
owner.register('model:parent', Parent);
owner.register('model:child', Child);
owner.register('adapter:child', ChildAdapter);
owner.register('serializer:application', JSONAPISerializer.extend());
const store = owner.lookup('service:store');
const meta = {
example: 'example meta',
};
const parent = store.push({
data: {
id: '1',
type: 'parent',
relationships: {
child: {
data: {
type: 'child',
id: '1',
},
meta,
},
},
},
});

const belongsToProxy = parent.child;

assert.expectAssertion(() => {
belongsToProxy.get('meta');
}, 'You attempted to access meta on the promise for the async belongsTo relationship ' + `child:child'.` + '\nUse `record.belongsTo(relationshipName).meta()` instead.');
assert.equal(parent.belongsTo('child').meta(), meta);
});
});
2 changes: 1 addition & 1 deletion packages/store/addon/-private/system/promise-proxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const PromiseBelongsTo = PromiseObject.extend({
meta: computed(function() {
assert(
'You attempted to access meta on the promise for the async belongsTo relationship ' +
`${this.get('_belongsToState').internalModel.modelName}:${this.get('_belongsToState').key}'.` +
`${this.get('_belongsToState').modelName}:${this.get('_belongsToState').key}'.` +
'\nUse `record.belongsTo(relationshipName).meta()` instead.',
false
);
Expand Down