From 41c3c5745e0611828cb6ed84e5e625faf12df13a Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 1 Oct 2024 21:12:34 +0900 Subject: [PATCH] DEV: Fix optionalService in decorator form (#29042) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …and remove unused injections --- .../app/components/reviewable-item.js | 2 +- .../app/components/topic-timeline.js | 3 - .../discourse/app/controllers/user.js | 2 +- .../discourse/app/lib/optional-service.js | 12 +++- .../tests/unit/utils/optional-service-test.js | 59 +++++++++++++++++++ .../discourse/components/chat-message.gjs | 2 - 6 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 app/assets/javascripts/discourse/tests/unit/utils/optional-service-test.js diff --git a/app/assets/javascripts/discourse/app/components/reviewable-item.js b/app/assets/javascripts/discourse/app/components/reviewable-item.js index 235e4953631..cf7ee8e5776 100644 --- a/app/assets/javascripts/discourse/app/components/reviewable-item.js +++ b/app/assets/javascripts/discourse/app/components/reviewable-item.js @@ -49,7 +49,7 @@ export default class ReviewableItem extends Component { @service siteSettings; @service currentUser; @service composer; - @optionalService("admin-tools") adminTools; + @optionalService adminTools; updating = null; editing = false; diff --git a/app/assets/javascripts/discourse/app/components/topic-timeline.js b/app/assets/javascripts/discourse/app/components/topic-timeline.js index 6f71f1ec7c2..c654e6ef7c9 100644 --- a/app/assets/javascripts/discourse/app/components/topic-timeline.js +++ b/app/assets/javascripts/discourse/app/components/topic-timeline.js @@ -2,7 +2,6 @@ import Component from "@glimmer/component"; import { tracked } from "@glimmer/tracking"; import { action } from "@ember/object"; import { service } from "@ember/service"; -import optionalService from "discourse/lib/optional-service"; import { bind } from "discourse-common/utils/decorators"; export default class TopicTimeline extends Component { @@ -13,8 +12,6 @@ export default class TopicTimeline extends Component { @tracked docked = false; @tracked dockedBottom = false; - adminTools = optionalService(); - constructor() { super(...arguments); diff --git a/app/assets/javascripts/discourse/app/controllers/user.js b/app/assets/javascripts/discourse/app/controllers/user.js index 457dae2be50..5ef8d775f22 100644 --- a/app/assets/javascripts/discourse/app/controllers/user.js +++ b/app/assets/javascripts/discourse/app/controllers/user.js @@ -15,7 +15,7 @@ export default class UserController extends Controller.extend(CanCheckEmails) { @service currentUser; @service router; @service dialog; - @optionalService("admin-tools") adminTools; + @optionalService adminTools; @controller("user-notifications") userNotifications; diff --git a/app/assets/javascripts/discourse/app/lib/optional-service.js b/app/assets/javascripts/discourse/app/lib/optional-service.js index 07412b59c1b..f57bdfc0b0f 100644 --- a/app/assets/javascripts/discourse/app/lib/optional-service.js +++ b/app/assets/javascripts/discourse/app/lib/optional-service.js @@ -2,8 +2,16 @@ import { computed } from "@ember/object"; import { getOwner } from "@ember/owner"; import { dasherize } from "@ember/string"; -export default function (name) { - return computed(function (defaultName) { +export default function (target, name, descriptor) { + name ??= target; + + const decorator = computed(function (defaultName) { return getOwner(this).lookup(`service:${name || dasherize(defaultName)}`); }); + + if (descriptor) { + return decorator(target, name, descriptor); + } else { + return decorator; + } } diff --git a/app/assets/javascripts/discourse/tests/unit/utils/optional-service-test.js b/app/assets/javascripts/discourse/tests/unit/utils/optional-service-test.js new file mode 100644 index 00000000000..4008c9e3ff2 --- /dev/null +++ b/app/assets/javascripts/discourse/tests/unit/utils/optional-service-test.js @@ -0,0 +1,59 @@ +import Component from "@ember/component"; +import Service from "@ember/service"; +import { render } from "@ember/test-helpers"; +import { hbs } from "ember-cli-htmlbars"; +import { module, test } from "qunit"; +import optionalService from "discourse/lib/optional-service"; +import { setupRenderingTest } from "discourse/tests/helpers/component-test"; + +class FooService extends Service { + name = "foo"; +} + +class BarService extends Service { + name = "bar"; +} + +const EmberObjectComponent = Component.extend({ + name: "", + layout: hbs`{{this.foo.name}} {{this.baz.name}}`, + + foo: optionalService(), + baz: optionalService("bar"), +}); + +class NativeComponent extends Component { + @optionalService foo; + @optionalService("bar") baz; + + name = ""; + layout = hbs`{{this.foo.name}} {{this.baz.name}}`; +} + +module("Unit | Utils | optional-service", function (hooks) { + setupRenderingTest(hooks); + + hooks.beforeEach(function () { + this.registry.register("service:foo", FooService); + this.registry.register("service:bar", BarService); + }); + + test("optionalService works in EmberObject classes", async function (assert) { + this.registry.register( + "component:ember-object-component", + EmberObjectComponent + ); + + await render(hbs``); + + assert.dom(".ember-object-component").hasText("foo bar"); + }); + + test("optionalService works in native classes", async function (assert) { + this.registry.register("component:native-component", NativeComponent); + + await render(hbs``); + + assert.dom(".native-component").hasText("foo bar"); + }); +}); diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs index e4cf9cbc92d..c0726184267 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs +++ b/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs @@ -14,7 +14,6 @@ import { modifier } from "ember-modifier"; import { eq, not } from "truth-helpers"; import DButton from "discourse/components/d-button"; import concatClass from "discourse/helpers/concat-class"; -import optionalService from "discourse/lib/optional-service"; import { applyValueTransformer } from "discourse/lib/transformer"; import { updateUserStatusOnMention } from "discourse/lib/update-user-status-on-mention"; import isZoomed from "discourse/lib/zoom-check"; @@ -65,7 +64,6 @@ export default class ChatMessage extends Component { @service router; @service toasts; @service modal; - @optionalService adminTools; @tracked isActive = false;