Class: MinitestShopifyThemes::LiquidTest

Inherits:
Minitest::Test
  • Object
show all
Includes:
Capybara::Minitest::Assertions, Filters::MoneyFilter::TestHelper, Filters::TranslationsFilter::TestHelper
Defined in:
lib/minitest_shopify_themes/liquid_test.rb

Direct Known Subclasses

ViewTest

Instance Method Summary collapse

Instance Method Details

#deep_stringify_keys(hash) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/minitest_shopify_themes/liquid_test.rb', line 44

def deep_stringify_keys(hash)
  # Liquid expects string keys for variables, but Rails convention is to use symbols.
  # This method recursively converts all keys to strings so either convention can be used.
  hash.each_with_object({}) do |(key, value), acc|
    acc[key.to_s] = case value
    when Hash
      deep_stringify_keys(value)
    when Array
      value.map { |v| v.is_a?(Hash) ? deep_stringify_keys(v) : v }
    else
      value
    end
  end
end

#htmlObject



40
41
42
# File 'lib/minitest_shopify_themes/liquid_test.rb', line 40

def html
  Loofah.html5_fragment(@output)
end

#pageObject

Helper to enable Capybara assertions on the rendered template.



26
27
28
# File 'lib/minitest_shopify_themes/liquid_test.rb', line 26

def page
  Capybara.string(@page)
end

#render(template:, variables: {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/minitest_shopify_themes/liquid_test.rb', line 14

def render(template:, variables: {})
  @page = render_liquid(template:, variables:)

  if MinitestShopifyThemes.configuration.layout_file
    @page = render_liquid(
      template: MinitestShopifyThemes.configuration.layout_file,
      variables: variables.merge({ content_for_layout: @page })
    )
  end
end

#render_liquid(template:, variables:) ⇒ Object



30
31
32
33
34
# File 'lib/minitest_shopify_themes/liquid_test.rb', line 30

def render_liquid(template:, variables:)
  file = File.read(File.join(MinitestShopifyThemes.configuration.theme_root, template) + ".liquid")
  template = Liquid::Template.parse(file, error_mode: :strict)
  @output = template.render!(deep_stringify_keys(variables), {strict_variables: true, strict_filters: true})
end

#textObject



36
37
38
# File 'lib/minitest_shopify_themes/liquid_test.rb', line 36

def text
  html&.to_text(encode_special_chars: false).gsub(/\s+/, ' ').strip
end