Class: Apotomo::TestCase

Inherits:
Cell::TestCase
  • Object
show all
Includes:
WidgetShortcuts
Defined in:
lib/apotomo/test_case.rb

Overview

Testing is fun. Test your widgets!

This class helps you testing widgets where it can. It is similar as in a controller. A declarative test would look like

class BlogWidgetTest < Apotomo::TestCase
  has_widgets do |root|
    root << widget(:comments_widget, 'post-comments')
  end

  it "should be rendered nicely" do
    render_widget 'post-comments'

    assert_select "div#post-comments", "Comments for this post"
  end

  it "should redraw on :update" do
    trigger :update
    assert_response "$(\"post-comments\").update ..."
  end

For unit testing, you can grab an instance of your tested widget.

it "should be visible" do
  assert root['post-comments'].visible?
end

See also in Cell::TestCase.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WidgetShortcuts

#widget

Class Method Details

.has_widgets(&block) ⇒ Object

Setup a widget tree as you’re used to it from your controller. Executed in test context.



37
38
39
# File 'lib/apotomo/test_case.rb', line 37

def has_widgets(&block)
  @has_widgets = block  # DISCUSS: use ControllerMethods?
end

.has_widgets_blocksObject



34
# File 'lib/apotomo/test_case.rb', line 34

def has_widgets_blocks; @has_widgets; end

Instance Method Details

#assert_response(*content) ⇒ Object

After a #trigger this assertion compares the actually triggered page updates with the passed.

Example:

trigger :submit, :source => "post-comments"
assert_response "alert(\":submit clicked!\")", /\$\("post-comments"\).update/


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/apotomo/test_case.rb', line 88

def assert_response(*content)
  updates = root.page_updates
  
  i = 0
  content.each do |assertion|
    if assertion.kind_of? Regexp
      assert_match assertion, updates[i] 
    else
      assert_equal assertion, updates[i]
    end
    
    i+=1
  end
end

#parent_controllerObject



61
62
63
# File 'lib/apotomo/test_case.rb', line 61

def parent_controller
  @controller
end

#render_widget(*args) ⇒ Object

Renders the widget name.



66
67
68
# File 'lib/apotomo/test_case.rb', line 66

def render_widget(*args)
  @last_invoke = root.render_widget(*args)
end

#rootObject

Returns the widget tree from TestCase.has_widgets.



54
55
56
57
58
59
# File 'lib/apotomo/test_case.rb', line 54

def root
  blk = self.class.has_widgets_blocks or raise "Please setup a widget tree using TestCase.has_widgets"
  @root ||= Apotomo::Widget.new(parent_controller, "root").tap do |root|
     self.instance_exec(root, &blk)
  end
end

#setupObject



42
43
44
45
46
47
48
49
50
# File 'lib/apotomo/test_case.rb', line 42

def setup
  super
  @controller.instance_eval do 
    def controller_path
     'barn'
    end
  end
  @controller.extend Apotomo::Rails::ControllerMethods
end

#trigger(type, options) ⇒ Object

Triggers an event of type. You have to pass :source as options.

Example:

trigger :submit, :source => "post-comments"


75
76
77
78
79
80
# File 'lib/apotomo/test_case.rb', line 75

def trigger(type, options)
  source = root.find_widget(options.delete(:source))
  source.options.merge!(options)  # TODO: this is just a try-out (what about children?). 
  source.fire(type)
  root.page_updates # DISCUSS: use ControllerMethods?
end