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

fix: notify during replace if existing localState never previously calculated #9251

Merged
merged 2 commits into from
Mar 11, 2024
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
Expand Up @@ -78,6 +78,13 @@ function replaceRelatedRecordsLocal(graph: Graph, op: ReplaceRelatedRecordsOpera
const identifiers = op.value;
const relationship = graph.get(op.record, op.field);
assert(`expected hasMany relationship`, isHasMany(relationship));

// relationships for newly created records begin in the dirty state, so if updated
// before flushed we would fail to notify. This check helps us avoid that.
const isMaybeFirstUpdate =
relationship.remoteState.length === 0 &&
relationship.localState === null &&
relationship.state.hasReceivedData === false;
relationship.state.hasReceivedData = true;
const { additions, removals } = relationship;
const { inverseKey, type } = relationship.definition;
Expand Down Expand Up @@ -151,7 +158,10 @@ function replaceRelatedRecordsLocal(graph: Graph, op: ReplaceRelatedRecordsOpera
relationship.localState = diff.finalState;
relationship.isDirty = wasDirty;

if (!wasDirty /*&& becameDirty // TODO to guard like this we need to detect reorder when diffing local */) {
if (
isMaybeFirstUpdate ||
!wasDirty /*&& becameDirty // TODO to guard like this we need to detect reorder when diffing local */
) {
notifyChange(graph, op.record, op.field);
}
}
Expand Down
44 changes: 28 additions & 16 deletions tests/main/tests/integration/references/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ import testInDebug from '@ember-data/unpublished-test-infra/test-support/test-in

import createTrackingContext from '../../helpers/create-tracking-context';

module('integration/references/has-many', function (hooks) {
setupRenderingTest(hooks);
class Family extends Model {
@hasMany('person', { async: true, inverse: 'family' }) persons;
}

hooks.beforeEach(function () {
const Family = Model.extend({
persons: hasMany('person', { async: true, inverse: 'family' }),
});
class Person extends Model {
@attr name;
@belongsTo('family', { async: true, inverse: 'persons' }) family;
@hasMany('pet', { async: true, inverse: null }) pets;
}

const Person = Model.extend({
name: attr(),
family: belongsTo('family', { async: true, inverse: 'persons' }),
pets: hasMany('pet', { async: true, inverse: null }),
});
class Pet extends Model {
@attr name;
}

const Pet = Model.extend({
name: attr(),
});
module('integration/references/has-many', function (hooks) {
setupRenderingTest(hooks);

hooks.beforeEach(function () {
this.owner.register('model:family', Family);
this.owner.register('model:person', Person);
this.owner.register('model:pet', Pet);

this.owner.register('adapter:application', Adapter.extend());
this.owner.register('serializer:application', class extends JSONAPISerializer {});
this.owner.register('adapter:application', Adapter);
this.owner.register('serializer:application', JSONAPISerializer);
});

testInDebug("record#hasMany asserts when specified relationship doesn't exist", function (assert) {
Expand Down Expand Up @@ -93,6 +93,18 @@ module('integration/references/has-many', function (hooks) {
assert.deepEqual(personsReference.ids(), ['1', '2']);
});

test('ref.ids() updates when using createRecord', async function (assert) {
const store = this.owner.lookup('service:store');

const family = store.createRecord('family');
const person1 = store.createRecord('person', {});
assert.strictEqual(family.hasMany('persons').ids().length, 0);

family.persons = [person1];

assert.strictEqual(family.hasMany('persons').ids().length, 1);
});

test('record#hasMany for linked references', function (assert) {
const store = this.owner.lookup('service:store');

Expand Down
Loading