|
1 | 1 | import { click, visit } from "@ember/test-helpers";
|
2 | 2 | import { test } from "qunit";
|
| 3 | +import sinon from "sinon"; |
3 | 4 | import { acceptance } from "discourse/tests/helpers/qunit-helpers";
|
4 | 5 | import { i18n } from "discourse-i18n";
|
5 | 6 |
|
6 | 7 | acceptance("Data Explorer Plugin | Run Query", function (needs) {
|
7 | 8 | needs.user();
|
8 | 9 | needs.settings({ data_explorer_enabled: true });
|
9 | 10 |
|
| 11 | + needs.hooks.beforeEach(() => { |
| 12 | + sinon.stub(window, "open"); |
| 13 | + }); |
| 14 | + |
| 15 | + needs.hooks.afterEach(() => { |
| 16 | + window.open.restore(); |
| 17 | + }); |
| 18 | + |
10 | 19 | needs.pretender((server, helper) => {
|
11 | 20 | server.get("/admin/plugins/explorer/groups.json", () => {
|
12 | 21 | return helper.response([
|
@@ -203,6 +212,12 @@ acceptance("Data Explorer Plugin | Run Query", function (needs) {
|
203 | 212 | rows: [[0, null, false]],
|
204 | 213 | });
|
205 | 214 | });
|
| 215 | + |
| 216 | + server.get("/session/csrf.json", function () { |
| 217 | + return helper.response({ |
| 218 | + csrf: "mgk906YLagHo2gOgM1ddYjAN4hQolBdJCqlY6jYzAYs= ", |
| 219 | + }); |
| 220 | + }); |
206 | 221 | });
|
207 | 222 |
|
208 | 223 | test("runs query and renders data and a chart", async function (assert) {
|
@@ -233,6 +248,74 @@ acceptance("Data Explorer Plugin | Run Query", function (needs) {
|
233 | 248 | assert.dom("canvas").exists("the chart was rendered");
|
234 | 249 | });
|
235 | 250 |
|
| 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 | + |
236 | 319 | test("runs query and renders 0, false, and NULL values correctly", async function (assert) {
|
237 | 320 | await visit("/admin/plugins/explorer/queries/2");
|
238 | 321 |
|
|
0 commit comments