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

Another safeSet for Ember happiness #397

Merged
merged 1 commit into from
Jan 11, 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
2 changes: 1 addition & 1 deletion addon/utils/merge-deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function mergeTargetAndSource(target: any, source: any, options: Options): any {

// else safe key on object
if (propertyIsOnObject(target, key) && isMergeableObject(source[key]) && !source[key].hasOwnProperty('value')) {
target[key] = mergeDeep(options.safeGet(target, key), options.safeGet(source, key), options);
options.safeSet(target, key, mergeDeep(options.safeGet(target, key), options.safeGet(source, key), options));
} else {
let next = source[key];
if (next && next instanceof Change) {
Expand Down
5 changes: 4 additions & 1 deletion tests/dummy/app/components/changeset-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import Changeset from 'ember-changeset'

export default class ChangesetForm extends Component {
model = {
email: 'something'
user: {
email: 'something'
},
cid: '1'
}

changeset = new Changeset(this.model)
Expand Down
21 changes: 16 additions & 5 deletions tests/dummy/app/templates/components/changeset-form.hbs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
<h2>Changeset value: <span data-test-changeset-email>{{this.changeset.email}}</span></h2>
<h2>Changeset Email: <span data-test-changeset-user-email>{{this.changeset.user.email}}</span></h2>
<h2>Changeset cid: <span data-test-changeset-cid>{{this.changeset.cid}}</span></h2>
<br />
<h2>Model value: <span data-test-model-email>{{this.model.email}}</span></h2>
<h2>Model Email: <span data-test-model-user-email>{{this.model.user.email}}</span></h2>
<h2>Model cid: <span data-test-model-cid>{{this.model.cid}}</span></h2>

<form
{{on "submit" (fn this.submitForm this.changeset)}}
>
<label>cid</label>
<input
data-test-email
data-test-cid
type="number"
value={{this.changeset.cid}}
oninput={{action (changeset-set this.changeset "cid") value="target.value"}}
>

<label>email</label>
<input
data-test-user-email
type="text"
value={{this.changeset.email}}
oninput={{action (changeset-set this.changeset "email") value="target.value"}}
value={{this.changeset.user.email}}
oninput={{action (changeset-set this.changeset "user.email") value="target.value"}}
>
<button data-test-submit type="submit">Submit form 1</button>

Expand Down
8 changes: 5 additions & 3 deletions tests/integration/components/changeset-form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ module('Integration | Component | changeset-form', function(hooks) {

test('it renders', async function(assert) {
await render(hbs`<ChangesetForm />`);
await fillIn('[data-test-email]', 'foo');
await fillIn('[data-test-user-email]', 'foo');
await fillIn('[data-test-cid]', 2);
await click('[data-test-submit]');

assert.equal(find('[data-test-model-email]').textContent.trim(), 'foo', 'has email after submit');
assert.equal(find('[data-test-changeset-email]').textContent.trim(), 'foo', 'changeset has email after submit');
assert.equal(find('[data-test-model-cid]').textContent.trim(), '2', 'has email after submit');
assert.equal(find('[data-test-model-user-email]').textContent.trim(), 'foo', 'has email after submit');
assert.equal(find('[data-test-changeset-user-email]').textContent.trim(), 'foo', 'changeset has email after submit');
});
});