DEV: Future-proof ThemeJavascriptCompiler spec (#17393)

Previously the spec was hardcoded to expect a certain compiled result. The precise result will change as we update to more recent Ember versions.

Instead, we can compile via the special theme compiler, and then compile our expected result via the normal compiler. If they match, then things are working as intended. This technique should be safe across template-compiler upgrades.
This commit is contained in:
David Taylor 2022-07-08 16:28:46 +01:00 committed by GitHub
parent 75e40baa64
commit 8e0b1ee74d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -70,41 +70,62 @@ describe ThemeJavascriptCompiler do
describe ThemeJavascriptCompiler::EmberTemplatePrecompiler do
# For the Ember (Glimmer) templates, serverside rendering is not trivial,
# so check the compiled JSON against known working output
let(:compiler) { described_class.new(theme_id) }
let(:helper_opcode) do
append = statement("{{dummy-helper 1}}")[0]
helper = append[1]
helper[0]
# so we compile the expected result with the standard compiler and compare to the theme compiler
let(:standard_compiler) { Barber::Ember::Precompiler.new }
let(:theme_compiler) { described_class.new(theme_id) }
def theme_compile(template)
compiled = theme_compiler.compile(template)
data = JSON.parse(compiled)
JSON.parse(data["block"])
end
def statement(template)
compiled = compiler.compile(template)
def standard_compile(template)
compiled = standard_compiler.compile(template)
data = JSON.parse(compiled)
block = JSON.parse(data["block"])
block["statements"]
JSON.parse(data["block"])
end
it 'adds the theme id to the helpers' do
expect(statement("{{theme-prefix 'translation_key'}}")).
to eq([[1, [helper_opcode, "theme-prefix", [22, "translation_key"], nil], false]])
expect(statement("{{theme-i18n 'translation_key'}}")).
to eq([[1, [helper_opcode, "theme-i18n", [22, "translation_key"], nil], false]])
expect(statement("{{theme-setting 'setting_key'}}")).
to eq([[1, [helper_opcode, "theme-setting", [22, "setting_key"], nil], false]])
expect(
theme_compile "{{theme-prefix 'translation_key'}}"
).to eq(
standard_compile "{{theme-prefix #{theme_id} 'translation_key'}}"
)
# Works when used inside other statements
expect(statement("{{dummy-helper (theme-prefix 'translation_key')}}")).
to eq([[1, [helper_opcode, "dummy-helper", [[helper_opcode, "theme-prefix", [22, "translation_key"], nil]], nil], false]])
expect(
theme_compile "{{theme-i18n 'translation_key'}}"
).to eq(
standard_compile "{{theme-i18n #{theme_id} 'translation_key'}}"
)
expect(
theme_compile "{{theme-setting 'setting_key'}}"
).to eq(
standard_compile "{{theme-setting #{theme_id} 'setting_key'}}"
)
# # Works when used inside other statements
expect(
theme_compile "{{dummy-helper (theme-prefix 'translation_key')}}"
).to eq(
standard_compile "{{dummy-helper (theme-prefix #{theme_id} 'translation_key')}}"
)
end
it 'works with the old settings syntax' do
expect(statement("{{themeSettings.setting_key}}")).
to eq([[1, [helper_opcode, "theme-setting", [22, "setting_key"], [["deprecated"], [true]]], false]])
expect(
theme_compile "{{themeSettings.setting_key}}"
).to eq(
standard_compile "{{theme-setting #{theme_id} 'setting_key' deprecated=true}}"
)
# Works when used inside other statements
expect(statement("{{dummy-helper themeSettings.setting_key}}")).
to eq([[1, [helper_opcode, "dummy-helper", [[helper_opcode, "theme-setting", [22, "setting_key"], [["deprecated"], [true]]]], nil], false]])
expect(
theme_compile "{{dummy-helper themeSettings.setting_key}}"
).to eq(
standard_compile "{{dummy-helper (theme-setting #{theme_id} 'setting_key' deprecated=true)}}"
)
end
end