Skip to content

Commit

Permalink
small cleanups and port #7266 test
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed May 4, 2021
1 parent 9946278 commit 11d6e76
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ module('integration/relationship/belongs-to BelongsTo Relationships (new-style)'
store = owner.lookup('service:store');
});

test("belongsTo saving with relationship that references yourself doesn't blow up", async function (assert) {
class Company extends Model {
@belongsTo('company', { inverse: null, async: true })
parentCompany;
@attr()
name;
}

this.owner.register('model:company', Company);
this.owner.register(
'adapter:company',
JSONAPIAdapter.extend({
createRecord(store, type, snapshot) {
return resolve({
data: {
type: 'company',
id: '123',
attributes: { name: 'Acme Corporation' },
relationships: {
parentCompany: {
data: { type: 'company', id: '123' },
},
},
},
});
},
})
);

let company = store.createRecord('company', { name: 'Acme Corporation' });
await company.save();
assert.strictEqual(company.id, '123', 'We updated to the correct id');
assert.strictEqual(company.belongsTo('parentCompany').id(), company.id, 'We are able to reference ourselves');
});

test("async belongsTo chains the related record's loading promise when present", async function (assert) {
let petFindRecordCalls = 0;
this.owner.register(
Expand Down
7 changes: 3 additions & 4 deletions packages/record-data/addon/-private/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,14 @@ export default class RecordDataDefault implements RelationshipRecordData {
this._isNew = false;
let newCanonicalAttributes: AttributesHash | null = null;
if (data) {
// this.store._internalModelDidReceiveRelationshipData(this.modelName, this.id, data.relationships);
if (data.relationships) {
this._setupRelationships(data);
}
if (data.id) {
// didCommit provided an ID, notify the store of it
this.storeWrapper.setRecordId(this.modelName, data.id, this.clientId);
this.id = coerceId(data.id);
}
if (data.relationships) {
this._setupRelationships(data);
}
newCanonicalAttributes = data.attributes || null;
}
let changedKeys = this._changedKeys(newCanonicalAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,7 @@ function anyUnloaded(manyArray) {
// Can't use `find` because of IE11 and these arrays are potentially massive
let state = manyArray.currentState;
let unloaded = false;
for (let i = 0; i < this.currentState.length; i++) {
for (let i = 0; i < state.length; i++) {
let im = state[i];
if (im._isDematerializing || !im.currentState.isLoaded) {
unloaded = true;
Expand Down
29 changes: 28 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5517,6 +5517,15 @@ cliui@^5.0.0:
strip-ansi "^5.2.0"
wrap-ansi "^5.1.0"

cliui@^6.0.0:
version "6.0.0"
resolved "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz#511d702c0c4e41ca156d7d0e96021f23e13225b1"
integrity sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==
dependencies:
string-width "^4.2.0"
strip-ansi "^6.0.0"
wrap-ansi "^6.2.0"

cliui@^7.0.2:
version "7.0.4"
resolved "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz#a0265ee655476fc807aea9df3df8df7783808b4f"
Expand Down Expand Up @@ -7599,7 +7608,8 @@ eslint-module-utils@^2.6.0:
pkg-dir "^2.0.0"

"eslint-plugin-ember-data@link:./packages/unpublished-eslint-rules":
version "3.28.0-alpha.1"
version "0.0.0"
uid ""

eslint-plugin-ember@^10.3.0, eslint-plugin-ember@^10.4.1:
version "10.4.1"
Expand Down Expand Up @@ -15781,6 +15791,15 @@ wrap-ansi@^5.1.0:
string-width "^3.0.0"
strip-ansi "^5.0.0"

wrap-ansi@^6.2.0:
version "6.2.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz#e9393ba07102e6c91a3b221478f0257cd2856e53"
integrity sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
Expand Down Expand Up @@ -15943,6 +15962,14 @@ yargs-parser@20.2.4:
resolved "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.4.tgz#b42890f14566796f85ae8e3a25290d205f154a54"
integrity sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA==

yargs-parser@^18.1.2:
version "18.1.3"
resolved "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0"
integrity sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==
dependencies:
camelcase "^5.0.0"
decamelize "^1.2.0"

yargs-parser@^20.2.2, yargs-parser@^20.2.3:
version "20.2.7"
resolved "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.7.tgz#61df85c113edfb5a7a4e36eb8aa60ef423cbc90a"
Expand Down

0 comments on commit 11d6e76

Please sign in to comment.