REFACTOR: Replace global find with queryAll

In newer Embers jQuery is removed. There is a `find` but it only returns
one element and not a jQuery selector. This patch migrates our code to a
new helper `queryAll` which allows us to remove the global.
This commit is contained in:
Robin Ward
2020-10-28 16:36:01 -04:00
parent c750a02f05
commit 435a9913a4
135 changed files with 1343 additions and 1025 deletions

View File

@@ -1,5 +1,6 @@
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
acceptance("Poll breakdown", function (needs) {
needs.user();
@@ -61,7 +62,7 @@ acceptance("Poll breakdown", function (needs) {
await visit("/t/-/topic_with_pie_chart_poll");
assert.equal(
find(".poll-show-breakdown").text(),
queryAll(".poll-show-breakdown").text(),
"Show breakdown",
"shows the breakdown button when poll_groupable_user_fields is non-empty"
);
@@ -69,19 +70,19 @@ acceptance("Poll breakdown", function (needs) {
await click(".poll-show-breakdown:first");
assert.equal(
find(".poll-breakdown-total-votes")[0].textContent.trim(),
queryAll(".poll-breakdown-total-votes")[0].textContent.trim(),
"2 votes",
"display the correct total vote count"
);
assert.equal(
find(".poll-breakdown-chart-container").length,
queryAll(".poll-breakdown-chart-container").length,
2,
"renders a chart for each of the groups in group_results response"
);
assert.ok(
find(".poll-breakdown-chart-container > canvas")[0].$chartjs,
queryAll(".poll-breakdown-chart-container > canvas")[0].$chartjs,
"$chartjs is defined on the pie charts"
);
});
@@ -91,7 +92,7 @@ acceptance("Poll breakdown", function (needs) {
await click(".poll-show-breakdown:first");
assert.equal(
find(".poll-breakdown-option-count:first")[0].textContent.trim(),
queryAll(".poll-breakdown-option-count:first")[0].textContent.trim(),
"40.0%",
"displays the correct vote percentage"
);
@@ -99,7 +100,7 @@ acceptance("Poll breakdown", function (needs) {
await click(".modal-tabs .count");
assert.equal(
find(".poll-breakdown-option-count:first")[0].textContent.trim(),
queryAll(".poll-breakdown-option-count:first")[0].textContent.trim(),
"2",
"displays the correct vote count"
);
@@ -107,7 +108,7 @@ acceptance("Poll breakdown", function (needs) {
await click(".modal-tabs .percentage");
assert.equal(
find(".poll-breakdown-option-count:last")[0].textContent.trim(),
queryAll(".poll-breakdown-option-count:last")[0].textContent.trim(),
"20.0%",
"displays the percentage again"
);

View File

@@ -6,6 +6,7 @@ import {
} from "discourse/tests/helpers/qunit-helpers";
import { displayPollBuilderButton } from "discourse/plugins/poll/helpers/display-poll-builder-button";
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
acceptance("Poll Builder - polls are enabled", function (needs) {
needs.user();
@@ -57,11 +58,11 @@ acceptance("Poll Builder - polls are enabled", function (needs) {
await fillIn(".poll-textarea textarea", "First option\nSecond option");
assert.equal(
find(".d-editor-preview li:first-child").text(),
queryAll(".d-editor-preview li:first-child").text(),
"First option"
);
assert.equal(
find(".d-editor-preview li:last-child").text(),
queryAll(".d-editor-preview li:last-child").text(),
"Second option"
);
});

View File

@@ -1,4 +1,5 @@
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
acceptance("Rendering polls with pie charts", function (needs) {
needs.user();
@@ -10,16 +11,16 @@ acceptance("Rendering polls with pie charts", function (needs) {
test("Displays the pie chart", async (assert) => {
await visit("/t/-/topic_with_pie_chart_poll");
const poll = find(".poll")[0];
const poll = queryAll(".poll")[0];
assert.equal(
find(".info-number", poll)[0].innerHTML,
queryAll(".info-number", poll)[0].innerHTML,
"2",
"it should display the right number of voters"
);
assert.equal(
find(".info-number", poll)[1].innerHTML,
queryAll(".info-number", poll)[1].innerHTML,
"5",
"it should display the right number of votes"
);
@@ -31,7 +32,7 @@ acceptance("Rendering polls with pie charts", function (needs) {
);
assert.equal(
find(".poll-results-chart", poll).length,
queryAll(".poll-results-chart", poll).length,
1,
"Renders the chart div instead of bar container"
);

View File

@@ -1,5 +1,6 @@
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
acceptance("Poll quote", function (needs) {
needs.user();
@@ -678,6 +679,6 @@ acceptance("Poll quote", function (needs) {
test("Quoted polls", async (assert) => {
await visit("/t/-/topic_with_two_quoted_polls");
await click(".quote-controls");
assert.equal(find(".poll").length, 2);
assert.equal(queryAll(".poll").length, 2);
});
});

View File

@@ -1,5 +1,6 @@
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
acceptance("Rendering polls with bar charts - desktop", function (needs) {
needs.user();
@@ -42,18 +43,18 @@ acceptance("Rendering polls with bar charts - desktop", function (needs) {
test("Polls", async (assert) => {
await visit("/t/-/15");
const polls = find(".poll");
const polls = queryAll(".poll");
assert.equal(polls.length, 2, "it should render the polls correctly");
assert.equal(
find(".info-number", polls[0]).text(),
queryAll(".info-number", polls[0]).text(),
"2",
"it should display the right number of votes"
);
assert.equal(
find(".info-number", polls[1]).text(),
queryAll(".info-number", polls[1]).text(),
"3",
"it should display the right number of votes"
);
@@ -62,13 +63,13 @@ acceptance("Rendering polls with bar charts - desktop", function (needs) {
test("Public poll", async (assert) => {
await visit("/t/-/14");
const polls = find(".poll");
const polls = queryAll(".poll");
assert.equal(polls.length, 1, "it should render the poll correctly");
await click("button.toggle-results");
assert.equal(
find(".poll-voters:first li").length,
queryAll(".poll-voters:first li").length,
25,
"it should display the right number of voters"
);
@@ -76,7 +77,7 @@ acceptance("Rendering polls with bar charts - desktop", function (needs) {
await click(".poll-voters-toggle-expand:first a");
assert.equal(
find(".poll-voters:first li").length,
queryAll(".poll-voters:first li").length,
26,
"it should display the right number of voters"
);
@@ -85,26 +86,26 @@ acceptance("Rendering polls with bar charts - desktop", function (needs) {
test("Public number poll", async (assert) => {
await visit("/t/-/13");
const polls = find(".poll");
const polls = queryAll(".poll");
assert.equal(polls.length, 1, "it should render the poll correctly");
await click("button.toggle-results");
assert.equal(
find(".poll-voters:first li").length,
queryAll(".poll-voters:first li").length,
25,
"it should display the right number of voters"
);
assert.notOk(
find(".poll-voters:first li:first a").attr("href"),
queryAll(".poll-voters:first li:first a").attr("href"),
"user URL does not exist"
);
await click(".poll-voters-toggle-expand:first a");
assert.equal(
find(".poll-voters:first li").length,
queryAll(".poll-voters:first li").length,
30,
"it should display the right number of voters"
);

View File

@@ -1,5 +1,6 @@
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
acceptance("Rendering polls with bar charts - mobile", function (needs) {
needs.user();
@@ -24,26 +25,26 @@ acceptance("Rendering polls with bar charts - mobile", function (needs) {
test("Public number poll", async (assert) => {
await visit("/t/-/13");
const polls = find(".poll");
const polls = queryAll(".poll");
assert.equal(polls.length, 1, "it should render the poll correctly");
await click("button.toggle-results");
assert.equal(
find(".poll-voters:first li").length,
queryAll(".poll-voters:first li").length,
25,
"it should display the right number of voters"
);
assert.notOk(
find(".poll-voters:first li:first a").attr("href"),
queryAll(".poll-voters:first li:first a").attr("href"),
"user URL does not exist"
);
await click(".poll-voters-toggle-expand:first a");
assert.equal(
find(".poll-voters:first li").length,
queryAll(".poll-voters:first li").length,
35,
"it should display the right number of voters"
);

View File

@@ -1,4 +1,9 @@
import { moduleForWidget, widgetTest } from "discourse/tests/helpers/widget-test";
import {
moduleForWidget,
widgetTest,
} from "discourse/tests/helpers/widget-test";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
moduleForWidget("discourse-poll-option");
const template = `{{mount-widget
@@ -14,7 +19,7 @@ widgetTest("single, not selected", {
},
test(assert) {
assert.ok(find("li .d-icon-far-circle:eq(0)").length === 1);
assert.ok(queryAll("li .d-icon-far-circle:eq(0)").length === 1);
},
});
@@ -27,7 +32,7 @@ widgetTest("single, selected", {
},
test(assert) {
assert.ok(find("li .d-icon-circle:eq(0)").length === 1);
assert.ok(queryAll("li .d-icon-circle:eq(0)").length === 1);
},
});
@@ -43,7 +48,7 @@ widgetTest("multi, not selected", {
},
test(assert) {
assert.ok(find("li .d-icon-far-square:eq(0)").length === 1);
assert.ok(queryAll("li .d-icon-far-square:eq(0)").length === 1);
},
});
@@ -59,6 +64,6 @@ widgetTest("multi, selected", {
},
test(assert) {
assert.ok(find("li .d-icon-far-check-square:eq(0)").length === 1);
assert.ok(queryAll("li .d-icon-far-check-square:eq(0)").length === 1);
},
});

View File

@@ -1,5 +1,9 @@
import EmberObject from "@ember/object";
import { moduleForWidget, widgetTest } from "discourse/tests/helpers/widget-test";
import {
moduleForWidget,
widgetTest,
} from "discourse/tests/helpers/widget-test";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
moduleForWidget("discourse-poll-standard-results");
@@ -21,8 +25,8 @@ widgetTest("options in descending order", {
},
test(assert) {
assert.equal(find(".option .percentage:eq(0)").text(), "56%");
assert.equal(find(".option .percentage:eq(1)").text(), "44%");
assert.equal(queryAll(".option .percentage:eq(0)").text(), "56%");
assert.equal(queryAll(".option .percentage:eq(1)").text(), "44%");
},
});
@@ -40,8 +44,8 @@ widgetTest("options in ascending order", {
},
test(assert) {
assert.equal(find(".option .percentage:eq(0)").text(), "56%");
assert.equal(find(".option .percentage:eq(1)").text(), "44%");
assert.equal(queryAll(".option .percentage:eq(0)").text(), "56%");
assert.equal(queryAll(".option .percentage:eq(1)").text(), "44%");
},
});
@@ -67,12 +71,12 @@ widgetTest("multiple options in descending order", {
},
test(assert) {
assert.equal(find(".option .percentage:eq(0)").text(), "41%");
assert.equal(find(".option .percentage:eq(1)").text(), "33%");
assert.equal(find(".option .percentage:eq(2)").text(), "16%");
assert.equal(find(".option .percentage:eq(3)").text(), "8%");
assert.equal(find(".option span:nth-child(2):eq(3)").text(), "a");
assert.equal(find(".option .percentage:eq(4)").text(), "8%");
assert.equal(find(".option span:nth-child(2):eq(4)").text(), "b");
assert.equal(queryAll(".option .percentage:eq(0)").text(), "41%");
assert.equal(queryAll(".option .percentage:eq(1)").text(), "33%");
assert.equal(queryAll(".option .percentage:eq(2)").text(), "16%");
assert.equal(queryAll(".option .percentage:eq(3)").text(), "8%");
assert.equal(queryAll(".option span:nth-child(2):eq(3)").text(), "a");
assert.equal(queryAll(".option .percentage:eq(4)").text(), "8%");
assert.equal(queryAll(".option span:nth-child(2):eq(4)").text(), "b");
},
});

View File

@@ -4,6 +4,7 @@ import {
moduleForWidget,
widgetTest,
} from "discourse/tests/helpers/widget-test";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
let requests = 0;
@@ -99,16 +100,17 @@ widgetTest("can vote", {
await click("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']");
assert.equal(requests, 1);
assert.equal(find(".chosen").length, 1);
assert.equal(find(".chosen").text(), "100%yes");
assert.equal(find(".toggle-results").text(), "Show vote");
assert.equal(queryAll(".chosen").length, 1);
assert.equal(queryAll(".chosen").text(), "100%yes");
assert.equal(queryAll(".toggle-results").text(), "Show vote");
await click(".toggle-results");
assert.equal(
find("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']").length,
queryAll("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']")
.length,
1
);
assert.equal(find(".toggle-results").text(), "Show results");
assert.equal(queryAll(".toggle-results").text(), "Show results");
},
});
@@ -146,10 +148,10 @@ widgetTest("cannot vote if not member of the right group", {
await click("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']");
assert.equal(
find(".poll-container .alert").text(),
queryAll(".poll-container .alert").text(),
I18n.t("poll.results.groups.title", { groups: "foo" })
);
assert.equal(requests, 0);
assert.equal(find(".chosen").length, 0);
assert.equal(queryAll(".chosen").length, 0);
},
});