From 9a4072fe319ab89fdc14e0f205822503fa6444f1 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 19 Oct 2022 18:10:06 +0100 Subject: [PATCH] DEV: Load plugin CSS in tests (#18668) --- .../discourse/lib/bootstrap-json/index.js | 2 + .../scripts/discourse-test-load-dynamic-js.js | 50 +++++++++++-------- app/assets/javascripts/discourse/testem.js | 6 +++ .../javascripts/discourse/tests/index.html | 1 + app/controllers/bootstrap_controller.rb | 19 ++++++- config/routes.rb | 3 ++ .../widgets/discourse-poll-test.js | 12 ++++- 7 files changed, 68 insertions(+), 25 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js index b2b243fdc56..1c778d80a02 100644 --- a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js +++ b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js @@ -429,6 +429,8 @@ module.exports = { `` ) .join("\n"); + } else if (shouldLoadPluginTestJs() && type === "test-plugin-css") { + return ``; } }, diff --git a/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js index fc0237c6225..8041b5141bd 100644 --- a/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js +++ b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js @@ -3,26 +3,32 @@ const dynamicJsTemplate = document.querySelector("#dynamic-test-js"); const params = new URLSearchParams(document.location.search); const skipPlugins = params.get("qunit_skip_plugins"); -for (const element of dynamicJsTemplate.content.childNodes) { - if (skipPlugins && element.dataset?.discoursePlugin) { - continue; +(async function setup() { + for (const element of dynamicJsTemplate.content.childNodes) { + if (skipPlugins && element.dataset?.discoursePlugin) { + continue; + } + + if ( + element.tagName === "SCRIPT" && + element.innerHTML.includes("EmberENV.TESTS_FILE_LOADED") + ) { + // Inline script introduced by ember-cli. Incompatible with CSP and our custom plugin JS loading system + // https://github.com/ember-cli/ember-cli/blob/04a38fda2c/lib/utilities/ember-app-utils.js#L131 + // We re-implement in test-boot-ember-cli.js + continue; + } + + const clone = element.cloneNode(true); + + if (clone.tagName === "SCRIPT") { + clone.async = false; + } + + document.querySelector("discourse-dynamic-test-js").appendChild(clone); + + if (clone.tagName === "LINK" && clone["rel"] === "stylesheet") { + await new Promise((resolve) => (clone.onload = resolve)); + } } - - if ( - element.tagName === "SCRIPT" && - element.innerHTML.includes("EmberENV.TESTS_FILE_LOADED") - ) { - // Inline script introduced by ember-cli. Incompatible with CSP and our custom plugin JS loading system - // https://github.com/ember-cli/ember-cli/blob/04a38fda2c/lib/utilities/ember-app-utils.js#L131 - // We re-implement in test-boot-ember-cli.js - continue; - } - - const clone = element.cloneNode(true); - - if (clone.tagName === "SCRIPT") { - clone.async = false; - } - - document.querySelector("discourse-dynamic-test-js").appendChild(clone); -} +})(); diff --git a/app/assets/javascripts/discourse/testem.js b/app/assets/javascripts/discourse/testem.js index fbc39514183..1bcc31d2cda 100644 --- a/app/assets/javascripts/discourse/testem.js +++ b/app/assets/javascripts/discourse/testem.js @@ -105,5 +105,11 @@ if (process.argv.includes("-t")) { "/plugins/": { target, }, + "/bootstrap/plugin-css-for-tests.css": { + target, + }, + "/stylesheets/": { + target, + }, }; } diff --git a/app/assets/javascripts/discourse/tests/index.html b/app/assets/javascripts/discourse/tests/index.html index 3277f0b1cc6..c286633609f 100644 --- a/app/assets/javascripts/discourse/tests/index.html +++ b/app/assets/javascripts/discourse/tests/index.html @@ -52,6 +52,7 @@