Skip to content

Commit 68eafd2

Browse files
author
Andrey Fel
committed
Add failing test case for autotracking of derived state
1 parent eaf059a commit 68eafd2

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tests/integration/misc-test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { module, test } from 'qunit';
2+
import { setupRenderingTest } from 'ember-qunit';
3+
import { get, set } from '@ember/object';
4+
import { Changeset } from 'ember-changeset';
5+
import hbs from 'htmlbars-inline-precompile';
6+
import { render, find, settled } from '@ember/test-helpers';
7+
import { tracked } from '@glimmer/tracking';
8+
9+
module('Integration | misc', function(hooks) {
10+
setupRenderingTest(hooks);
11+
12+
test('it re-renders derived state automatically when root state changes', async function(assert) {
13+
class MyModel {
14+
@tracked isOptionOne = false;
15+
@tracked isOptionTwo = false;
16+
@tracked isOptionThree = false;
17+
}
18+
19+
const Validations = {
20+
isOptionSelected: (newValue) => {
21+
return newValue === true ? true : 'No options selected';
22+
}
23+
}
24+
25+
function myValidator({ key, newValue, oldValue, changes, content }) {
26+
let validatorFn = get(Validations, key);
27+
28+
if (typeof validatorFn === 'function') {
29+
return validatorFn(newValue, oldValue, changes, content);
30+
}
31+
}
32+
33+
const myObject = new MyModel();
34+
const myChangeset = Changeset(myObject, myValidator, Validations);
35+
36+
Object.defineProperty(myChangeset, 'isOptionSelected', {
37+
get() {
38+
return this.get('isOptionOne') || this.get('isOptionTwo') || this.get('isOptionThree');
39+
}
40+
});
41+
42+
// initial validation
43+
await myChangeset.validate();
44+
45+
this.set('myChangeset', myChangeset);
46+
47+
await render(hbs`
48+
{{#if this.myChangeset.isInvalid}}
49+
<span class="invalid"></span>
50+
{{else}}
51+
<span class="valid"></span>
52+
{{/if}}
53+
`);
54+
55+
assert.ok(find('.invalid'), 'Changeset is invalid as none of the options are selected');
56+
57+
set(myChangeset, 'isOptionTwo', true);
58+
await settled();
59+
60+
// We don't call validate explicitly, expecting autotracking to handle that since skipValidate is false
61+
assert.ok(find('.valid'), 'Changeset is valid as one of the options is selected');
62+
});
63+
});

0 commit comments

Comments
 (0)