Skip to content

Commit 7948f16

Browse files
VincentMolinieVincent Molinié
andauthored
fix(changedAttributes): initial value of property which are supposed to be null return a value when the record is in flight (#480)
* test: add tests of attributes initially null to proove regression * fix: changedAttributes return the correct value until the record has been fully committed * test: fix tests --------- Co-authored-by: Vincent Molinié <[email protected]>
1 parent 36149fe commit 7948f16

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

addon/record-data.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,7 @@ export default class FragmentRecordData extends RecordData {
671671
this._fragmentsOrInFlight
672672
)) {
673673
const behavior = this._fragmentBehavior[key];
674-
const oldFragment =
675-
key in this._inFlightFragments
676-
? this._inFlightFragments[key]
677-
: this._fragmentData[key];
674+
const oldFragment = this._fragmentData[key];
678675
diffData[key] = [
679676
behavior.canonicalState(oldFragment),
680677
behavior.currentState(newFragment),

tests/unit/fragment_test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,59 @@ module('unit - `MF.Fragment`', function (hooks) {
201201
);
202202
});
203203

204+
test("fragment properties that are initially null are indicated in the owner record's `changedAttributes`", async function (assert) {
205+
store.push({
206+
data: {
207+
type: 'person',
208+
id: 1,
209+
attributes: {
210+
name: null,
211+
},
212+
},
213+
});
214+
215+
const person = await store.findRecord('person', 1);
216+
person.set('name', {
217+
first: 'Rob',
218+
last: 'Stark',
219+
});
220+
221+
const [oldName, newName] = person.changedAttributes().name;
222+
assert.deepEqual(
223+
oldName,
224+
null,
225+
'old fragment is indicated in the diff object'
226+
);
227+
assert.deepEqual(
228+
newName,
229+
{ first: 'Rob', last: 'Stark', prefixes: [] },
230+
'new fragment is indicated in the diff object'
231+
);
232+
233+
person._internalModel.adapterWillCommit();
234+
235+
const [oldNameAfterWillCommit, newNameAfterWillCommit] =
236+
person.changedAttributes().name;
237+
assert.deepEqual(
238+
oldNameAfterWillCommit,
239+
null,
240+
'old fragment is indicated in the diff object'
241+
);
242+
assert.deepEqual(
243+
newNameAfterWillCommit,
244+
{ first: 'Rob', last: 'Stark', prefixes: [] },
245+
'new fragment is indicated in the diff object'
246+
);
247+
248+
person._internalModel.adapterDidCommit();
249+
250+
assert.strictEqual(
251+
person.changedAttributes().name,
252+
undefined,
253+
'changedAttributes is reset after commit'
254+
);
255+
});
256+
204257
test('changes to attributes can be rolled back', async function (assert) {
205258
store.push({
206259
data: {

0 commit comments

Comments
 (0)