DEV: Clean up QUnit tests (#13328)

* DEV: Use `query` helper instead of `queryAll()[0]`
* DEV: Replace `queryAll().length` w/ `exists()`/`count()`
* DEV: Use `exists()` instead of `count() > 0`, `count() === 0`
* DEV: Use `count()`/`exists()` instead of `find().length`
This commit is contained in:
Jarek Radosz
2021-06-08 17:54:12 +02:00
committed by GitHub
parent 9811a1c5d9
commit 21e8a33177
98 changed files with 976 additions and 1070 deletions

View File

@ -1,4 +1,8 @@
import { acceptance, queryAll } from "discourse/tests/helpers/qunit-helpers";
import {
acceptance,
query,
queryAll,
} from "discourse/tests/helpers/qunit-helpers";
import I18n from "I18n";
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
import selectKit from "discourse/tests/helpers/select-kit-helper";
@ -26,7 +30,7 @@ acceptance("Details Button", function (needs) {
await fillIn(".d-editor-input", "This is my title");
const textarea = queryAll(".d-editor-input")[0];
const textarea = query(".d-editor-input");
textarea.selectionStart = 0;
textarea.selectionEnd = textarea.value.length;
@ -115,7 +119,7 @@ acceptance("Details Button", function (needs) {
await click("#create-topic");
await fillIn(".d-editor-input", multilineInput);
const textarea = queryAll(".d-editor-input")[0];
const textarea = query(".d-editor-input");
textarea.selectionStart = 0;
textarea.selectionEnd = textarea.value.length;

View File

@ -1,4 +1,9 @@
import { acceptance, queryAll } from "discourse/tests/helpers/qunit-helpers";
import {
acceptance,
count,
query,
queryAll,
} from "discourse/tests/helpers/qunit-helpers";
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
acceptance("Poll breakdown", function (needs) {
@ -69,19 +74,19 @@ acceptance("Poll breakdown", function (needs) {
await click(".poll-show-breakdown");
assert.equal(
queryAll(".poll-breakdown-total-votes")[0].textContent.trim(),
query(".poll-breakdown-total-votes").textContent.trim(),
"2 votes",
"display the correct total vote count"
);
assert.equal(
queryAll(".poll-breakdown-chart-container").length,
count(".poll-breakdown-chart-container"),
2,
"renders a chart for each of the groups in group_results response"
);
assert.ok(
queryAll(".poll-breakdown-chart-container > canvas")[0].$chartjs,
query(".poll-breakdown-chart-container > canvas").$chartjs,
"$chartjs is defined on the pie charts"
);
});
@ -91,7 +96,7 @@ acceptance("Poll breakdown", function (needs) {
await click(".poll-show-breakdown");
assert.equal(
queryAll(".poll-breakdown-option-count")[0].textContent.trim(),
query(".poll-breakdown-option-count").textContent.trim(),
"40.0%",
"displays the correct vote percentage"
);
@ -99,7 +104,7 @@ acceptance("Poll breakdown", function (needs) {
await click(".modal-tabs .count");
assert.equal(
queryAll(".poll-breakdown-option-count")[0].textContent.trim(),
query(".poll-breakdown-option-count").textContent.trim(),
"2",
"displays the correct vote count"
);
@ -107,8 +112,8 @@ acceptance("Poll breakdown", function (needs) {
await click(".modal-tabs .percentage");
assert.equal(
queryAll(".poll-breakdown-option-count:last")[0].textContent.trim(),
"20.0%",
query(".poll-breakdown-option-count").textContent.trim(),
"40.0%",
"displays the percentage again"
);
});

View File

@ -1,4 +1,8 @@
import { acceptance, queryAll } from "discourse/tests/helpers/qunit-helpers";
import {
acceptance,
query,
queryAll,
} from "discourse/tests/helpers/qunit-helpers";
acceptance("Rendering polls with pie charts", function (needs) {
needs.user();
@ -10,10 +14,10 @@ acceptance("Rendering polls with pie charts", function (needs) {
test("Displays the pie chart", async function (assert) {
await visit("/t/-/topic_with_pie_chart_poll");
const poll = queryAll(".poll")[0];
const poll = query(".poll");
assert.equal(
queryAll(".info-number", poll)[0].innerHTML,
query(".info-number", poll).innerHTML,
"2",
"it should display the right number of voters"
);

View File

@ -1,4 +1,4 @@
import { acceptance, queryAll } from "discourse/tests/helpers/qunit-helpers";
import { acceptance, count } from "discourse/tests/helpers/qunit-helpers";
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
acceptance("Poll quote", function (needs) {
@ -427,7 +427,7 @@ acceptance("Poll quote", function (needs) {
test("renders and extends", async function (assert) {
await visit("/t/-/topic_with_two_quoted_polls");
await click(".quote-controls");
assert.equal(queryAll(".poll").length, 2, "polls are rendered");
assert.equal(queryAll(".poll-buttons").length, 2, "polls are extended");
assert.equal(count(".poll"), 2, "polls are rendered");
assert.equal(count(".poll-buttons"), 2, "polls are extended");
});
});

View File

@ -4,7 +4,7 @@ import {
} from "discourse/tests/helpers/widget-test";
import EmberObject from "@ember/object";
import I18n from "I18n";
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
import { count, exists, queryAll } from "discourse/tests/helpers/qunit-helpers";
let requests = 0;
@ -100,7 +100,7 @@ widgetTest("can vote", {
await click("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']");
assert.equal(requests, 1);
assert.equal(queryAll(".chosen").length, 1);
assert.equal(count(".chosen"), 1);
assert.equal(queryAll(".chosen").text(), "100%yes");
assert.equal(queryAll(".toggle-results").text(), "Show vote");
@ -152,6 +152,6 @@ widgetTest("cannot vote if not member of the right group", {
I18n.t("poll.results.groups.title", { groups: "foo" })
);
assert.equal(requests, 0);
assert.equal(queryAll(".chosen").length, 0);
assert.ok(!exists(".chosen"));
},
});