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: prevent rollbackRelationships from setting remoteState and localState to the same array reference #9221

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/graph/src/-private/-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export function rollbackRelationship(
op: 'replaceRelatedRecords',
record: identifier,
field,
value: relationship.remoteState,
value: relationship.remoteState.slice(),
},
false
);
Expand Down
55 changes: 55 additions & 0 deletions tests/main/tests/integration/relationships/rollback-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { settled } from '@ember/test-helpers';

import { module, test } from 'qunit';

import { setupTest } from 'ember-qunit';

import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import type Store from '@ember-data/store';
import { recordIdentifierFor } from '@ember-data/store';
import type { StableRecordIdentifier } from '@warp-drive/core-types';

class App extends Model {
Expand Down Expand Up @@ -398,6 +401,58 @@ module('Integration | Relationships | Rollback', function (hooks) {
assert.arrayStrictEquals(changed, [], 'belongsTo has rolled back');
assert.strictEqual(config.app, store.peekRecord('app', '1') as App, 'belongsTo has rolled back');
});

test('relationship rollback can be repeated', async function (assert) {
class Message extends Model {
@attr declare msg: string;
}
class Job extends Model {
@attr declare name: string;
@hasMany('message', { async: false, inverse: null }) declare messages: Message[];
}

this.owner.register('model:job', Job);
this.owner.register('model:message', Message);
const store = this.owner.lookup('service:store') as Store;

const job = store.push({
data: {
id: '1',
type: 'job',
attributes: {
name: 'First Job',
},
},
}) as Job;

const msg1 = store.push({
data: {
id: '1',
type: 'message',
attributes: {
msg: 'First Message',
},
},
}) as Message;
assert.strictEqual(job.messages.length, 0, 'job has 0 messages');
const jobIdentifier = recordIdentifierFor(job);

// add message, assert state, rollback, assert state is clean
job.messages.push(msg1);
assert.strictEqual(job.messages.length, 1, 'job has 1 message');

const rollbackResult = store.cache.rollbackRelationships(jobIdentifier);
assert.strictEqual(rollbackResult.length, 1, '1 rollbackRelations');
assert.strictEqual(job.messages.length, 0, 'job has no message');

// repeat the scenario to add a message and rollback
job.messages.push(msg1);
assert.strictEqual(job.messages.length, 1, 'job has 1 message');

const rollbackResult2 = store.cache.rollbackRelationships(jobIdentifier);
assert.strictEqual(rollbackResult2.length, 1, '1 rollbackRelations');
assert.strictEqual(job.messages.length, 0, 'job has no message');
});
});

module('<cache>.changedRelationships', function () {
Expand Down
Loading