Created
May 23, 2023 17:47
-
-
Save mupkoo/321e832e60fb8f176b696f715ad6215f to your computer and use it in GitHub Desktop.
Ember.JS add dynamic properties to a native class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable ember/no-computed-properties-in-native-classes */ | |
import { render, settled } from '@ember/test-helpers'; | |
import { setupRenderingTest } from 'ember-qunit'; | |
import hbs from 'htmlbars-inline-precompile'; | |
import { module, test } from 'qunit'; | |
import { set, defineProperty, computed } from '@ember/object'; | |
function example(target) { | |
defineProperty( | |
target.prototype, | |
'computed', | |
computed('bar', function () { | |
return `Works!${this.bar}`; | |
}) | |
); | |
return target; | |
} | |
@example | |
class Foo { | |
bar = ''; | |
} | |
module('Integration | Decorator', function (hooks) { | |
setupRenderingTest(hooks); | |
test('it works', async function (assert) { | |
this.foo = new Foo(); | |
await render(hbs` | |
<span id="computed">{{this.foo.computed}}</span> | |
`); | |
assert.dom('#computed').hasText('Works!'); | |
}); | |
test('it invalidates when the dependent key changes', async function (assert) { | |
this.foo = new Foo(); | |
await render(hbs` | |
<span id="computed">{{this.foo.computed}}</span> | |
`); | |
assert.dom('#computed').hasText('Works!'); | |
set(this.foo, 'bar', '!!'); | |
await settled(); | |
assert.dom('#computed').hasText('Works!!!'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment