discourse/plugins/poll/test/javascripts/acceptance/polls-bar-chart-test-desktop.js
Alan Guo Xiang Tan 913fd3a7b3
DEV: Improve addToolbarPopupMenuOptionsCallback plugin api (#23769)
Why this change?

Previously just using the `addToolbarPopupMenuOptionsCallback` plugin
API itself was insufficient because it required the return object to
include an `action` key which only accepted a name of the action
function as a string. This was highly problematic because the action
function had to be defined on the `composer` service which means using
the `modifyClass` API to add the action function. This made the API
awkward to use leading to poor developer experiencec.

What does this change do?

This commit introduces a couple of improvemnts to the API.

1. First the API has been renamed to `addComposerToolbarPopupMenuOption` because
   the API no longer accepts a callback function which was quite
   redundant. Instead, it now accepts an Object. The
   `addToolbarPopupMenuOptionsCallback` API function is deprecated and
   will be dropped in Discourse 3.3. Note that passing the API a
   function is still supported but will be dropped when the `addToolbarPopupMenuOptionsCallback`
   is removed.

2. The `action` key in the Object passed to the function can now be a
   function and is passed the `toolbarEvent` object when called.

3. The `condition` on key in the Object passed to the function can now be a
   function and is passed the `composer` service when called.
2023-10-06 07:43:40 +08:00

112 lines
2.9 KiB
JavaScript

import { acceptance, queryAll } from "discourse/tests/helpers/qunit-helpers";
import { test } from "qunit";
import { click, visit } from "@ember/test-helpers";
acceptance("Rendering polls with bar charts - desktop", function (needs) {
needs.user();
needs.settings({ poll_enabled: true });
needs.pretender((server, helper) => {
server.get("/polls/voters.json", (request) => {
let body = {};
if (
request.queryParams.option_id === "68b434ff88aeae7054e42cd05a4d9056"
) {
body = {
voters: {
"68b434ff88aeae7054e42cd05a4d9056": [
{
id: 777,
username: "bruce777",
avatar_template: "/images/avatar.png",
name: "Bruce Wayne",
},
],
},
};
} else {
body = {
voters: Array.from(new Array(5), (_, i) => ({
id: 600 + i,
username: `bruce${600 + i}`,
avatar_template: "/images/avatar.png",
name: "Bruce Wayne",
})),
};
}
return helper.response(body);
});
});
test("Polls", async function (assert) {
await visit("/t/-/15");
const polls = queryAll(".poll");
assert.strictEqual(polls.length, 2, "it should render the polls correctly");
assert.strictEqual(
queryAll(".info-number", polls[0]).text(),
"2",
"it should display the right number of votes"
);
assert.strictEqual(
queryAll(".info-number", polls[1]).text(),
"3",
"it should display the right number of votes"
);
});
test("Public poll", async function (assert) {
await visit("/t/-/14");
const polls = queryAll(".poll");
assert.strictEqual(polls.length, 1, "it should render the poll correctly");
await click("button.toggle-results");
assert.strictEqual(
queryAll(".poll-voters:nth-of-type(1) li").length,
25,
"it should display the right number of voters"
);
await click(".poll-voters-toggle-expand:nth-of-type(1) a");
assert.strictEqual(
queryAll(".poll-voters:nth-of-type(1) li").length,
26,
"it should display the right number of voters"
);
});
test("Public number poll", async function (assert) {
await visit("/t/-/13");
const polls = queryAll(".poll");
assert.strictEqual(polls.length, 1, "it should render the poll correctly");
await click("button.toggle-results");
assert.strictEqual(
queryAll(".poll-voters:nth-of-type(1) li").length,
25,
"it should display the right number of voters"
);
assert.notOk(
queryAll(".poll-voters:nth-of-type(1) li:nth-of-type(1) a").attr("href"),
"user URL does not exist"
);
await click(".poll-voters-toggle-expand:nth-of-type(1) a");
assert.strictEqual(
queryAll(".poll-voters:nth-of-type(1) li").length,
30,
"it should display the right number of voters"
);
});
});