mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
DEV: Apply syntax_tree formatting to spec/*
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe ThemeJavascriptCompiler do
|
||||
let(:compiler) { ThemeJavascriptCompiler.new(1, 'marks') }
|
||||
let(:compiler) { ThemeJavascriptCompiler.new(1, "marks") }
|
||||
|
||||
describe "#append_raw_template" do
|
||||
it 'uses the correct template paths' do
|
||||
it "uses the correct template paths" do
|
||||
template = "<h1>hello</h1>"
|
||||
name = "/path/to/templates1"
|
||||
compiler.append_raw_template("#{name}.raw", template)
|
||||
@ -21,49 +21,76 @@ RSpec.describe ThemeJavascriptCompiler do
|
||||
end
|
||||
|
||||
describe "#append_ember_template" do
|
||||
it 'maintains module names so that discourse-boot.js can correct them' do
|
||||
it "maintains module names so that discourse-boot.js can correct them" do
|
||||
compiler.append_ember_template("/connectors/blah-1", "{{var}}")
|
||||
expect(compiler.raw_content.to_s).to include("define(\"discourse/theme-1/connectors/blah-1\", [\"exports\", \"@ember/template-factory\"]")
|
||||
expect(compiler.raw_content.to_s).to include(
|
||||
"define(\"discourse/theme-1/connectors/blah-1\", [\"exports\", \"@ember/template-factory\"]",
|
||||
)
|
||||
|
||||
compiler.append_ember_template("connectors/blah-2", "{{var}}")
|
||||
expect(compiler.raw_content.to_s).to include("define(\"discourse/theme-1/connectors/blah-2\", [\"exports\", \"@ember/template-factory\"]")
|
||||
expect(compiler.raw_content.to_s).to include(
|
||||
"define(\"discourse/theme-1/connectors/blah-2\", [\"exports\", \"@ember/template-factory\"]",
|
||||
)
|
||||
|
||||
compiler.append_ember_template("javascripts/connectors/blah-3", "{{var}}")
|
||||
expect(compiler.raw_content.to_s).to include("define(\"discourse/theme-1/javascripts/connectors/blah-3\", [\"exports\", \"@ember/template-factory\"]")
|
||||
expect(compiler.raw_content.to_s).to include(
|
||||
"define(\"discourse/theme-1/javascripts/connectors/blah-3\", [\"exports\", \"@ember/template-factory\"]",
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe "connector module name handling" do
|
||||
it 'separates colocated connectors to avoid module name clash' do
|
||||
it "separates colocated connectors to avoid module name clash" do
|
||||
# Colocated under `/connectors`
|
||||
compiler = ThemeJavascriptCompiler.new(1, 'marks')
|
||||
compiler.append_tree({
|
||||
"connectors/outlet/blah-1.hbs" => "{{var}}",
|
||||
"connectors/outlet/blah-1.js" => "console.log('test')"
|
||||
})
|
||||
compiler = ThemeJavascriptCompiler.new(1, "marks")
|
||||
compiler.append_tree(
|
||||
{
|
||||
"connectors/outlet/blah-1.hbs" => "{{var}}",
|
||||
"connectors/outlet/blah-1.js" => "console.log('test')",
|
||||
},
|
||||
)
|
||||
expect(compiler.raw_content.to_s).to include("discourse/theme-1/connectors/outlet/blah-1")
|
||||
expect(compiler.raw_content.to_s).to include("discourse/theme-1/templates/connectors/outlet/blah-1")
|
||||
expect(JSON.parse(compiler.source_map)["sources"]).to contain_exactly("connectors/outlet/blah-1.js", "templates/connectors/outlet/blah-1.js")
|
||||
expect(compiler.raw_content.to_s).to include(
|
||||
"discourse/theme-1/templates/connectors/outlet/blah-1",
|
||||
)
|
||||
expect(JSON.parse(compiler.source_map)["sources"]).to contain_exactly(
|
||||
"connectors/outlet/blah-1.js",
|
||||
"templates/connectors/outlet/blah-1.js",
|
||||
)
|
||||
|
||||
# Colocated under `/templates/connectors`
|
||||
compiler = ThemeJavascriptCompiler.new(1, 'marks')
|
||||
compiler.append_tree({
|
||||
"templates/connectors/outlet/blah-1.hbs" => "{{var}}",
|
||||
"templates/connectors/outlet/blah-1.js" => "console.log('test')"
|
||||
})
|
||||
compiler = ThemeJavascriptCompiler.new(1, "marks")
|
||||
compiler.append_tree(
|
||||
{
|
||||
"templates/connectors/outlet/blah-1.hbs" => "{{var}}",
|
||||
"templates/connectors/outlet/blah-1.js" => "console.log('test')",
|
||||
},
|
||||
)
|
||||
expect(compiler.raw_content.to_s).to include("discourse/theme-1/connectors/outlet/blah-1")
|
||||
expect(compiler.raw_content.to_s).to include("discourse/theme-1/templates/connectors/outlet/blah-1")
|
||||
expect(JSON.parse(compiler.source_map)["sources"]).to contain_exactly("connectors/outlet/blah-1.js", "templates/connectors/outlet/blah-1.js")
|
||||
expect(compiler.raw_content.to_s).to include(
|
||||
"discourse/theme-1/templates/connectors/outlet/blah-1",
|
||||
)
|
||||
expect(JSON.parse(compiler.source_map)["sources"]).to contain_exactly(
|
||||
"connectors/outlet/blah-1.js",
|
||||
"templates/connectors/outlet/blah-1.js",
|
||||
)
|
||||
|
||||
# Not colocated
|
||||
compiler = ThemeJavascriptCompiler.new(1, 'marks')
|
||||
compiler.append_tree({
|
||||
"templates/connectors/outlet/blah-1.hbs" => "{{var}}",
|
||||
"connectors/outlet/blah-1.js" => "console.log('test')"
|
||||
})
|
||||
compiler = ThemeJavascriptCompiler.new(1, "marks")
|
||||
compiler.append_tree(
|
||||
{
|
||||
"templates/connectors/outlet/blah-1.hbs" => "{{var}}",
|
||||
"connectors/outlet/blah-1.js" => "console.log('test')",
|
||||
},
|
||||
)
|
||||
expect(compiler.raw_content.to_s).to include("discourse/theme-1/connectors/outlet/blah-1")
|
||||
expect(compiler.raw_content.to_s).to include("discourse/theme-1/templates/connectors/outlet/blah-1")
|
||||
expect(JSON.parse(compiler.source_map)["sources"]).to contain_exactly("connectors/outlet/blah-1.js", "templates/connectors/outlet/blah-1.js")
|
||||
expect(compiler.raw_content.to_s).to include(
|
||||
"discourse/theme-1/templates/connectors/outlet/blah-1",
|
||||
)
|
||||
expect(JSON.parse(compiler.source_map)["sources"]).to contain_exactly(
|
||||
"connectors/outlet/blah-1.js",
|
||||
"templates/connectors/outlet/blah-1.js",
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@ -89,11 +116,13 @@ RSpec.describe ThemeJavascriptCompiler do
|
||||
import Component from "@glimmer/component";
|
||||
export default class MyComponent extends Component {}
|
||||
JS
|
||||
"discourse/templates/components/mycomponent.hbs" => "{{my-component-template}}"
|
||||
}
|
||||
"discourse/templates/components/mycomponent.hbs" => "{{my-component-template}}",
|
||||
},
|
||||
)
|
||||
expect(compiler.raw_content).to include('define("discourse/theme-1/components/mycomponent"')
|
||||
expect(compiler.raw_content).to include('define("discourse/theme-1/discourse/templates/components/mycomponent"')
|
||||
expect(compiler.raw_content).to include(
|
||||
'define("discourse/theme-1/discourse/templates/components/mycomponent"',
|
||||
)
|
||||
end
|
||||
|
||||
it "handles colocated components" do
|
||||
@ -103,8 +132,8 @@ RSpec.describe ThemeJavascriptCompiler do
|
||||
import Component from "@glimmer/component";
|
||||
export default class MyComponent extends Component {}
|
||||
JS
|
||||
"discourse/components/mycomponent.hbs" => "{{my-component-template}}"
|
||||
}
|
||||
"discourse/components/mycomponent.hbs" => "{{my-component-template}}",
|
||||
},
|
||||
)
|
||||
expect(compiler.raw_content).to include("__COLOCATED_TEMPLATE__ =")
|
||||
expect(compiler.raw_content).to include("setComponentTemplate")
|
||||
@ -117,17 +146,17 @@ RSpec.describe ThemeJavascriptCompiler do
|
||||
import Component from "@glimmer/component";
|
||||
export default class MyComponent extends Component {}
|
||||
JS
|
||||
"admin/components/mycomponent.hbs" => "{{my-component-template}}"
|
||||
}
|
||||
"admin/components/mycomponent.hbs" => "{{my-component-template}}",
|
||||
},
|
||||
)
|
||||
expect(compiler.raw_content).to include("__COLOCATED_TEMPLATE__ =")
|
||||
expect(compiler.raw_content).to include("setComponentTemplate")
|
||||
end
|
||||
|
||||
it "applies theme AST transforms to colocated components" do
|
||||
compiler = ThemeJavascriptCompiler.new(12345678910, 'my theme name')
|
||||
compiler = ThemeJavascriptCompiler.new(12_345_678_910, "my theme name")
|
||||
compiler.append_tree(
|
||||
{ "discourse/components/mycomponent.hbs" => '{{theme-i18n "my_translation_key"}}' }
|
||||
{ "discourse/components/mycomponent.hbs" => '{{theme-i18n "my_translation_key"}}' },
|
||||
)
|
||||
template_compiled_line = compiler.raw_content.lines.find { |l| l.include?('"block":') }
|
||||
expect(template_compiled_line).to include("12345678910")
|
||||
@ -140,8 +169,8 @@ RSpec.describe ThemeJavascriptCompiler do
|
||||
import Component from "@glimmer/component";
|
||||
class MyComponent extends Component {}
|
||||
JS
|
||||
"discourse/components/mycomponent.hbs" => "{{my-component-template}}"
|
||||
}
|
||||
"discourse/components/mycomponent.hbs" => "{{my-component-template}}",
|
||||
},
|
||||
)
|
||||
expect(compiler.raw_content).to include("__COLOCATED_TEMPLATE__ =")
|
||||
expect(compiler.raw_content).to include("throw new Error")
|
||||
@ -149,9 +178,7 @@ RSpec.describe ThemeJavascriptCompiler do
|
||||
|
||||
it "handles template-only components" do
|
||||
compiler.append_tree(
|
||||
{
|
||||
"discourse/components/mycomponent.hbs" => "{{my-component-template}}"
|
||||
}
|
||||
{ "discourse/components/mycomponent.hbs" => "{{my-component-template}}" },
|
||||
)
|
||||
expect(compiler.raw_content).to include("__COLOCATED_TEMPLATE__ =")
|
||||
expect(compiler.raw_content).to include("setComponentTemplate")
|
||||
@ -163,7 +190,7 @@ RSpec.describe ThemeJavascriptCompiler do
|
||||
it "applies terser and provides sourcemaps" do
|
||||
sources = {
|
||||
"multiply.js" => "let multiply = (firstValue, secondValue) => firstValue * secondValue;",
|
||||
"add.js" => "let add = (firstValue, secondValue) => firstValue + secondValue;"
|
||||
"add.js" => "let add = (firstValue, secondValue) => firstValue + secondValue;",
|
||||
}
|
||||
|
||||
compiler.append_tree(sources)
|
||||
@ -181,7 +208,7 @@ RSpec.describe ThemeJavascriptCompiler do
|
||||
it "handles invalid JS" do
|
||||
compiler.append_raw_script("filename.js", "if(someCondition")
|
||||
expect(compiler.content).to include('console.error("[THEME 1')
|
||||
expect(compiler.content).to include('Unexpected token')
|
||||
expect(compiler.content).to include("Unexpected token")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user