require File.expand_path(File.dirname(__FILE__) + "/spec_helper") describe "clicks_button" do before do @session = ActionController::Integration::Session.new @session.stubs(:assert_response) @session.stubs(:get_via_redirect) @page = Webrat::Page.new(@session) @session.stubs(:current_page).returns(@page) @response = mock @session.stubs(:response).returns(@response) end it "should fail if no buttons" do @response.stubs(:body).returns(<<-EOS)
EOS lambda { @session.clicks_button }.should raise_error end it "should fail if input is not a submit button" do @response.stubs(:body).returns(<<-EOS)
EOS lambda { @session.clicks_button }.should raise_error end it "should default to get method" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect) @session.clicks_button end it "should assert valid response" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:assert_response).with(:success) @session.clicks_button end it "should default to current url" do @response.stubs(:body).returns(<<-EOS)
EOS @page.stubs(:url).returns("/current") @session.expects(:get_via_redirect).with("/current", {}) @session.clicks_button end it "should submit the first form by default" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/form1", {}) @session.clicks_button end it "should not explode on file fields" do @response.stubs(:body).returns(<<-EOS)
EOS @session.clicks_button end it "should submit the form with the specified button" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/form2", {}) @session.clicks_button "Form2" end it "should use action from form" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", {}) @session.clicks_button end it "should use method from form" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:post_via_redirect) @session.clicks_button end it "should send button as param if it has a name" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:post_via_redirect).with("/login", "login" => "Login") @session.clicks_button("Login") end it "should not send button as param if it has no name" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:post_via_redirect).with("/login", {}) @session.clicks_button("Login") end it "should send default password field values" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "user" => {"password" => "mypass"}) @session.clicks_button end it "should send default hidden field values" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "user" => {"email" => "test@example.com"}) @session.clicks_button end it "should send default text field values" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "user" => {"email" => "test@example.com"}) @session.clicks_button end it "should send default checked fields" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "1"}) @session.clicks_button end it "should send default radio options" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "F"}) @session.clicks_button end it "should send correct data for rails style unchecked fields" do @response.stubs(:body).returns(<<-EOS)
TOS
EOS @session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "0"}) @session.clicks_button end it "should send correct data for rails style checked fields" do @response.stubs(:body).returns(<<-EOS)
TOS
EOS @session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "1"}) @session.clicks_button end it "should send default collection fields" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:post_via_redirect).with("/login", "options" => ["burger", "fries", "soda", "soda", "dessert"], "response" => { "choices" => [{"selected" => "one"}, {"selected" => "two"}, {"selected" => "two"}]}) @session.clicks_button end it "should not send default unchecked fields" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", {}) @session.clicks_button end it "should send default textarea values" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:post_via_redirect).with("/posts", "post" => {"body" => "Post body here!"}) @session.clicks_button end it "should send default selected option value from select" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "month" => "2") @session.clicks_button end it "should send default selected option inner html from select when no value attribute" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "month" => "February") @session.clicks_button end it "should send first select option value when no option selected" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "month" => "1") @session.clicks_button end it "should handle nested properties" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:post_via_redirect).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}}) @session.clicks_button end it "should send default empty text field values" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""}) @session.clicks_button end it "should recognize button tags" do @response.stubs(:body).returns(<<-EOS)
EOS @session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""}) @session.clicks_button "Login" end end