From 68eafd2a3c53ddfcec432c855c28b16f2cbad2a6 Mon Sep 17 00:00:00 2001 From: Andrey Fel Date: Wed, 3 Jun 2020 16:37:51 +0300 Subject: [PATCH] Add failing test case for autotracking of derived state --- tests/integration/misc-test.js | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/integration/misc-test.js diff --git a/tests/integration/misc-test.js b/tests/integration/misc-test.js new file mode 100644 index 00000000..14ac941f --- /dev/null +++ b/tests/integration/misc-test.js @@ -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}} + + {{else}} + + {{/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'); + }); +});