Module: RespondsToParent::SelectorAssertion

Included in:
ActionController::Assertions::SelectorAssertions
Defined in:
lib/responds_to_parent/selector_assertion.rb

Instance Method Summary collapse

Instance Method Details

#assert_select_parent(*args, &block) ⇒ Object

:call-seq:

assert_select_parent()
assert_select_parent() { |script| ... }

Selects JavaScript that is generated for the ‘parent’ window.

Without a block, #assert_select_parent asserts that the response is generated by responds_to_parent.

With a block, #assert_select_parent selects script that is supposed to be evaluated in the parent window and passes it to the block. Typically #assert_select_rjs is used in the block.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/responds_to_parent/selector_assertion.rb', line 15

def assert_select_parent(*args, &block)
  wrapper_re_str = Regexp.escape("with(window.parent) { setTimeout(function() { window.eval('") +
                 "(.*)" +
                 Regexp.escape("'); window.loc && loc.replace('about:blank'); }, 1) }")
  match = @response.body.match(Regexp.new(wrapper_re_str))

  if match
    escaped_js = match[1]
    unescaped_js = escaped_js.
      gsub(%r!</scr"\+"ipt>!, '</script>').
      gsub(/\\(\'|\")/, '\1').
      gsub(/((?:^|[^\\])(?:\\\\)*)\\n/, "\\1\n"). # replace `n' with odd number of backslash.
      gsub(/\\\\/, '\\')
    @response.body = unescaped_js # assert_select_rjs refers @response.body.

    if block_given?
      begin
        in_scope, @selected = @selected, unescaped_js
        yield unescaped_js
      ensure
        @selected = in_scope
      end
    end
    unescaped_js
  else
    # doesn't seem a responds_to_parent content.
    flunk args.shift || "No content for the parent window."
  end
end