2024-07-25 21:09:06 +08:00
|
|
|
import { getOwner } from "@ember/owner";
|
2023-06-08 22:37:50 +08:00
|
|
|
import { setupTest } from "ember-qunit";
|
2023-05-30 00:41:12 +08:00
|
|
|
import { module, test } from "qunit";
|
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2023-06-08 22:37:50 +08:00
|
|
|
import User from "discourse/models/user";
|
2023-10-11 02:38:59 +08:00
|
|
|
import pretender from "discourse/tests/helpers/create-pretender";
|
|
|
|
import { logIn } from "discourse/tests/helpers/qunit-helpers";
|
2023-06-08 22:37:50 +08:00
|
|
|
import ChatMessageInteractor, {
|
|
|
|
resetRemovedChatComposerSecondaryActions,
|
|
|
|
} from "discourse/plugins/chat/discourse/lib/chat-message-interactor";
|
2024-04-09 03:00:09 +08:00
|
|
|
import ChatFabricators from "discourse/plugins/chat/discourse/lib/fabricators";
|
2023-05-30 00:41:12 +08:00
|
|
|
|
|
|
|
module("Chat | Unit | Utility | plugin-api", function (hooks) {
|
|
|
|
setupTest(hooks);
|
|
|
|
|
|
|
|
test("#sendChatMessage", async function (assert) {
|
|
|
|
const done = assert.async();
|
|
|
|
|
|
|
|
pretender.post("/chat/1", (request) => {
|
|
|
|
assert.strictEqual(request.url, "/chat/1");
|
|
|
|
assert.strictEqual(request.requestBody, "thread_id=2&message=hello");
|
|
|
|
done();
|
|
|
|
return [200, {}, {}];
|
|
|
|
});
|
|
|
|
|
|
|
|
withPluginApi("1.1.0", async (api) => {
|
|
|
|
await api.sendChatMessage(1, { message: "hello", threadId: 2 });
|
|
|
|
});
|
|
|
|
});
|
2023-06-08 22:37:50 +08:00
|
|
|
|
|
|
|
test("#removeChatComposerSecondaryActions", async function (assert) {
|
|
|
|
withPluginApi("1.1.0", async (api) => {
|
|
|
|
// assert that the api method is defined
|
2024-11-04 04:04:38 +08:00
|
|
|
assert.strictEqual(
|
|
|
|
typeof api.removeChatComposerSecondaryActions,
|
|
|
|
"function"
|
|
|
|
);
|
2023-06-08 22:37:50 +08:00
|
|
|
|
|
|
|
logIn();
|
|
|
|
const currentUser = User.current();
|
|
|
|
getOwner(this).unregister("service:current-user");
|
|
|
|
getOwner(this).register("service:current-user", currentUser, {
|
|
|
|
instantiate: false,
|
|
|
|
});
|
|
|
|
|
2024-04-09 03:00:09 +08:00
|
|
|
const message = new ChatFabricators(getOwner(this)).message({
|
2023-07-27 15:57:03 +08:00
|
|
|
user: currentUser,
|
|
|
|
});
|
2023-06-08 22:37:50 +08:00
|
|
|
const context = "channel";
|
|
|
|
const interactor = new ChatMessageInteractor(
|
|
|
|
getOwner(this),
|
|
|
|
message,
|
|
|
|
context
|
|
|
|
);
|
|
|
|
|
|
|
|
// assert that the initial secondary actions are present
|
|
|
|
const secondaryActions = interactor.secondaryActions;
|
|
|
|
assert.ok(secondaryActions.length > 0);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// remove the first secondary action listed
|
|
|
|
api.removeChatComposerSecondaryActions(secondaryActions[0].id);
|
|
|
|
|
|
|
|
const updatedSecondaryActions = interactor.secondaryActions;
|
|
|
|
|
|
|
|
// assert that the secondary action was removed
|
|
|
|
assert.ok(
|
|
|
|
updatedSecondaryActions.length < secondaryActions.length,
|
|
|
|
"the updated secondary actions must contain less items than the original"
|
|
|
|
);
|
|
|
|
assert.notOk(
|
|
|
|
updatedSecondaryActions
|
|
|
|
.map((v) => v.id)
|
|
|
|
.includes(secondaryActions[0]),
|
|
|
|
"the updated secondary actions must not include the removed action"
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
// reset the secondary actions removed to prevent leakage to other tests
|
|
|
|
resetRemovedChatComposerSecondaryActions();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-05-30 00:41:12 +08:00
|
|
|
});
|