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

Bugfix for wrong hasMany relationship state in scenario of sideposting with lid #7126

Merged
merged 1 commit into from
Oct 9, 2020
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
@@ -1,11 +1,11 @@
import { module, test } from 'qunit';
import { defer } from 'rsvp';
import { defer, resolve } from 'rsvp';

import { setupTest } from 'ember-qunit';

import Adapter from '@ember-data/adapter';
import { RECORD_DATA_STATE } from '@ember-data/canary-features';
import Model, { attr } from '@ember-data/model';
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import Serializer from '@ember-data/serializer';
import Store, { recordIdentifierFor } from '@ember-data/store';

Expand Down Expand Up @@ -145,4 +145,79 @@ module('Integration | Identifiers - lid reflection', function(hooks) {

assert.strictEqual(record.name, '@runspired', 'After we finish we use the most recent clean name');
});

test('hasMany() has correct state after .save() on a newly created record with sideposted child record when lid is provided in the response payload', async function(assert) {
class Ingredient extends Model {
@attr name;
@belongsTo('cake') cake;
}

class Cake extends Model {
@attr name;
@hasMany('ingredient', { async: false }) ingredients;
}

this.owner.register('model:ingredient', Ingredient);
this.owner.register('model:cake', Cake);

class TestSerializer extends Serializer {
normalizeResponse(_, __, payload) {
return payload;
}
}
class TestAdapter extends Adapter {
createRecord(store, ModelClass, snapshot) {
return resolve({
data: {
type: 'cake',
id: '1',
attributes: {
name: 'Cheesecake',
},
relationships: {
ingredients: {
data: [
{
type: 'ingredient',
id: '2',
lid: cheeseIdentifier.lid,
},
],
},
},
},
included: [
{
type: 'ingredient',
id: '2',
lid: cheeseIdentifier.lid,
attributes: {
name: 'Cheese',
},
relationships: {
cake: {
data: {
type: 'cake',
id: '1',
},
},
},
},
],
});
}
}
this.owner.register('serializer:application', TestSerializer);
this.owner.register('adapter:application', TestAdapter);

const cheese = store.createRecord('ingredient', { name: 'Cheese' });
const cake = store.createRecord('cake', { name: 'Cheesecake', ingredients: [cheese] });

const cheeseIdentifier = recordIdentifierFor(cheese);

await cake.save();

assert.deepEqual(cake.hasMany('ingredients').ids(), ['2']);
assert.equal(cake.ingredients.objectAt(0).name, 'Cheese');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ export default class ManyRelationship extends Relationship {
} else {
recordDatas = new Array(data.length);
for (let i = 0; i < data.length; i++) {
recordDatas[i] = this.recordData.storeWrapper.recordDataFor(data[i].type, data[i].id) as RelationshipRecordData;
recordDatas[i] = this.recordData.storeWrapper.recordDataFor(
data[i].type,
data[i].id,
data[i].lid
) as RelationshipRecordData;
}
}
this.updateRecordDatasFromAdapter(recordDatas);
Expand Down