This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/integration/components/widgets/post-small-action-test.js
Alan Guo Xiang Tan 5bd29b89dd
DEV: Add missing test case (#19510)
Follow-up to f77660b047
2022-12-19 10:09:19 +08:00

132 lines
4.0 KiB
JavaScript

import { module, test } from "qunit";
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",
function (hooks) {
setupRenderingTest(hooks);
test("does not have delete/edit/recover buttons by default", async function (assert) {
this.set("args", { id: 123 });
await render(
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
);
assert.ok(!exists(".small-action-desc .small-action-delete"));
assert.ok(!exists(".small-action-desc .small-action-recover"));
assert.ok(!exists(".small-action-desc .small-action-edit"));
});
test("shows edit button if canEdit", async function (assert) {
this.set("args", { id: 123, canEdit: true });
await render(
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
);
assert.ok(
exists(".small-action-desc .small-action-edit"),
"it adds the edit small action button"
);
});
test("uses custom widget if actionDescriptionWidget", async function (assert) {
this.set("args", { id: 123, actionDescriptionWidget: "button" });
await render(
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
);
assert.ok(
exists(".small-action .widget-button"),
"it adds the custom widget"
);
});
test("does not show edit button if canRecover even if canEdit", async function (assert) {
this.set("args", { id: 123, canEdit: true, canRecover: true });
await render(
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
);
assert.ok(
!exists(".small-action-desc .small-action-edit"),
"it does not add the edit small action button"
);
assert.ok(
exists(".small-action-desc .small-action-recover"),
"it adds the recover small action button"
);
});
test("shows delete button if canDelete", async function (assert) {
this.set("args", { id: 123, canDelete: true });
await render(
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
);
assert.ok(
exists(".small-action-desc .small-action-delete"),
"it adds the delete small action button"
);
});
test("shows undo button if canRecover", async function (assert) {
this.set("args", { id: 123, canRecover: true });
await render(
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
);
assert.ok(
exists(".small-action-desc .small-action-recover"),
"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();
}
});
}
);