Skip to content

Commit

Permalink
Add failing test case for autotracking of derived state
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyfel committed Jun 3, 2020
1 parent 8086d08 commit 95867a1
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/integration/misc-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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';

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

test('it re-renders derived state automatically when root state changes', async function(assert) {
class MyModel {
isOptionOne = false;
isOptionTwo = false;
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');
});
});

0 comments on commit 95867a1

Please sign in to comment.