discourse/plugins/poll/test/javascripts/acceptance/poll-breakdown-test.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

119 lines
3.1 KiB
JavaScript

import {
acceptance,
count,
exists,
query,
} from "discourse/tests/helpers/qunit-helpers";
import { test } from "qunit";
import { click, visit } from "@ember/test-helpers";
acceptance("Poll breakdown", function (needs) {
needs.user();
needs.settings({
poll_enabled: true,
poll_groupable_user_fields: "something",
});
needs.pretender((server, helper) => {
server.get("/polls/grouped_poll_results.json", () =>
helper.response({
grouped_results: [
{
group: "Engineering",
options: [
{
digest: "687a1ccf3c6a260f9aeeb7f68a1d463c",
html: "This Is",
votes: 1,
},
{
digest: "9377906763a1221d31d656ea0c4a4495",
html: "A test for sure",
votes: 1,
},
{
digest: "ecf47c65a85a0bb20029072b1b721977",
html: "Why not give it some more",
votes: 1,
},
],
},
{
group: "Marketing",
options: [
{
digest: "687a1ccf3c6a260f9aeeb7f68a1d463c",
html: "This Is",
votes: 1,
},
{
digest: "9377906763a1221d31d656ea0c4a4495",
html: "A test for sure",
votes: 1,
},
{
digest: "ecf47c65a85a0bb20029072b1b721977",
html: "Why not give it some more",
votes: 1,
},
],
},
],
})
);
});
test("Displaying the poll breakdown modal", async function (assert) {
await visit("/t/-/topic_with_pie_chart_poll");
await click(".widget-dropdown-header");
assert.ok(
exists(".item-showBreakdown"),
"shows the breakdown button when poll_groupable_user_fields is non-empty"
);
await click(".item-showBreakdown");
assert.ok(exists(".poll-breakdown-total-votes"), "displays the vote count");
assert.strictEqual(
count(".poll-breakdown-chart-container"),
2,
"renders a chart for each of the groups in group_results response"
);
assert.ok(
query(".poll-breakdown-chart-container > canvas").$chartjs,
"$chartjs is defined on the pie charts"
);
});
test("Changing the display mode from percentage to count", async function (assert) {
await visit("/t/-/topic_with_pie_chart_poll");
await click(".widget-dropdown-header");
await click(".item-showBreakdown");
assert.strictEqual(
query(".poll-breakdown-option-count").textContent.trim(),
"40.0%",
"displays the correct vote percentage"
);
await click(".modal-tabs .count");
assert.strictEqual(
query(".poll-breakdown-option-count").textContent.trim(),
"2",
"displays the correct vote count"
);
await click(".modal-tabs .percentage");
assert.strictEqual(
query(".poll-breakdown-option-count").textContent.trim(),
"40.0%",
"displays the percentage again"
);
});
});