RJS Assertions
RJS Assertions is a collection of assertions useful for
testing your RJS templates. RJS Assertions function very
similarly to the standard assertions built in to Rails,
except that RJS Assertions are capable of parsing the
JavaScript produced by an RJS template, and assert various
conditions.
Usage:
assert_rjs_tag & assert_rjs_no_tag
- Same options as assert_tag/assert_no_tag. :rjs option can
added to limit scope of assert. Example:
# Asserts the content exists in any HTML fragments.
assert_rjs_tag :content => 'Hello World'
# Asserts the content exists in HTML fragments being added
# to or replacing the greeting_div_id HTML.
assert_rjs_tag :rjs => {:block => 'greeting_div_id'}.
:content => 'Hello World'
assert_rjs_visual_effect:
- Accepts two arguments, a string representing the id of the block the effect
should be performed on, and a symbol or string of the effect.
(Currently only supports "show" and "hide") Example:
assert_rjs_visual_effect 'greeting_div_id', :show
Example:
Say you have an action that triggers the following RJS template:
page.replace_html 'details_column_left', :partial => 'field', :collection => @left_column,
:locals => {:values => @field_hash}
page.replace_html 'details_column_right', :partial => 'field', :collection => @right_column,
:locals => {:values => @field_hash}
page.hide 'information_save_button'
page.show 'information_edit_button'
page.hide 'field_errors'
The action JavaScript response sent to the browser might end up looking something like:
Element.update("details_column_left", "");
Element.update("details_column_right", "");
Element.hide("information_save_button");
Element.show("information_edit_button");
Using RJS Assertions, you can now assert various conditions on this response. Note the example
assertions below:
def test_updating_fields
post :update_fields, {:id => @joe_schmoe.id, :field => {'1' => 'New Value'}}, logged_in_session
assert_rjs_tag :rjs => {:block => 'details_column_left' }, :tag => 'label', :content => 'Preferred:'
assert_rjs_tag :rjs => {:block => 'details_column_right' }, :tag => 'label', :content => 'Locations:'
assert_rjs_no_tag :rjs => {:block => 'details_column_left' }, :content => 'Left Value One!!!'
assert_rjs_visual_effect 'information_save_button', :hide
assert_rjs_visual_effect 'information_edit_button', :show
end