Skip to content

Commit f62d8cc

Browse files
authoredAug 28, 2024··
DEV: Update tests (#323)
new imports, resolved deprecations, qunit-dom, strict assertions, fixed flakes, migrated the legacy component test, typos, gjs
1 parent ba9ead8 commit f62d8cc

File tree

10 files changed

+374
-501
lines changed

10 files changed

+374
-501
lines changed
 

‎assets/javascripts/discourse/components/query-result.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ function randomIdShort() {
295295

296296
function transformedRelTable(table, modelClass) {
297297
const result = {};
298-
table.forEach((item) => {
298+
table?.forEach((item) => {
299299
if (modelClass) {
300300
result[item.id] = modelClass.create(item);
301301
} else {

‎test/javascripts/acceptance/list-queries-test.js

+27-42
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
import { visit } from "@ember/test-helpers";
22
import { test } from "qunit";
3-
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
4-
import {
5-
acceptance,
6-
count,
7-
exists,
8-
query,
9-
} from "discourse/tests/helpers/qunit-helpers";
10-
import I18n from "I18n";
3+
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
4+
import I18n from "discourse-i18n";
115

126
acceptance("Data Explorer Plugin | List Queries", function (needs) {
137
needs.user();
148
needs.settings({ data_explorer_enabled: true });
15-
needs.hooks.beforeEach(() => {
16-
clearPopupMenuOptionsCallback();
17-
});
189

1910
needs.pretender((server, helper) => {
2011
server.get("/admin/plugins/explorer/groups.json", () => {
@@ -142,44 +133,38 @@ acceptance("Data Explorer Plugin | List Queries", function (needs) {
142133
});
143134
});
144135

145-
test("it renders the page with the list of queries", async function (assert) {
136+
test("renders the page with the list of queries", async function (assert) {
146137
await visit("/admin/plugins/explorer");
147138

148-
assert.ok(
149-
query("div.query-list input.ember-text-field").placeholder ===
139+
assert
140+
.dom("div.query-list input.ember-text-field")
141+
.hasAttribute(
142+
"placeholder",
150143
I18n.t("explorer.search_placeholder"),
151-
"the search box was rendered"
152-
);
144+
"the search box was rendered"
145+
);
153146

154-
assert.ok(
155-
exists("div.query-list button.btn-icon svg.d-icon-plus"),
156-
"the add query button was rendered"
157-
);
147+
assert
148+
.dom("div.query-list button.btn-icon svg.d-icon-plus")
149+
.exists("the add query button was rendered");
158150

159-
assert.ok(
160-
query("div.query-list button.btn-icon-text span.d-button-label")
161-
.innerText === I18n.t("explorer.import.label"),
162-
"the import button was rendered"
163-
);
151+
assert
152+
.dom("div.query-list button.btn-icon-text span.d-button-label")
153+
.hasText(
154+
I18n.t("explorer.import.label"),
155+
"the import button was rendered"
156+
);
164157

165-
assert.equal(
166-
count("div.container table.recent-queries tbody tr"),
167-
2,
168-
"the list of queries was rendered"
169-
);
158+
assert
159+
.dom("div.container table.recent-queries tbody tr")
160+
.exists({ count: 2 }, "the list of queries was rendered");
170161

171-
assert.ok(
172-
query(
173-
"div.container table.recent-queries tbody tr:nth-child(1) td a"
174-
).innerText.startsWith("Top 100 Likers"),
175-
"The first query was rendered"
176-
);
162+
assert
163+
.dom("div.container table.recent-queries tbody tr:nth-child(1) td a")
164+
.hasText(/^\s*Top 100 Likers/, "The first query was rendered");
177165

178-
assert.ok(
179-
query(
180-
"div.container table.recent-queries tbody tr:nth-child(2) td a"
181-
).innerText.startsWith("Top 100 Active Topics"),
182-
"The second query was rendered"
183-
);
166+
assert
167+
.dom("div.container table.recent-queries tbody tr:nth-child(2) td a")
168+
.hasText(/^\s*Top 100 Active Topics/, "The second query was rendered");
184169
});
185170
});

‎test/javascripts/acceptance/new-query-test.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { click, currentURL, fillIn, visit } from "@ember/test-helpers";
22
import { test } from "qunit";
3-
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
43
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
54

65
acceptance("Data Explorer Plugin | New Query", function (needs) {
76
needs.user();
87
needs.settings({ data_explorer_enabled: true });
9-
needs.hooks.beforeEach(() => {
10-
clearPopupMenuOptionsCallback();
11-
});
128

139
needs.pretender((server, helper) => {
1410
server.get("/admin/plugins/explorer/groups.json", () => {
@@ -61,6 +57,6 @@ acceptance("Data Explorer Plugin | New Query", function (needs) {
6157
// select create new query button
6258
await click(".query-create button");
6359

64-
assert.equal(currentURL(), "/admin/plugins/explorer?id=-15");
60+
assert.strictEqual(currentURL(), "/admin/plugins/explorer?id=-15");
6561
});
6662
});

‎test/javascripts/acceptance/param-input-test.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { click, currentURL, fillIn, visit } from "@ember/test-helpers";
22
import { test } from "qunit";
3-
import {
4-
acceptance,
5-
exists,
6-
query,
7-
} from "discourse/tests/helpers/qunit-helpers";
3+
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
84

95
acceptance("Data Explorer Plugin | Param Input", function (needs) {
106
needs.user();
@@ -327,6 +323,7 @@ acceptance("Data Explorer Plugin | Param Input", function (needs) {
327323
},
328324
});
329325
});
326+
330327
server.put("/admin/plugins/explorer/queries/3", () => {
331328
return helper.response({
332329
query: {
@@ -353,58 +350,58 @@ acceptance("Data Explorer Plugin | Param Input", function (needs) {
353350
});
354351
});
355352

356-
test("it puts params for the query into the url", async function (assert) {
353+
test("puts params for the query into the url", async function (assert) {
357354
await visit("/admin/plugins/explorer?id=-6");
358355
const monthsAgoValue = "2";
359356
await fillIn(".query-params input", monthsAgoValue);
360357
await click("form.query-run button");
361358

362359
const searchParams = new URLSearchParams(currentURL().split("?")[1]);
363360
const monthsAgoParam = JSON.parse(searchParams.get("params")).months_ago;
364-
assert.equal(monthsAgoParam, monthsAgoValue);
361+
assert.strictEqual(monthsAgoParam, monthsAgoValue);
365362
});
366363

367-
test("it puts params for the query into the url for group reports", async function (assert) {
364+
test("puts params for the query into the url for group reports", async function (assert) {
368365
await visit("/g/discourse/reports/-8");
369366
const monthsAgoValue = "2";
370367
await fillIn(".query-params input", monthsAgoValue);
371368
await click("form.query-run button");
372369

373370
const searchParams = new URLSearchParams(currentURL().split("?")[1]);
374371
const monthsAgoParam = JSON.parse(searchParams.get("params")).months_ago;
375-
assert.equal(monthsAgoParam, monthsAgoValue);
372+
assert.strictEqual(monthsAgoParam, monthsAgoValue);
376373
});
377374

378-
test("it loads the page if one of the parameter is null", async function (assert) {
375+
test("loads the page if one of the parameter is null", async function (assert) {
379376
await visit('/admin/plugins/explorer?id=-7&params={"user":null}');
380-
assert.ok(exists(".query-params .user-chooser"));
381-
assert.ok(exists(".query-run .btn.btn-primary"));
377+
assert.dom(".query-params .user-chooser").exists();
378+
assert.dom(".query-run .btn.btn-primary").exists();
382379
});
383380

384-
test("it loads the page if one of the parameter is null for group reports", async function (assert) {
381+
test("loads the page if one of the parameter is null for group reports", async function (assert) {
385382
await visit('/g/discourse/reports/-8?params={"months_ago":null}');
386-
assert.ok(exists(".query-params input"));
387-
assert.ok(exists(".query-run .btn.btn-primary"));
383+
assert.dom(".query-params input").exists();
384+
assert.dom(".query-run .btn.btn-primary").exists();
388385
});
389386

390-
test("it applies params when running a report", async function (assert) {
387+
test("applies params when running a report", async function (assert) {
391388
await visit("/g/discourse/reports/-8");
392389
const monthsAgoValue = "2";
393390
await fillIn(".query-params input", monthsAgoValue);
394391
await click("form.query-run button");
395-
assert.equal(query(".query-params input").value, monthsAgoValue);
392+
assert.dom(".query-params input").hasValue(monthsAgoValue);
396393
});
397394

398-
test("it creates input boxes if has parameters when save", async function (assert) {
395+
test("creates input boxes if has parameters when save", async function (assert) {
399396
await visit("/admin/plugins/explorer?id=3");
400-
assert.notOk(exists(".query-params input"));
397+
assert.dom(".query-params input").doesNotExist();
401398
await click(".query-edit .btn-edit-query");
402399
await click(".query-editor .ace_text-input");
403400
await fillIn(
404401
".query-editor .ace_text-input",
405402
"-- [params]\n-- int :months_ago = 1\n\nSELECT 1"
406403
);
407404
await click(".query-edit .btn-save-query");
408-
assert.ok(exists(".query-params input"));
405+
assert.dom(".query-params input").exists();
409406
});
410407
});

‎test/javascripts/acceptance/run-query-test.js

+35-58
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
import { click, visit } from "@ember/test-helpers";
22
import { test } from "qunit";
3-
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
4-
import {
5-
acceptance,
6-
exists,
7-
query,
8-
queryAll,
9-
} from "discourse/tests/helpers/qunit-helpers";
10-
import I18n from "I18n";
3+
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
4+
import I18n from "discourse-i18n";
115

126
acceptance("Data Explorer Plugin | Run Query", function (needs) {
137
needs.user();
148
needs.settings({ data_explorer_enabled: true });
15-
needs.hooks.beforeEach(() => {
16-
clearPopupMenuOptionsCallback();
17-
});
189

1910
needs.pretender((server, helper) => {
2011
server.get("/admin/plugins/explorer/groups.json", () => {
@@ -185,71 +176,57 @@ acceptance("Data Explorer Plugin | Run Query", function (needs) {
185176
});
186177
});
187178

188-
test("it runs query and renders data and a chart", async function (assert) {
179+
test("runs query and renders data and a chart", async function (assert) {
189180
await visit("/admin/plugins/explorer?id=-6");
190181

191-
assert.ok(
192-
query("div.name h1").innerText.trim() === "Top 100 Likers",
193-
"the query name was rendered"
194-
);
182+
assert
183+
.dom("div.name h1")
184+
.hasText("Top 100 Likers", "the query name was rendered");
195185

196-
assert.ok(exists("div.query-edit"), "the query code was rendered");
186+
assert.dom("div.query-edit").exists("the query code was rendered");
197187

198-
assert.ok(
199-
query("form.query-run button span").innerText.trim() ===
200-
I18n.t("explorer.run"),
201-
"the run button was rendered"
202-
);
188+
assert
189+
.dom("form.query-run button span")
190+
.hasText(I18n.t("explorer.run"), "the run button was rendered");
203191

204192
await click("form.query-run button");
205193

206-
assert.ok(
207-
queryAll("div.query-results table tbody tr").length === 2,
208-
"the table with query results was rendered"
209-
);
194+
assert
195+
.dom("div.query-results table tbody tr")
196+
.exists({ count: 2 }, "the table with query results was rendered");
210197

211-
assert.ok(
212-
query("div.result-info button:nth-child(3) span").innerText.trim() ===
213-
I18n.t("explorer.show_graph"),
214-
"the chart button was rendered"
215-
);
198+
assert
199+
.dom("div.result-info button:nth-child(3) span")
200+
.hasText(I18n.t("explorer.show_graph"), "the chart button was rendered");
216201

217202
await click("div.result-info button:nth-child(3)");
218203

219-
assert.ok(exists("canvas"), "the chart was rendered");
204+
assert.dom("canvas").exists("the chart was rendered");
220205
});
221206

222-
test("it runs query and renders 0, false, and NULL values correctly", async function (assert) {
207+
test("runs query and renders 0, false, and NULL values correctly", async function (assert) {
223208
await visit("/admin/plugins/explorer?id=2");
224209

225-
assert.ok(
226-
query("div.name h1").innerText.trim() === "What about 0?",
227-
"the query name was rendered"
228-
);
210+
assert
211+
.dom("div.name h1")
212+
.hasText("What about 0?", "the query name was rendered");
229213

230-
assert.ok(
231-
query("form.query-run button span").innerText.trim() ===
232-
I18n.t("explorer.run"),
233-
"the run button was rendered"
234-
);
214+
assert
215+
.dom("form.query-run button span")
216+
.hasText(I18n.t("explorer.run"), "the run button was rendered");
235217

236218
await click("form.query-run button");
237219

238-
assert.ok(
239-
query("div.query-results tbody td:nth-child(1)").innerText.trim() === "0",
240-
"renders '0' values"
241-
);
242-
243-
assert.ok(
244-
query("div.query-results tbody td:nth-child(2)").innerText.trim() ===
245-
"NULL",
246-
"renders 'NULL' values"
247-
);
248-
249-
assert.ok(
250-
query("div.query-results tbody td:nth-child(3)").innerText.trim() ===
251-
"false",
252-
"renders 'false' values"
253-
);
220+
assert
221+
.dom("div.query-results tbody td:nth-child(1)")
222+
.hasText("0", "renders '0' values");
223+
224+
assert
225+
.dom("div.query-results tbody td:nth-child(2)")
226+
.hasText("NULL", "renders 'NULL' values");
227+
228+
assert
229+
.dom("div.query-results tbody td:nth-child(3)")
230+
.hasText("false", "renders 'false' values");
254231
});
255232
});

‎test/javascripts/components/param-input-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ module("Data Explorer Plugin | Component | param-input", function (hooks) {
276276
assert.rejects(this.submit());
277277
});
278278

279-
test("async normalizion", async function (assert) {
279+
test("async normalization", async function (assert) {
280280
this.setProperties({
281281
param_info: [
282282
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { array } from "@ember/helper";
2+
import { render } from "@ember/test-helpers";
3+
import { module, test } from "qunit";
4+
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
5+
import DataExplorerBarChart from "../../discourse/components/data-explorer-bar-chart";
6+
7+
module(
8+
"Data Explorer Plugin | Integration | Component | data-explorer-bar-chart",
9+
function (hooks) {
10+
setupRenderingTest(hooks);
11+
12+
test("renders a chart", async function (assert) {
13+
await render(<template>
14+
<DataExplorerBarChart
15+
@labels={{array "label_1" "label_2"}}
16+
@values={{array 115 1000}}
17+
@datasetName="data"
18+
/>
19+
</template>);
20+
21+
assert.dom("canvas").exists("renders a canvas");
22+
});
23+
}
24+
);

‎test/javascripts/integration/components/data-explorer-bar-chart-test.js

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
import { click, render } from "@ember/test-helpers";
2+
import { module, test } from "qunit";
3+
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
4+
import I18n from "discourse-i18n";
5+
import QueryResult from "../../discourse/components/query-result";
6+
7+
module(
8+
"Data Explorer Plugin | Integration | Component | query-result",
9+
function (hooks) {
10+
setupRenderingTest(hooks);
11+
12+
test("renders query results", async function (assert) {
13+
const content = {
14+
colrender: [],
15+
result_count: 2,
16+
columns: ["user_name", "like_count"],
17+
rows: [
18+
["user1", 10],
19+
["user2", 20],
20+
],
21+
};
22+
23+
await render(<template><QueryResult @content={{content}} /></template>);
24+
25+
assert
26+
.dom("div.result-info button:nth-child(1) span")
27+
.hasText(I18n.t("explorer.download_json"), "renders the JSON button");
28+
29+
assert
30+
.dom("div.result-info button:nth-child(2) span")
31+
.hasText(I18n.t("explorer.download_csv"), "renders the CSV button");
32+
33+
assert
34+
.dom("div.result-info button:nth-child(3) span")
35+
.hasText(I18n.t("explorer.show_graph"), "renders the chart button");
36+
37+
assert.dom("div.result-about").exists("renders a query summary");
38+
39+
assert.dom("table thead tr th:nth-child(1)").hasText("user_name");
40+
assert.dom("table thead tr th:nth-child(2)").hasText("like_count");
41+
assert
42+
.dom("table tbody tr:nth-child(1) td:nth-child(1)")
43+
.hasText("user1");
44+
assert.dom("table tbody tr:nth-child(1) td:nth-child(2)").hasText("10");
45+
assert
46+
.dom("table tbody tr:nth-child(2) td:nth-child(1)")
47+
.hasText("user2");
48+
assert.dom("table tbody tr:nth-child(2) td:nth-child(2)").hasText("20");
49+
});
50+
51+
test("renders badge names in query results", async function (assert) {
52+
const content = {
53+
colrender: { 0: "badge" },
54+
relations: {
55+
badge: [
56+
{
57+
description: "description",
58+
icon: "fa-user",
59+
id: 1,
60+
name: "badge name",
61+
display_name: "badge display name",
62+
},
63+
],
64+
},
65+
result_count: 1,
66+
columns: ["badge_id"],
67+
rows: [[1]],
68+
};
69+
70+
await render(<template><QueryResult @content={{content}} /></template>);
71+
72+
assert
73+
.dom("table tbody tr:nth-child(1) td:nth-child(1) span")
74+
.hasText("badge display name");
75+
});
76+
77+
test("renders a post in query results", async function (assert) {
78+
const content = {
79+
colrender: { 0: "post" },
80+
relations: {
81+
post: [
82+
{
83+
description: "description",
84+
id: 1,
85+
topic_id: 1,
86+
post_number: 1,
87+
excerpt: "foo",
88+
username: "user1",
89+
avatar_template: "",
90+
},
91+
],
92+
},
93+
result_count: 1,
94+
columns: [""],
95+
rows: [[1]],
96+
};
97+
98+
await render(<template><QueryResult @content={{content}} /></template>);
99+
100+
assert
101+
.dom("table tbody tr:nth-child(1) td:nth-child(1) aside")
102+
.hasAttribute("data-post", "1");
103+
assert
104+
.dom("table tbody tr:nth-child(1) td:nth-child(1) aside")
105+
.hasAttribute("data-topic", "1");
106+
});
107+
108+
test("renders a category_id in query results", async function (assert) {
109+
const content = {
110+
colrender: { 0: "category" },
111+
relations: {
112+
category: [
113+
{
114+
id: 1,
115+
name: "foo",
116+
slug: "foo",
117+
topic_count: 0,
118+
position: 1,
119+
description: "a category",
120+
can_edit: true,
121+
},
122+
],
123+
},
124+
result_count: 1,
125+
columns: [""],
126+
rows: [[1]],
127+
};
128+
129+
await render(<template><QueryResult @content={{content}} /></template>);
130+
131+
assert
132+
.dom(
133+
"table tbody tr:nth-child(1) td:nth-child(1) .badge-category__name"
134+
)
135+
.exists();
136+
});
137+
}
138+
);
139+
140+
module(
141+
"Data Explorer Plugin | Integration | Component | query-result | chart",
142+
function (hooks) {
143+
setupRenderingTest(hooks);
144+
145+
test("navigation between a table and a chart works", async function (assert) {
146+
const content = {
147+
colrender: [],
148+
result_count: 2,
149+
columns: ["user_name", "like_count"],
150+
rows: [
151+
["user1", 10],
152+
["user2", 20],
153+
],
154+
};
155+
156+
await render(<template><QueryResult @content={{content}} /></template>);
157+
158+
assert
159+
.dom("div.result-info button:nth-child(3) span")
160+
.hasText(
161+
I18n.t("explorer.show_graph"),
162+
"the chart button was rendered"
163+
);
164+
assert.dom("table").exists("the table was rendered");
165+
166+
await click("div.result-info button:nth-child(3)");
167+
168+
assert
169+
.dom("div.result-info button:nth-child(3) span")
170+
.hasText(
171+
I18n.t("explorer.show_table"),
172+
"the chart button was changed to the table button"
173+
);
174+
assert.dom("canvas").exists("the chart was rendered");
175+
176+
await click("div.result-info button:nth-child(3)");
177+
assert
178+
.dom("div.result-info button:nth-child(3) span")
179+
.hasText(
180+
I18n.t("explorer.show_graph"),
181+
"the table button was changed to the chart button"
182+
);
183+
assert.dom("table").exists("the table was rendered");
184+
});
185+
186+
test("renders a chart button when data has two columns and numbers in the second column", async function (assert) {
187+
const content = {
188+
colrender: [],
189+
result_count: 2,
190+
columns: ["user_name", "like_count"],
191+
rows: [
192+
["user1", 10],
193+
["user2", 20],
194+
],
195+
};
196+
197+
await render(<template><QueryResult @content={{content}} /></template>);
198+
199+
assert
200+
.dom("div.result-info button:nth-child(3) span")
201+
.hasText(I18n.t("explorer.show_graph"));
202+
});
203+
204+
test("doesn't render a chart button when data contains identifiers in the second column", async function (assert) {
205+
const content = {
206+
colrender: { 1: "user" },
207+
relations: {
208+
user: [
209+
{ id: 1, username: "user1" },
210+
{ id: 2, username: "user2" },
211+
],
212+
},
213+
result_count: 2,
214+
columns: ["topic_id", "user_id"],
215+
rows: [
216+
[1, 10],
217+
[2, 20],
218+
],
219+
};
220+
221+
await render(<template><QueryResult @content={{content}} /></template>);
222+
223+
assert.dom("div.result-info button:nth-child(3)").doesNotExist();
224+
});
225+
226+
test("doesn't render a chart button when data contains one column", async function (assert) {
227+
const content = {
228+
colrender: [],
229+
result_count: 2,
230+
columns: ["user_name"],
231+
rows: [["user1"], ["user2"]],
232+
};
233+
234+
await render(<template><QueryResult @content={{content}} /></template>);
235+
236+
assert.dom("div.result-info button:nth-child(3)").doesNotExist();
237+
});
238+
239+
test("doesn't render a chart button when data contains more than two columns", async function (assert) {
240+
const content = {
241+
colrender: [],
242+
result_count: 2,
243+
columns: ["user_name", "like_count", "post_count"],
244+
rows: [
245+
["user1", 10, 1],
246+
["user2", 20, 2],
247+
],
248+
};
249+
250+
await render(<template><QueryResult @content={{content}} /></template>);
251+
252+
assert.dom("div.result-info button:nth-child(3)").doesNotExist();
253+
});
254+
255+
test("handles no results", async function (assert) {
256+
const content = {
257+
colrender: [],
258+
result_count: 0,
259+
columns: ["user_name", "like_count", "post_count"],
260+
rows: [],
261+
};
262+
263+
await render(<template><QueryResult @content={{content}} /></template>);
264+
265+
assert.dom("table tbody tr").doesNotExist("renders no results");
266+
});
267+
}
268+
);

‎test/javascripts/integration/components/query-result-test.js

-348
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.