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

Add safeSet for Ember tracking context #395

Merged
merged 8 commits into from
Jan 11, 2020
Merged
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 .travis.yml
Original file line number Diff line number Diff line change
@@ -51,8 +51,8 @@ jobs:
env: EMBER_TRY_SCENARIO=ember-3.13
- env: EMBER_TRY_SCENARIO=ember-release
- env: EMBER_TRY_SCENARIO=ember-beta
- env: EMBER_TRY_SCENARIO=ember-canary
- env: EMBER_TRY_SCENARIO=ember-default-with-jquery
- env: EMBER_TRY_SCENARIO=ember-canary

before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
6 changes: 3 additions & 3 deletions addon/utils/merge-deep.ts
Original file line number Diff line number Diff line change
@@ -101,11 +101,11 @@ function mergeTargetAndSource(target: any, source: any, options: Options): any {
} else {
let next = source[key];
if (next && next instanceof Change) {
return target[key] = next.value;
return options.safeSet(target, key, next.value);
}

// if just some normal leaf value, then set
return target[key] = next;
return options.safeSet(target, key, next);
}
});

@@ -123,7 +123,7 @@ function mergeTargetAndSource(target: any, source: any, options: Options): any {
*/
export default function mergeDeep(target: any, source: any, options: Options = { safeGet: undefined, safeSet: undefined }): object | [any] {
options.safeGet = options.safeGet || function (obj: any, key: string): any { return obj[key] };
options.safeSet = options.safeSet;
options.safeSet = options.safeSet || function(obj: any, key: string, value: unknown): any { return obj[key] = value };
let sourceIsArray = Array.isArray(source);
let targetIsArray = Array.isArray(target);
let sourceAndTargetTypesMatch = sourceIsArray === targetIsArray;
1 change: 1 addition & 0 deletions config/ember-try.js
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ module.exports = async function() {
},
{
name: 'ember-canary',
allowedToFail: true,
npm: {
devDependencies: {
'ember-source': await getChannelURL('canary')
16 changes: 16 additions & 0 deletions tests/dummy/app/components/changeset-form.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Component from '@ember/component';
import { action } from '@ember/object';
import Changeset from 'ember-changeset'

export default class ChangesetForm extends Component {
model = {
email: 'something'
}

changeset = new Changeset(this.model)

@action submitForm(changeset: any, event: any) {
event.preventDefault()
changeset.save()
}
}
4 changes: 3 additions & 1 deletion tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<h2 id="title">Hello world</h2>
<h2 id="title">Basic Form</h2>

<ChangesetForm />

{{outlet}}
16 changes: 16 additions & 0 deletions tests/dummy/app/templates/components/changeset-form.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Changeset value: <span data-test-changeset-email>{{this.changeset.email}}</span></h2>
<br />
<h2>Model value: <span data-test-model-email>{{this.model.email}}</span></h2>

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

</form>
17 changes: 17 additions & 0 deletions tests/integration/components/changeset-form-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { click, find, fillIn, render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Component | changeset-form', function(hooks) {
setupRenderingTest(hooks);

test('it renders', async function(assert) {
await render(hbs`<ChangesetForm />`);
await fillIn('[data-test-email]', 'foo');
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');
});
});