Module: JRails::SelectorAssertions
- Defined in:
- lib/jrails/selector_assertions.rb
Overview
– Copyright © 2006 Assaf Arkin (labnotes.org) Under MIT and/or CC By license. ++
Instance Method Summary collapse
-
#assert_select_rjs(*args, &block) ⇒ Object
Selects content from the RJS response.
- #response_from_page ⇒ Object
Instance Method Details
#assert_select_rjs(*args, &block) ⇒ Object
Selects content from the RJS response.
Narrowing down
With no arguments, asserts that one or more elements are updated or inserted by RJS statements.
Use the id argument to narrow down the assertion to only statements that update or insert an element with that identifier.
Use the first argument to narrow down assertions to only statements of that type. Possible values are :replace, :replace_html, :show, :hide, :toggle, :remove</tta>, <tt>:insert_html and :redirect.
Use the argument :insert followed by an insertion position to narrow down the assertion to only statements that insert elements in that position. Possible values are :top, :bottom, :before and :after.
Use the argument :redirect followed by a path to check that an statement which redirects to the specified path is generated.
Using the :remove statement, you will be able to pass a block, but it will be ignored as there is no HTML passed for this statement.
Using blocks
Without a block, assert_select_rjs merely asserts that the response contains one or more RJS statements that replace or update content.
With a block, assert_select_rjs also selects all elements used in these statements and passes them to the block. Nested assertions are supported.
Calling assert_select_rjs with no arguments and using nested asserts asserts that the HTML content is returned by one or more RJS statements. Using assert_select directly makes the same assertion on the content, but without distinguishing whether the content is returned in an HTML or JavaScript.
Examples
# Replacing the element foo.
# page.replace 'foo', ...
assert_select_rjs :replace, "foo"
# Replacing with the chained RJS proxy.
# page[:foo].replace ...
assert_select_rjs :chained_replace, 'foo'
# Inserting into the element bar, top position.
assert_select_rjs :insert, :top, "bar"
# Remove the element bar
assert_select_rjs :remove, "bar"
# Changing the element foo, with an image.
assert_select_rjs "foo" do
assert_select "img[src=/images/logo.gif""
end
# RJS inserts or updates a list with four items.
assert_select_rjs do
assert_select "ol>li", 4
end
# The same, but shorter.
assert_select "ol>li", 4
# Checking for a redirect.
assert_select_rjs :redirect, root_path
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/jrails/selector_assertions.rb', line 98 def assert_select_rjs(*args, &block) rjs_type = args.first.is_a?(Symbol) ? args.shift : nil id = args.first.is_a?(String) ? args.shift : nil # If the first argument is a symbol, it's the type of RJS statement we're looking # for (update, replace, insertion, etc). Otherwise, we're looking for just about # any RJS statement. if rjs_type if rjs_type == :insert position = args.shift id = args.shift insertion = "insert_#{position}".to_sym raise ArgumentError, "Unknown RJS insertion type #{position}" unless RJS_STATEMENTS[insertion] statement = "(#{RJS_STATEMENTS[insertion]})" else raise ArgumentError, "Unknown RJS statement type #{rjs_type}" unless RJS_STATEMENTS[rjs_type] statement = "(#{RJS_STATEMENTS[rjs_type]})" end else statement = "#{RJS_STATEMENTS[:any]}" end # Next argument we're looking for is the element identifier. If missing, we pick # any element, otherwise we replace it in the statement. pattern = Regexp.new( id ? statement.gsub(RJS_ANY_ID, "\"#{id}\"") : statement ) # Duplicate the body since the next step involves destroying it. matches = nil case rjs_type when :remove, :show, :hide, :toggle matches = @response.body.match(pattern) else if Rails::VERSION::STRING >= "4.2.0" @response.body.gsub pattern do |match| html = unescape_rjs match matches ||= Nokogiri::HTML.fragment '' Nokogiri::HTML(html).root.children.each{ |n| matches.add_child n if n.element? } "" end else @response.body.gsub pattern do |match| html = unescape_rjs match matches ||= [] matches.concat HTML::Document.new(html).root.children.select{ |n| n.tag? } "" end end end if matches assert true # to count the assertion if block_given? && !([:remove, :show, :hide, :toggle].include? rjs_type) begin @selected ||= nil in_scope, @selected = @selected, matches yield matches ensure @selected = in_scope end end matches else # RJS statement not found. case rjs_type when :remove, :show, :hide, :toggle = "No RJS statement that #{rjs_type.to_s}s '#{id}' was rendered." else = "No RJS statement that replaces or inserts HTML content." end flunk args.shift || end end |
#response_from_page ⇒ Object
21 22 23 |
# File 'lib/jrails/selector_assertions.rb', line 21 def response_from_page html_document.root end |