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 failing test case for autotracking of derived state #495

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions tests/integration/misc-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { get, set } from '@ember/object';
import { Changeset } from 'ember-changeset';
import hbs from 'htmlbars-inline-precompile';
import { render, find, settled } from '@ember/test-helpers';
import { tracked } from '@glimmer/tracking';

module('Integration | misc', function(hooks) {
setupRenderingTest(hooks);

test('it re-renders derived state automatically when root state changes', async function(assert) {
class MyModel {
@tracked isOptionOne = false;
@tracked isOptionTwo = false;
@tracked isOptionThree = false;
}

const Validations = {
isOptionSelected: (newValue) => {
return newValue === true ? true : 'No options selected';
}
}

function myValidator({ key, newValue, oldValue, changes, content }) {
let validatorFn = get(Validations, key);

if (typeof validatorFn === 'function') {
return validatorFn(newValue, oldValue, changes, content);
}
}

const myObject = new MyModel();
const myChangeset = Changeset(myObject, myValidator, Validations);

Object.defineProperty(myChangeset, 'isOptionSelected', {
get() {
return this.get('isOptionOne') || this.get('isOptionTwo') || this.get('isOptionThree');
}
});

// initial validation
await myChangeset.validate();

this.set('myChangeset', myChangeset);

await render(hbs`
{{#if this.myChangeset.isInvalid}}
<span class="invalid"></span>
{{else}}
<span class="valid"></span>
{{/if}}
`);

assert.ok(find('.invalid'), 'Changeset is invalid as none of the options are selected');

set(myChangeset, 'isOptionTwo', true);
await settled();

// We don't call validate explicitly, expecting autotracking to handle that since skipValidate is false
assert.ok(find('.valid'), 'Changeset is valid as one of the options is selected');
});
});