DEV: Load plugin CSS in tests (#18668)

This commit is contained in:
David Taylor 2022-10-19 18:10:06 +01:00 committed by GitHub
parent a96f22cd67
commit 9a4072fe31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 68 additions and 25 deletions

View File

@ -429,6 +429,8 @@ module.exports = {
`<script src="${config.rootURL}assets/plugins/test/${directoryName}_tests.js" data-discourse-plugin="${pluginName}"></script>` `<script src="${config.rootURL}assets/plugins/test/${directoryName}_tests.js" data-discourse-plugin="${pluginName}"></script>`
) )
.join("\n"); .join("\n");
} else if (shouldLoadPluginTestJs() && type === "test-plugin-css") {
return `<link rel="stylesheet" href="${config.rootURL}bootstrap/plugin-css-for-tests.css" data-discourse-plugin="_all" />`;
} }
}, },

View File

@ -3,7 +3,8 @@ const dynamicJsTemplate = document.querySelector("#dynamic-test-js");
const params = new URLSearchParams(document.location.search); const params = new URLSearchParams(document.location.search);
const skipPlugins = params.get("qunit_skip_plugins"); const skipPlugins = params.get("qunit_skip_plugins");
for (const element of dynamicJsTemplate.content.childNodes) { (async function setup() {
for (const element of dynamicJsTemplate.content.childNodes) {
if (skipPlugins && element.dataset?.discoursePlugin) { if (skipPlugins && element.dataset?.discoursePlugin) {
continue; continue;
} }
@ -25,4 +26,9 @@ for (const element of dynamicJsTemplate.content.childNodes) {
} }
document.querySelector("discourse-dynamic-test-js").appendChild(clone); document.querySelector("discourse-dynamic-test-js").appendChild(clone);
}
if (clone.tagName === "LINK" && clone["rel"] === "stylesheet") {
await new Promise((resolve) => (clone.onload = resolve));
}
}
})();

View File

@ -105,5 +105,11 @@ if (process.argv.includes("-t")) {
"/plugins/": { "/plugins/": {
target, target,
}, },
"/bootstrap/plugin-css-for-tests.css": {
target,
},
"/stylesheets/": {
target,
},
}; };
} }

View File

@ -52,6 +52,7 @@
<script src="{{rootURL}}assets/wizard.js"></script> <script src="{{rootURL}}assets/wizard.js"></script>
<template id="dynamic-test-js"> <template id="dynamic-test-js">
{{content-for "test-plugin-css"}}
{{content-for "test-plugin-js"}} {{content-for "test-plugin-js"}}
<script defer src="{{rootURL}}assets/test-helpers.js"></script> <script defer src="{{rootURL}}assets/test-helpers.js"></script>
<script defer src="{{rootURL}}assets/core-tests.js"></script> <script defer src="{{rootURL}}assets/core-tests.js"></script>

View File

@ -3,7 +3,7 @@
class BootstrapController < ApplicationController class BootstrapController < ApplicationController
include ApplicationHelper include ApplicationHelper
skip_before_action :redirect_to_login_if_required skip_before_action :redirect_to_login_if_required, :check_xhr
# This endpoint allows us to produce the data required to start up Discourse via JSON API, # This endpoint allows us to produce the data required to start up Discourse via JSON API,
# so that you don't have to scrape the HTML for `data-*` payloads # so that you don't have to scrape the HTML for `data-*` payloads
@ -98,6 +98,23 @@ class BootstrapController < ApplicationController
render_json_dump(bootstrap: bootstrap) render_json_dump(bootstrap: bootstrap)
end end
def plugin_css_for_tests
urls = Discourse.find_plugin_css_assets(
include_disabled: true,
desktop_view: true,
).map do |target|
details = Stylesheet::Manager.new().stylesheet_details(target, 'all')
details[0][:new_href]
end
stylesheet = <<~CSS
/* For use in tests only - `@import`s all plugin stylesheets */
#{urls.map { |url| "@import \"#{url}\";" }.join("\n") }
CSS
render plain: stylesheet, content_type: 'text/css'
end
private private
def add_scheme(scheme_id, media, css_class) def add_scheme(scheme_id, media, css_class)
return if scheme_id.to_i == -1 return if scheme_id.to_i == -1

View File

@ -17,6 +17,9 @@ Discourse::Application.routes.draw do
get "/404-body" => "exceptions#not_found_body" get "/404-body" => "exceptions#not_found_body"
get "/bootstrap" => "bootstrap#index" get "/bootstrap" => "bootstrap#index"
if Rails.env.test? || Rails.env.development?
get "/bootstrap/plugin-css-for-tests.css" => "bootstrap#plugin_css_for_tests"
end
post "webhooks/aws" => "webhooks#aws" post "webhooks/aws" => "webhooks#aws"
post "webhooks/mailgun" => "webhooks#mailgun" post "webhooks/mailgun" => "webhooks#mailgun"

View File

@ -1,7 +1,12 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { click, render } from "@ember/test-helpers"; import { click, render } from "@ember/test-helpers";
import { count, exists, query } from "discourse/tests/helpers/qunit-helpers"; import {
count,
exists,
query,
queryAll,
} from "discourse/tests/helpers/qunit-helpers";
import hbs from "htmlbars-inline-precompile"; import hbs from "htmlbars-inline-precompile";
import pretender, { response } from "discourse/tests/helpers/create-pretender"; import pretender, { response } from "discourse/tests/helpers/create-pretender";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
@ -85,7 +90,10 @@ module("Integration | Component | Widget | discourse-poll", function (hooks) {
await click("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']"); await click("li[data-poll-option-id='1f972d1df351de3ce35a787c89faad29']");
assert.strictEqual(requests, 1); assert.strictEqual(requests, 1);
assert.strictEqual(count(".chosen"), 1); assert.strictEqual(count(".chosen"), 1);
assert.strictEqual(query(".chosen").innerText, "100%yes"); assert.deepEqual(
Array.from(queryAll(".chosen span")).map((span) => span.innerText),
["100%", "yes"]
);
await click(".toggle-results"); await click(".toggle-results");
assert.strictEqual( assert.strictEqual(