DEV: Add missing test case (#19510)

Follow-up to f77660b047
This commit is contained in:
Alan Guo Xiang Tan 2022-12-19 10:09:19 +08:00 committed by GitHub
parent baf78d3d91
commit 5bd29b89dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 1 deletions

View File

@ -87,6 +87,10 @@ export function addPostSmallActionClassesCallback(callback) {
addPostSmallActionClassesCallbacks.push(callback);
}
export function resetPostSmallActionClassesCallbacks() {
addPostSmallActionClassesCallbacks.length = 0;
}
export default createWidget("post-small-action", {
buildKey: (attrs) => `post-small-act-${attrs.id}`,
tagName: "div.small-action.onscreen-post",
@ -97,13 +101,15 @@ export default createWidget("post-small-action", {
buildClasses(attrs) {
let classNames = [];
if (attrs.deleted) {
classNames.push("deleted");
}
if (addPostSmallActionClassesCallbacks) {
if (addPostSmallActionClassesCallbacks.length > 0) {
addPostSmallActionClassesCallbacks.forEach((callback) => {
const additionalClasses = callback.call(this, attrs);
if (additionalClasses) {
classNames.push(...additionalClasses);
}

View File

@ -3,6 +3,8 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { render } from "@ember/test-helpers";
import { exists } from "discourse/tests/helpers/qunit-helpers";
import { hbs } from "ember-cli-htmlbars";
import { withPluginApi } from "discourse/lib/plugin-api";
import { resetPostSmallActionClassesCallbacks } from "discourse/widgets/post-small-action";
module(
"Integration | Component | Widget | post-small-action",
@ -89,5 +91,41 @@ module(
"it adds the recover small action button"
);
});
test("`addPostSmallActionClassesCallback` plugin api", async function (assert) {
try {
withPluginApi("1.6.0", (api) => {
api.addPostSmallActionClassesCallback((postAttrs) => {
if (postAttrs.canRecover) {
return ["abcde"];
}
});
});
this.set("args", { id: 123, canRecover: false });
await render(
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
);
assert.notOk(
exists(".abcde"),
"custom CSS class is not added when condition is not met"
);
this.set("args", { id: 123, canRecover: true });
await render(
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
);
assert.ok(
exists(".abcde"),
"it adds custom CSS class as registered from the plugin API"
);
} finally {
resetPostSmallActionClassesCallbacks();
}
});
}
);