mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
This reverts commita53d8d3e61and105634435f. Reverted because the change broke some components. Will be added back in a few days.
This commit is contained in:
@@ -127,18 +127,4 @@ describe ThemeJavascriptCompiler do
|
||||
expect(compiler.content.to_s).to include("addRawTemplate(\"#{name}.hbs\"")
|
||||
end
|
||||
end
|
||||
|
||||
describe "#append_ember_template" do
|
||||
let(:compiler) { ThemeJavascriptCompiler.new(1, 'marks') }
|
||||
it 'prepends `javascripts/` to template name if it is not prepended' do
|
||||
compiler.append_ember_template("/connectors/blah-1", "{{var}}")
|
||||
expect(compiler.content.to_s).to include('Ember.TEMPLATES["javascripts/connectors/blah-1"]')
|
||||
|
||||
compiler.append_ember_template("connectors/blah-2", "{{var}}")
|
||||
expect(compiler.content.to_s).to include('Ember.TEMPLATES["javascripts/connectors/blah-2"]')
|
||||
|
||||
compiler.append_ember_template("javascripts/connectors/blah-3", "{{var}}")
|
||||
expect(compiler.content.to_s).to include('Ember.TEMPLATES["javascripts/connectors/blah-3"]')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -40,7 +40,6 @@ describe RemoteTheme do
|
||||
"stylesheets/file.scss" => ".class1{color:red}",
|
||||
"stylesheets/empty.scss" => "",
|
||||
"javascripts/discourse/controllers/test.js.es6" => "console.log('test');",
|
||||
"test/acceptance/theme-test.js" => "assert.ok(true);",
|
||||
"common/header.html" => "I AM HEADER",
|
||||
"common/random.html" => "I AM SILLY",
|
||||
"common/embedded.scss" => "EMBED",
|
||||
@@ -75,7 +74,7 @@ describe RemoteTheme do
|
||||
|
||||
expect(@theme.theme_modifier_set.serialize_topic_excerpts).to eq(true)
|
||||
|
||||
expect(@theme.theme_fields.length).to eq(11)
|
||||
expect(@theme.theme_fields.length).to eq(10)
|
||||
|
||||
mapped = Hash[*@theme.theme_fields.map { |f| ["#{f.target_id}-#{f.name}", f.value] }.flatten]
|
||||
|
||||
@@ -89,9 +88,8 @@ describe RemoteTheme do
|
||||
expect(mapped["3-yaml"]).to eq("boolean_setting: true")
|
||||
|
||||
expect(mapped["4-en"]).to eq("sometranslations")
|
||||
expect(mapped["7-acceptance/theme-test.js"]).to eq("assert.ok(true);")
|
||||
|
||||
expect(mapped.length).to eq(11)
|
||||
expect(mapped.length).to eq(10)
|
||||
|
||||
expect(@theme.settings.length).to eq(1)
|
||||
expect(@theme.settings.first.value).to eq(true)
|
||||
|
||||
@@ -192,22 +192,34 @@ HTML
|
||||
unknown_field = theme.set_field(target: :extra_js, name: "discourse/controllers/discovery.blah", value: "this wont work")
|
||||
theme.save!
|
||||
|
||||
js_field.reload
|
||||
expect(js_field.value_baked).to include("if ('define' in window) {")
|
||||
expect(js_field.value_baked).to include("define(\"discourse/theme-#{theme.id}/controllers/discovery\"")
|
||||
expect(js_field.value_baked).to include("console.log('hello from .js.es6');")
|
||||
expected_js = <<~JS
|
||||
define("discourse/controllers/discovery", ["discourse/lib/ajax"], function (_ajax) {
|
||||
"use strict";
|
||||
|
||||
expect(hbs_field.reload.value_baked).to include('Ember.TEMPLATES["javascripts/discovery"]')
|
||||
var __theme_name__ = "#{theme.name}";
|
||||
|
||||
var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id});
|
||||
|
||||
var themePrefix = function themePrefix(key) {
|
||||
return "theme_translations.#{theme.id}.".concat(key);
|
||||
};
|
||||
|
||||
console.log('hello from .js.es6');
|
||||
});
|
||||
JS
|
||||
expect(js_field.reload.value_baked).to eq(expected_js.strip)
|
||||
|
||||
expect(hbs_field.reload.value_baked).to include('Ember.TEMPLATES["discovery"]')
|
||||
expect(raw_hbs_field.reload.value_baked).to include('addRawTemplate("discovery"')
|
||||
expect(hbr_field.reload.value_baked).to include('addRawTemplate("other_discovery"')
|
||||
expect(unknown_field.reload.value_baked).to eq("")
|
||||
expect(unknown_field.reload.error).to eq(I18n.t("themes.compile_error.unrecognized_extension", extension: "blah"))
|
||||
|
||||
# All together
|
||||
expect(theme.javascript_cache.content).to include('Ember.TEMPLATES["javascripts/discovery"]')
|
||||
expect(theme.javascript_cache.content).to include('Ember.TEMPLATES["discovery"]')
|
||||
expect(theme.javascript_cache.content).to include('addRawTemplate("discovery"')
|
||||
expect(theme.javascript_cache.content).to include("define(\"discourse/theme-#{theme.id}/controllers/discovery\"")
|
||||
expect(theme.javascript_cache.content).to include("define(\"discourse/theme-#{theme.id}/controllers/discovery-2\"")
|
||||
expect(theme.javascript_cache.content).to include('define("discourse/controllers/discovery"')
|
||||
expect(theme.javascript_cache.content).to include('define("discourse/controllers/discovery-2"')
|
||||
expect(theme.javascript_cache.content).to include("var settings =")
|
||||
end
|
||||
|
||||
|
||||
@@ -276,7 +276,7 @@ HTML
|
||||
def transpile(html)
|
||||
f = ThemeField.create!(target_id: Theme.targets[:mobile], theme_id: 1, name: "after_header", value: html)
|
||||
f.ensure_baked!
|
||||
[f.value_baked, f.javascript_cache, f]
|
||||
[f.value_baked, f.javascript_cache]
|
||||
end
|
||||
|
||||
it "transpiles ES6 code" do
|
||||
@@ -286,21 +286,10 @@ HTML
|
||||
</script>
|
||||
HTML
|
||||
|
||||
baked, javascript_cache, field = transpile(html)
|
||||
baked, javascript_cache = transpile(html)
|
||||
expect(baked).to include(javascript_cache.url)
|
||||
|
||||
expect(javascript_cache.content).to include("if ('define' in window) {")
|
||||
expect(javascript_cache.content).to include(
|
||||
"define(\"discourse/theme-#{field.theme_id}/initializers/theme-field-#{field.id}-mobile-html-script-1\""
|
||||
)
|
||||
expect(javascript_cache.content).to include(
|
||||
"settings = require(\"discourse/lib/theme-settings-store\").getObjectForTheme(#{field.theme_id});"
|
||||
)
|
||||
expect(javascript_cache.content).to include("name: \"theme-field-#{field.id}-mobile-html-script-1\",")
|
||||
expect(javascript_cache.content).to include("after: \"inject-objects\",")
|
||||
expect(javascript_cache.content).to include("(0, _pluginApi.withPluginApi)(\"0.1\", function (api) {")
|
||||
expect(javascript_cache.content).to include("var x = 1;")
|
||||
expect(javascript_cache.content).to include("(0, _utilities.rescueThemeError)(__theme_name__, err, api);")
|
||||
expect(javascript_cache.content).to include('var x = 1;')
|
||||
expect(javascript_cache.content).to include("_registerPluginCode('0.1'")
|
||||
end
|
||||
|
||||
it "wraps constants calls in a readOnlyError function" do
|
||||
@@ -380,32 +369,83 @@ HTML
|
||||
theme_field = theme.set_field(target: :common, name: :after_header, value: '<script type="text/discourse-plugin" version="1.0">alert(settings.name); let a = ()=>{};</script>')
|
||||
theme.save!
|
||||
|
||||
transpiled = <<~HTML
|
||||
(function() {
|
||||
if ('Discourse' in window && Discourse.__container__) {
|
||||
Discourse.__container__
|
||||
.lookup("service:theme-settings")
|
||||
.registerSettings(#{theme.id}, {"name":"bob"});
|
||||
}
|
||||
})();
|
||||
(function () {
|
||||
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
|
||||
var __theme_name__ = "awesome theme\\\"";
|
||||
|
||||
var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id});
|
||||
|
||||
var themePrefix = function themePrefix(key) {
|
||||
return "theme_translations.#{theme.id}.".concat(key);
|
||||
};
|
||||
|
||||
Discourse._registerPluginCode('1.0', function (api) {
|
||||
try {
|
||||
alert(settings.name);
|
||||
|
||||
var a = function a() {};
|
||||
} catch (err) {
|
||||
var rescue = require("discourse/lib/utilities").rescueThemeError;
|
||||
|
||||
rescue(__theme_name__, err, api);
|
||||
}
|
||||
});
|
||||
}
|
||||
})();
|
||||
HTML
|
||||
|
||||
theme_field.reload
|
||||
expect(Theme.lookup_field(theme.id, :desktop, :after_header)).to include(theme_field.javascript_cache.url)
|
||||
expect(theme_field.javascript_cache.content).to include("if ('require' in window) {")
|
||||
expect(theme_field.javascript_cache.content).to include(
|
||||
"require(\"discourse/lib/theme-settings-store\").registerSettings(#{theme_field.theme.id}, {\"name\":\"bob\"});"
|
||||
)
|
||||
expect(theme_field.javascript_cache.content).to include("if ('define' in window) {")
|
||||
expect(theme_field.javascript_cache.content).to include(
|
||||
"define(\"discourse/theme-#{theme_field.theme.id}/initializers/theme-field-#{theme_field.id}-common-html-script-1\","
|
||||
)
|
||||
expect(theme_field.javascript_cache.content).to include("var __theme_name__ = \"awesome theme\\\"\";")
|
||||
expect(theme_field.javascript_cache.content).to include("name: \"theme-field-#{theme_field.id}-common-html-script-1\",")
|
||||
expect(theme_field.javascript_cache.content).to include("after: \"inject-objects\",")
|
||||
expect(theme_field.javascript_cache.content).to include("(0, _pluginApi.withPluginApi)(\"1.0\", function (api)")
|
||||
expect(theme_field.javascript_cache.content).to include("alert(settings.name)")
|
||||
expect(theme_field.javascript_cache.content).to include("var a = function a() {}")
|
||||
expect(theme_field.javascript_cache.content).to eq(transpiled.strip)
|
||||
|
||||
setting = theme.settings.find { |s| s.name == :name }
|
||||
setting.value = 'bill'
|
||||
theme.save!
|
||||
|
||||
transpiled = <<~HTML
|
||||
(function() {
|
||||
if ('Discourse' in window && Discourse.__container__) {
|
||||
Discourse.__container__
|
||||
.lookup("service:theme-settings")
|
||||
.registerSettings(#{theme.id}, {"name":"bill"});
|
||||
}
|
||||
})();
|
||||
(function () {
|
||||
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
|
||||
var __theme_name__ = "awesome theme\\\"";
|
||||
|
||||
var settings = Discourse.__container__.lookup("service:theme-settings").getObjectForTheme(#{theme.id});
|
||||
|
||||
var themePrefix = function themePrefix(key) {
|
||||
return "theme_translations.#{theme.id}.".concat(key);
|
||||
};
|
||||
|
||||
Discourse._registerPluginCode('1.0', function (api) {
|
||||
try {
|
||||
alert(settings.name);
|
||||
|
||||
var a = function a() {};
|
||||
} catch (err) {
|
||||
var rescue = require("discourse/lib/utilities").rescueThemeError;
|
||||
|
||||
rescue(__theme_name__, err, api);
|
||||
}
|
||||
});
|
||||
}
|
||||
})();
|
||||
HTML
|
||||
|
||||
theme_field.reload
|
||||
expect(theme_field.javascript_cache.content).to include(
|
||||
"require(\"discourse/lib/theme-settings-store\").registerSettings(#{theme_field.theme.id}, {\"name\":\"bill\"});"
|
||||
)
|
||||
expect(Theme.lookup_field(theme.id, :desktop, :after_header)).to include(theme_field.javascript_cache.url)
|
||||
expect(theme_field.javascript_cache.content).to eq(transpiled.strip)
|
||||
end
|
||||
|
||||
it 'is empty when the settings are invalid' do
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe QunitController do
|
||||
let(:theme) { Fabricate(:theme, name: 'main-theme') }
|
||||
let(:component) { Fabricate(:theme, component: true, name: 'enabled-component') }
|
||||
let(:disabled_component) { Fabricate(:theme, component: true, enabled: false, name: 'disabled-component') }
|
||||
|
||||
before do
|
||||
Theme.destroy_all
|
||||
theme.set_default!
|
||||
component.add_relative_theme!(:parent, theme)
|
||||
disabled_component.add_relative_theme!(:parent, theme)
|
||||
[theme, component, disabled_component].each do |t|
|
||||
t.set_field(
|
||||
target: :extra_js,
|
||||
type: :js,
|
||||
name: "discourse/initializers/my-#{t.id}-initializer.js",
|
||||
value: "console.log(#{t.id});"
|
||||
)
|
||||
t.set_field(
|
||||
target: :tests_js,
|
||||
type: :js,
|
||||
name: "acceptance/some-test-#{t.id}.js",
|
||||
value: "assert.ok(#{t.id});"
|
||||
)
|
||||
t.save!
|
||||
end
|
||||
end
|
||||
|
||||
context "when no theme is specified" do
|
||||
it "includes tests of enabled theme + components" do
|
||||
get '/qunit'
|
||||
js_urls = JavascriptCache.where(theme_id: [theme.id, component.id]).map(&:url)
|
||||
expect(js_urls.size).to eq(2)
|
||||
js_urls.each do |url|
|
||||
expect(response.body).to include(url)
|
||||
end
|
||||
[theme, component].each do |t|
|
||||
expect(response.body).to include("/theme-javascripts/tests/#{t.id}.js")
|
||||
end
|
||||
|
||||
js_urls = JavascriptCache.where(theme_id: disabled_component).map(&:url)
|
||||
expect(js_urls.size).to eq(1)
|
||||
js_urls.each do |url|
|
||||
expect(response.body).not_to include(url)
|
||||
end
|
||||
expect(response.body).not_to include("/theme-javascripts/tests/#{disabled_component.id}.js")
|
||||
end
|
||||
end
|
||||
|
||||
context "when a theme is specified" do
|
||||
it "includes tests of the specified theme only" do
|
||||
[theme, disabled_component].each do |t|
|
||||
get "/qunit?theme_name=#{t.name}"
|
||||
js_urls = JavascriptCache.where(theme_id: t.id).map(&:url)
|
||||
expect(js_urls.size).to eq(1)
|
||||
js_urls.each do |url|
|
||||
expect(response.body).to include(url)
|
||||
end
|
||||
expect(response.body).to include("/theme-javascripts/tests/#{t.id}.js")
|
||||
|
||||
excluded = Theme.pluck(:id) - [t.id]
|
||||
js_urls = JavascriptCache.where(theme_id: excluded).map(&:url)
|
||||
expect(js_urls.size).to eq(2)
|
||||
js_urls.each do |url|
|
||||
expect(response.body).not_to include(url)
|
||||
end
|
||||
excluded.each do |id|
|
||||
expect(response.body).not_to include("/theme-javascripts/tests/#{id}.js")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -54,24 +54,4 @@ describe ThemeJavascriptsController do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#show_tests" do
|
||||
context "theme settings" do
|
||||
let(:component) { Fabricate(:theme, component: true, name: 'enabled-component') }
|
||||
|
||||
it "forces default values" do
|
||||
ThemeField.create!(
|
||||
theme: component,
|
||||
target_id: Theme.targets[:settings],
|
||||
name: "yaml",
|
||||
value: "num_setting: 5"
|
||||
)
|
||||
component.reload
|
||||
component.update_setting(:num_setting, 643)
|
||||
|
||||
get "/theme-javascripts/tests/#{component.id}.js"
|
||||
expect(response.body).to include("require(\"discourse/lib/theme-settings-store\").registerSettings(#{component.id}, {\"num_setting\":5}, { force: true });")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user