From 8e0b1ee74dbe4dd5703831d43621c2518bba92a1 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 8 Jul 2022 16:28:46 +0100 Subject: [PATCH] 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. --- spec/lib/theme_javascript_compiler_spec.rb | 67 ++++++++++++++-------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/spec/lib/theme_javascript_compiler_spec.rb b/spec/lib/theme_javascript_compiler_spec.rb index 335b649307b..e3fcdda1362 100644 --- a/spec/lib/theme_javascript_compiler_spec.rb +++ b/spec/lib/theme_javascript_compiler_spec.rb @@ -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