From 21abcfe5a7e497640c6546490dcec9778875dfc8 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Mon, 22 Aug 2022 17:33:32 +0200 Subject: [PATCH] FIX: Track native class deps in discourseComputed (#18023) Co-authored-by: David Taylor --- .../addon/utils/handle-descriptor.js | 2 +- .../tests/unit/utils/decorators-test.js | 99 ++++++++++++------- 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/app/assets/javascripts/discourse-common/addon/utils/handle-descriptor.js b/app/assets/javascripts/discourse-common/addon/utils/handle-descriptor.js index eb15763279e..fb371a7bf4f 100644 --- a/app/assets/javascripts/discourse-common/addon/utils/handle-descriptor.js +++ b/app/assets/javascripts/discourse-common/addon/utils/handle-descriptor.js @@ -11,7 +11,7 @@ export default function handleDescriptor(target, key, desc, params = []) { desc.value = undefined; desc.get = callUserSuppliedGet(params, val); - return computed(target, key, desc); + return computed(...params)(target, key, desc); } else { return { enumerable: desc.enumerable, diff --git a/app/assets/javascripts/discourse/tests/unit/utils/decorators-test.js b/app/assets/javascripts/discourse/tests/unit/utils/decorators-test.js index 2ec56a92b0b..0563fc56d02 100644 --- a/app/assets/javascripts/discourse/tests/unit/utils/decorators-test.js +++ b/app/assets/javascripts/discourse/tests/unit/utils/decorators-test.js @@ -1,12 +1,11 @@ +import { module, test } from "qunit"; +import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import Component from "@ember/component"; -import { clearRender } from "@ember/test-helpers"; +import { clearRender, render } from "@ember/test-helpers"; import discourseComputed, { afterRender, } from "discourse-common/utils/decorators"; -import componentTest, { - setupRenderingTest, -} from "discourse/tests/helpers/component-test"; -import { discourseModule, exists } from "discourse/tests/helpers/qunit-helpers"; +import { exists } from "discourse/tests/helpers/qunit-helpers"; import { hbs } from "ember-cli-htmlbars"; const fooComponent = Component.extend({ @@ -32,8 +31,19 @@ const fooComponent = Component.extend({ }, }); +const EmberObjectComponent = Component.extend({ + name: "", + layout: hbs`{{this.text}}`, + + @discourseComputed("name") + text(name) { + return `hello, ${name}`; + }, +}); + class NativeComponent extends Component { name = ""; + layout = hbs`{{this.text}}`; @discourseComputed("name") text(name) { @@ -41,49 +51,62 @@ class NativeComponent extends Component { } } -discourseModule("Unit | Utils | decorators", function (hooks) { +module("Unit | Utils | decorators", function (hooks) { setupRenderingTest(hooks); - componentTest("afterRender", { - template: hbs`{{foo-component baz=baz}}`, + test("afterRender", async function (assert) { + this.registry.register("component:foo-component", fooComponent); + this.set("baz", 0); - beforeEach() { - this.registry.register("component:foo-component", fooComponent); - this.set("baz", 0); - }, + await render(hbs`{{foo-component baz=baz}}`); - async test(assert) { - assert.ok(exists(document.querySelector(".foo-component"))); - assert.strictEqual(this.baz, 1); + assert.ok(exists(document.querySelector(".foo-component"))); + assert.strictEqual(this.baz, 1); - await clearRender(); + await clearRender(); - assert.ok(!exists(document.querySelector(".foo-component"))); - assert.strictEqual(this.baz, 1); - }, + assert.ok(!exists(document.querySelector(".foo-component"))); + assert.strictEqual(this.baz, 1); }); - componentTest("discourseComputed works in native classes", { - template: hbs``, + test("discourseComputed works in EmberObject classes", async function (assert) { + this.registry.register( + "component:ember-object-component", + EmberObjectComponent + ); - beforeEach() { - // eslint-disable-next-line no-undef - Ember.TEMPLATES[ - "components/native-component" - ] = hbs`{{this.text}}`; - this.registry.register("component:native-component", NativeComponent); - }, + this.set("name", "Jarek"); + await render(hbs``); - afterEach() { - // eslint-disable-next-line no-undef - delete Ember.TEMPLATES["components/native-component"]; - }, + assert.strictEqual( + document.querySelector(".ember-object-component").textContent, + "hello, Jarek" + ); - test(assert) { - assert.strictEqual( - document.querySelector(".native-component").textContent, - "hello, Jarek" - ); - }, + this.set("name", "Joffrey"); + assert.strictEqual( + document.querySelector(".ember-object-component").textContent, + "hello, Joffrey", + "rerenders the component when arguments change" + ); + }); + + test("discourseComputed works in native classes", async function (assert) { + this.registry.register("component:native-component", NativeComponent); + + this.set("name", "Jarek"); + await render(hbs``); + + assert.strictEqual( + document.querySelector(".native-component").textContent, + "hello, Jarek" + ); + + this.set("name", "Joffrey"); + assert.strictEqual( + document.querySelector(".native-component").textContent, + "hello, Joffrey", + "rerenders the component when arguments change" + ); }); });