Module: Tiler::WidgetTestHelper

Defined in:
lib/tiler/test_helpers.rb

Overview

Shared assertions for testing widgets in either the engine or a host app. Mix into ActiveSupport::TestCase / ActionView::TestCase:

class MyWidgetTest < ActiveSupport::TestCase
  include Tiler::WidgetTestHelper
  test "renders" do
    assert_widget_renders("my_widget", config: { foo: 1 })
  end
end

Instance Method Summary collapse

Instance Method Details

#assert_widget_default_size(type, w:, h:) ⇒ Object

Asserts the registered class’ default_size matches the expected w/h.



20
21
22
23
24
25
26
27
# File 'lib/tiler/test_helpers.rb', line 20

def assert_widget_default_size(type, w:, h:)
  klass = assert_widget_in_registry(type)
  size  = klass.default_size
  actual_w = size[:w] || size["w"]
  actual_h = size[:h] || size["h"]
  assert_equal w, actual_w, "#{type}.default_size width drift"
  assert_equal h, actual_h, "#{type}.default_size height drift"
end

#assert_widget_in_registry(type) ⇒ Object

Asserts the widget is registered under the given type slug.



13
14
15
16
17
# File 'lib/tiler/test_helpers.rb', line 13

def assert_widget_in_registry(type)
  klass = Tiler.widgets[type.to_s]
  assert klass, "expected '#{type}' in Tiler.widgets registry; got #{Tiler.widgets.types.inspect}"
  klass
end

#assert_widget_renders(type, config: {}, data_source: nil, dashboard: nil) ⇒ Object

Renders the widget’s partial against the panel’s data hash; asserts it produced any HTML at all and returns the rendered string. Requires an ActionView::TestCase (or a class that includes ActionView’s render API).



39
40
41
42
43
44
45
46
47
# File 'lib/tiler/test_helpers.rb', line 39

def assert_widget_renders(type, config: {}, data_source: nil, dashboard: nil)
  raise "assert_widget_renders requires ActionView::TestCase (no #render)" unless respond_to?(:render)
  dashboard ||= (defined?(create_dashboard) ? create_dashboard : Tiler::Dashboard.create!(name: "T#{SecureRandom.hex(3)}"))
  panel = build_widget_panel(type, dashboard, config: config, data_source: data_source)
  data  = panel.data
  html  = render partial: panel.widget.partial, locals: { panel: panel, data: data }
  assert html.present?, "#{type} partial rendered empty for config=#{config.inspect}"
  html
end

#assert_widget_supports_color(type) ⇒ Object

True when the widget exposes the per-panel single-color picker on the form.



50
51
52
53
# File 'lib/tiler/test_helpers.rb', line 50

def assert_widget_supports_color(type)
  klass = assert_widget_in_registry(type)
  assert klass.supports_color_config?, "#{type} should opt into supports_color_config?"
end

#widget_data(type, config: {}, data_source: nil, dashboard: nil) ⇒ Object

Builds a panel + invokes #data without raising. Returns the data hash.



30
31
32
33
34
# File 'lib/tiler/test_helpers.rb', line 30

def widget_data(type, config: {}, data_source: nil, dashboard: nil)
  dashboard ||= (defined?(create_dashboard) ? create_dashboard : Tiler::Dashboard.create!(name: "T#{SecureRandom.hex(3)}"))
  panel = build_widget_panel(type, dashboard, config: config, data_source: data_source)
  panel.data
end