Skip to content

A platform for community discussion. Free, open, simple.

FIX: Hovering a poll breakdown option threw an error (#44602)

Jarek Radoszc419f9ae6fcd
diff --git a/frontend/discourse/app/lib/load-chart-js.js b/frontend/discourse/app/lib/load-chart-js.js
index f5af8182931..a31bf68ef9a 100644
--- a/frontend/discourse/app/lib/load-chart-js.js
+++ b/frontend/discourse/app/lib/load-chart-js.js
@@ -6,10 +6,17 @@ export default async function loadChartJS() {
}
/**
- *
- * @returns {import("chart.js").Plugin}
+ * @returns {Promise<import("chart.js").Plugin>}
*/
export async function loadChartJSDatalabels() {
return (await waitForPromise(import("discourse/static/chart-js-bundle")))
.ChartDataLabels;
}
+
+/**
+ * @returns {Promise<typeof import("chart.js/helpers").getHoverColor>}
+ */
+export async function loadChartJSGetHoverColor() {
+ return (await waitForPromise(import("discourse/static/chart-js-bundle")))
+ .getHoverColor;
+}
diff --git a/frontend/discourse/app/static/chart-js-bundle.js b/frontend/discourse/app/static/chart-js-bundle.js
index c4bdeed2753..386b55e1b91 100644
--- a/frontend/discourse/app/static/chart-js-bundle.js
+++ b/frontend/discourse/app/static/chart-js-bundle.js
@@ -2,3 +2,4 @@ import "chartjs-adapter-moment";
export { default as Chart } from "chart.js/auto";
export { default as ChartDataLabels } from "chartjs-plugin-datalabels";
+export { getHoverColor } from "chart.js/helpers";
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-breakdown-option.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-breakdown-option.gjs
index d32b9f62f5b..e1d0be17c48 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-breakdown-option.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-breakdown-option.gjs
@@ -4,7 +4,7 @@ import { on } from "@ember/modifier";
import { computed } from "@ember/object";
import { trustHTML } from "@ember/template";
import { tagName } from "@ember-decorators/component";
-import loadChartJS from "discourse/lib/load-chart-js";
+import { loadChartJSGetHoverColor } from "discourse/lib/load-chart-js";
import { deepEqual } from "discourse/lib/object";
import I18n, { i18n } from "discourse-i18n";
import { getColors } from "discourse/plugins/poll/lib/chart-colors";
@@ -24,8 +24,8 @@ export default class PollBreakdownOption extends Component {
constructor() {
super(...arguments);
- loadChartJS().then((Chart) => {
- this.set("Chart", Chart);
+ loadChartJSGetHoverColor().then((getHoverColor) => {
+ this.set("getHoverColor", getHoverColor);
});
}
@@ -59,13 +59,14 @@ export default class PollBreakdownOption extends Component {
}
}
- @computed("Chart", "highlighted", "optionColors", "index")
+ @computed("getHoverColor", "highlighted", "optionColors", "index")
get colorPreviewStyle() {
- const color = this.highlighted
- ? this.Chart?.helpers.getHoverColor(this.optionColors[this.index])
- : this.optionColors[this.index];
+ const color = this.optionColors[this.index];
+ const previewColor = this.highlighted
+ ? (this.getHoverColor?.(color) ?? color)
+ : color;
- return trustHTML(`background: ${color};`);
+ return trustHTML(`background: ${previewColor};`);
}
<template>
diff --git a/plugins/poll/test/javascripts/component/poll-breakdown-test.gjs b/plugins/poll/test/javascripts/component/poll-breakdown-test.gjs
new file mode 100644
index 00000000000..155cbe911bc
--- /dev/null
+++ b/plugins/poll/test/javascripts/component/poll-breakdown-test.gjs
@@ -0,0 +1,48 @@
+import { getOwner } from "@ember/owner";
+import { render, settled, triggerEvent } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import ModalContainer from "discourse/components/modal-container";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
+import PollBreakdownModal from "discourse/plugins/poll/discourse/components/modal/poll-breakdown";
+
+module("Poll | Component | PollBreakdownModal", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("uses the hover color for the highlighted option", async function (assert) {
+ this.siteSettings.poll_groupable_user_fields = "occupation";
+ pretender.get("/polls/grouped_poll_results.json", () =>
+ response({ grouped_results: [] })
+ );
+ const model = {
+ post: { id: 1, topic: { title: "A topic" } },
+ poll: {
+ name: "poll",
+ title: "Favorite color",
+ voters: 4,
+ options: [
+ { id: "a", html: "Red", votes: 1 },
+ { id: "b", html: "Blue", votes: 3 },
+ ],
+ },
+ };
+ await render(<template><ModalContainer /></template>);
+ getOwner(this).lookup("service:modal").show(PollBreakdownModal, { model });
+ await settled();
+
+ const swatch =
+ ".poll-breakdown-option:nth-child(2) .poll-breakdown-option-color";
+ const baseStyle = document.querySelector(swatch).getAttribute("style");
+
+ await triggerEvent(".poll-breakdown-option:nth-child(2)", "mouseover");
+
+ assert
+ .dom(swatch)
+ .hasAttribute("style", /^background: (?!undefined)\S+;$/, "has a color");
+ assert.notStrictEqual(
+ document.querySelector(swatch).getAttribute("style"),
+ baseStyle,
+ "differs from the base color"
+ );
+ });
+});