Simulate Ajax Posts with select_form

We need a system that tests our new FORM by linking it to its action and that moves the test case closer to doing what a real web browser would do. Erase that test and start again:

  def test_update_yaml_node
    yaml_to_xhtml(get_omap)

    xhr :get,
        :edit_node,
        :page_name => 'WikiTestPage',
        :ypath     => 'node:test_uncle_wiggily:script'

#    puts @response.body
    form = select_form('/wiki/update_yaml_node')
    spike = 'new contents'
    form['node_contents'] = spike
    ypath = '//test_uncle_wiggily//script'
    assert_equal ypath, form['ypath'].value
    assert_equal @yar.page_name, form['page_name'].value
#    form.submit_without_clicking_button
#    yaml = YAML.parse_file(@yar.yaml_path)
#    assert_equal spike, yaml.select(ypath)[0].value
  end

That will soon be a much better test. It starts by using xhr :get to fetch out the FORM (the one that will soon edit one YAML node in our Wiki). The secret @response.body variable contains our FORM's HTML source. For the helplessly curious, it looks like this (reformatted):

  <form action = '/wiki/update_yaml_node'
            id = 'edit_node_form'
          name = 'edit_node_form'>
    <textarea name = 'node_contents'
         onkeydown = 'editor_keydown(event);'
              cols = '60'
              rows =  '3'
      >something tests uncle wiggily</textarea>
  </form>

As we suspected, that FORM has no hidden page_name or ypath variable. The server will not be able to identify the target file or node. We saw that coming; we knew it all along.

That's why the test case now uses select_form. It's also why we commented-out ...

Get Test Driven Ajax (on Rails) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.