Skip to content

Commit

Permalink
Override getChangesForSnapshot and getChangesFromSnapshot methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sapryniukt committed Mar 18, 2022
1 parent 6ae0ee6 commit a7eb661
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
35 changes: 34 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from '@ember/debug';
import { dependentKeyCompat } from '@ember/object/compat';
import { BufferedChangeset } from 'validated-changeset';
import { BufferedChangeset, Change, isChange } from 'validated-changeset';
import ArrayProxy from '@ember/array/proxy';
import ObjectProxy from '@ember/object/proxy';
import { notifyPropertyChange } from '@ember/object';
Expand All @@ -10,6 +10,7 @@ import { tracked } from '@glimmer/tracking';
import { get as safeGet, set as safeSet } from '@ember/object';
import { macroCondition, dependencySatisfies, importSync } from '@embroider/macros';

const { keys, getOwnPropertySymbols } = Object;
const CHANGES = '_changes';
const PREVIOUS_CONTENT = '_previousContent';
const CONTENT = '_content';
Expand Down Expand Up @@ -196,6 +197,38 @@ export class EmberChangeset extends BufferedChangeset {

return this;
}

/**
* Gets the changes of the changeset to take a snapshot of them
*
* @override
* @see https://github.com/validated-changeset/validated-changeset/blob/main/src/index.ts#L673
*/
getChangesForSnapshot(changes) {
return keys(changes).reduce((newObj, key) => {
newObj[key] = isChange(changes[key]) ? changes[key] : this.getChangesForSnapshot(changes[key]);
return newObj;
}, {});
}

/**
* Gets the changes of the changeset from the snapshot as well as
* takes nested keys and recursively makes their values into `Change` objects.
*
* @override
* @see https://github.com/validated-changeset/validated-changeset/blob/main/src/index.ts#L699
*/
getChangesFromSnapshot(value) {
if (isChange(value)) {
const VALUE = getOwnPropertySymbols(value)[0];
return new Change(value[VALUE]);
}

return keys(value).reduce((newObj, key) => {
newObj[key] = this.getChangesFromSnapshot(value[key]);
return newObj;
}, {});
}
}

/**
Expand Down
35 changes: 33 additions & 2 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Changeset, EmberChangeset, Changeset as ChangesetFactory } from 'ember-
import { settled } from '@ember/test-helpers';
import { module, test, todo } from 'qunit';
import { setupTest } from 'ember-qunit';
import { lookupValidator } from 'validated-changeset';
import { lookupValidator, Change } from 'validated-changeset';

import EmberObject, { get, set, setProperties } from '@ember/object';

Expand Down Expand Up @@ -2427,7 +2427,7 @@ module('Unit | Utility | changeset', function (hooks) {
dummyChangeset.set('password', false);
let snapshot = dummyChangeset.snapshot();
let expectedResult = {
changes: { name: 'Pokemon Go', password: false },
changes: { name: new Change('Pokemon Go'), password: new Change(false) },
errors: { password: { validation: ['foo', 'bar'], value: false } },
};

Expand Down Expand Up @@ -2458,6 +2458,37 @@ module('Unit | Utility | changeset', function (hooks) {
);
});

test('#restore restores a snapshot of the changeset when nested value is object', function (assert) {
class Country {
constructor(id, name) {
this.id = id;
this.name = name;
}
}

var us = new Country('US', 'United States');
var prk = new Country('PRK', 'North Korea');
var aus = new Country('AUS', 'Australia');

let user = {
name: 'Adam',
address: { country: us },
};
let dummyChangeset = Changeset(user);

dummyChangeset.set('name', 'Jim Bob');
dummyChangeset.set('address.country', prk);
let snapshot1 = dummyChangeset.snapshot();

dummyChangeset.set('name', 'Poteto');
dummyChangeset.set('address.country', aus);

dummyChangeset.restore(snapshot1);

assert.strictEqual(get(dummyChangeset, 'change.name'), 'Jim Bob', 'should restore changes');
assert.deepEqual(get(dummyChangeset, 'change.address.country'), prk, 'should restore nested changes');
});

/**
* #cast
*/
Expand Down

0 comments on commit a7eb661

Please sign in to comment.