Skip to content

Commit c13e79d

Browse files
authored
FIX: Query downloads were being passed an incorrect query object. (#359)
This is a follow-up to d726c48. The previous change missed changing the name of the query object when passing it to QueryResultsWrapper, which resulted in the download links not working properly after a query was run. This change fixes that bug, and includes an acceptance test to ensure it stays fixed during future work on this plugin.
1 parent 6dc695c commit c13e79d

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

assets/javascripts/discourse/controllers/admin-plugins-explorer-index.js

-5
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ export default class PluginsExplorerController extends Controller {
150150
this.showCreate = true;
151151
}
152152

153-
@action
154-
resetParams() {
155-
this.selectedItem.resetParams();
156-
}
157-
158153
@action
159154
updateSortProperty(property) {
160155
if (this.sortByProperty === property) {

assets/javascripts/discourse/templates/admin/plugins-explorer-queries-details.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
<QueryResultsWrapper
218218
@results={{this.results}}
219219
@showResults={{this.showResults}}
220-
@query={{this.selectedItem}}
220+
@query={{this.model}}
221221
@content={{this.results}}
222222
/>
223223
{{/if}}

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

+83
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { click, visit } from "@ember/test-helpers";
22
import { test } from "qunit";
3+
import sinon from "sinon";
34
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
45
import { i18n } from "discourse-i18n";
56

67
acceptance("Data Explorer Plugin | Run Query", function (needs) {
78
needs.user();
89
needs.settings({ data_explorer_enabled: true });
910

11+
needs.hooks.beforeEach(() => {
12+
sinon.stub(window, "open");
13+
});
14+
15+
needs.hooks.afterEach(() => {
16+
window.open.restore();
17+
});
18+
1019
needs.pretender((server, helper) => {
1120
server.get("/admin/plugins/explorer/groups.json", () => {
1221
return helper.response([
@@ -203,6 +212,12 @@ acceptance("Data Explorer Plugin | Run Query", function (needs) {
203212
rows: [[0, null, false]],
204213
});
205214
});
215+
216+
server.get("/session/csrf.json", function () {
217+
return helper.response({
218+
csrf: "mgk906YLagHo2gOgM1ddYjAN4hQolBdJCqlY6jYzAYs= ",
219+
});
220+
});
206221
});
207222

208223
test("runs query and renders data and a chart", async function (assert) {
@@ -233,6 +248,74 @@ acceptance("Data Explorer Plugin | Run Query", function (needs) {
233248
assert.dom("canvas").exists("the chart was rendered");
234249
});
235250

251+
test("runs query and is able to download the results", async function (assert) {
252+
await visit("/admin/plugins/explorer/queries/-6");
253+
254+
await click("form.query-run button");
255+
256+
const createElement = document.createElement.bind(document);
257+
const appendChild = document.body.appendChild.bind(document.body);
258+
const removeChild = document.body.removeChild.bind(document.body);
259+
260+
const finishedForm = sinon.promise();
261+
262+
let formElement;
263+
264+
const formStub = sinon
265+
.stub(document, "createElement")
266+
.callsFake((tagName) => {
267+
if (tagName === "form") {
268+
formElement = {
269+
fakeForm: true,
270+
setAttribute: sinon.stub(),
271+
appendChild: sinon.stub(),
272+
submit: sinon.stub().callsFake(finishedForm.resolve),
273+
};
274+
275+
return formElement;
276+
}
277+
278+
return createElement(tagName);
279+
});
280+
281+
const appendChildStub = sinon
282+
.stub(document.body, "appendChild")
283+
.callsFake((el) => {
284+
if (!el.fakeForm) {
285+
return appendChild(el);
286+
}
287+
});
288+
289+
const removeChildStub = sinon
290+
.stub(document.body, "removeChild")
291+
.callsFake((el) => {
292+
if (!el.fakeForm) {
293+
return removeChild(el);
294+
}
295+
});
296+
297+
await click("div.result-info button:nth-child(1)");
298+
299+
await finishedForm;
300+
301+
formStub.restore();
302+
appendChildStub.restore();
303+
removeChildStub.restore();
304+
305+
assert.ok(window.open.called, "window.open was called for downloading");
306+
assert.ok(formStub.called, "form was created for downloading");
307+
assert.ok(formElement.submit.called, "form was submitted for downloading");
308+
309+
assert.ok(
310+
formElement.setAttribute.calledWith("action"),
311+
"form action attribute was set"
312+
);
313+
assert.ok(
314+
formElement.setAttribute.calledWith("method", "post"),
315+
"form method attribute was set to POST"
316+
);
317+
});
318+
236319
test("runs query and renders 0, false, and NULL values correctly", async function (assert) {
237320
await visit("/admin/plugins/explorer/queries/2");
238321

0 commit comments

Comments
 (0)