webrat/spec/public/check_spec.rb

192 lines
5.2 KiB
Ruby
Raw Normal View History

require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2008-03-02 20:14:52 +00:00
describe "check" do
2008-04-27 16:45:01 +00:00
it "should fail if no checkbox found" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/login">
</form>
</html>
HTML
lambda { check "remember_me" }.should raise_error(Webrat::NotFoundError)
2008-03-02 20:14:52 +00:00
end
2008-04-27 16:45:01 +00:00
it "should fail if input is not a checkbox" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="text" name="remember_me" />
</form>
</html>
HTML
2009-04-08 00:30:12 +00:00
2008-11-23 05:22:49 +00:00
lambda { check "remember_me" }.should raise_error(Webrat::NotFoundError)
2008-03-02 20:14:52 +00:00
end
2009-04-08 00:30:12 +00:00
2008-04-27 16:45:01 +00:00
it "should check rails style checkboxes" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
2008-03-02 20:14:52 +00:00
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" />
<input name="user[tos]" type="hidden" value="0" />
<label for="user_tos">TOS</label>
<input type="submit" />
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
2009-04-08 00:30:12 +00:00
2008-11-23 05:22:49 +00:00
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
check "TOS"
click_button
2008-03-02 20:14:52 +00:00
end
2009-04-08 00:30:12 +00:00
2008-04-27 16:45:01 +00:00
it "should result in the value on being posted if not specified" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" />
<input type="submit" />
</form>
</html>
HTML
2009-04-08 00:30:12 +00:00
2008-11-23 05:22:49 +00:00
webrat_session.should_receive(:post).with("/login", "remember_me" => "on")
check "remember_me"
click_button
2008-03-02 20:14:52 +00:00
end
2009-04-08 00:30:12 +00:00
it "should fail if the checkbox is disabled" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" disabled="disabled" />
<input type="submit" />
</form>
</html>
HTML
2009-04-08 00:30:12 +00:00
2008-11-23 05:22:49 +00:00
lambda { check "remember_me" }.should raise_error(Webrat::DisabledFieldError)
end
2009-04-08 00:30:12 +00:00
2008-04-27 16:45:01 +00:00
it "should result in a custom value being posted" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" value="yes" />
<input type="submit" />
</form>
</html>
HTML
2009-04-08 00:30:12 +00:00
2008-11-23 05:22:49 +00:00
webrat_session.should_receive(:post).with("/login", "remember_me" => "yes")
check "remember_me"
click_button
2008-03-02 20:14:52 +00:00
end
end
describe "uncheck" do
2008-04-27 16:45:01 +00:00
it "should fail if no checkbox found" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
2008-03-02 20:14:52 +00:00
<form method="post" action="/login">
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
2008-04-07 04:52:49 +00:00
2008-11-23 05:22:49 +00:00
lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError)
2008-03-02 20:14:52 +00:00
end
2008-04-27 16:45:01 +00:00
it "should fail if input is not a checkbox" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
2008-03-02 20:14:52 +00:00
<form method="post" action="/login">
<input type="text" name="remember_me" />
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
2009-04-08 00:30:12 +00:00
2008-11-23 05:22:49 +00:00
lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError)
2008-03-02 20:14:52 +00:00
end
2009-04-08 00:30:12 +00:00
it "should fail if the checkbox is disabled" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" checked="checked" disabled="disabled" />
<input type="submit" />
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
lambda { uncheck "remember_me" }.should raise_error(Webrat::DisabledFieldError)
end
2009-04-08 00:30:12 +00:00
2008-04-27 16:45:01 +00:00
it "should uncheck rails style checkboxes" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
2008-03-02 20:14:52 +00:00
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" />
<input name="user[tos]" type="hidden" value="0" />
<label for="user_tos">TOS</label>
<input type="submit" />
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
check "TOS"
uncheck "TOS"
click_button
2008-03-02 20:14:52 +00:00
end
2008-04-27 16:45:01 +00:00
it "should result in value not being posted" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
2008-03-02 20:14:52 +00:00
<form method="post" action="/login">
<input type="checkbox" name="remember_me" value="yes" checked="checked" />
<input type="submit" />
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
webrat_session.should_receive(:post).with("/login", {})
uncheck "remember_me"
click_button
2008-03-02 20:14:52 +00:00
end
2009-04-08 00:30:12 +00:00
it "should work with checkboxes with the same name" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="option_1" name="options[]" type="checkbox" value="1" />
<label for="option_1">Option 1</label>
<input id="option_2" name="options[]" type="checkbox" value="2" />
<label for="option_2">Option 2</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", {"options" => ["1", "2"]})
check 'Option 1'
check 'Option 2'
click_button
end
2009-04-08 00:30:12 +00:00
it "should uncheck rails style checkboxes nested inside a label" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<label>
TOS
<input name="user[tos]" type="hidden" value="0" />
<input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" />
</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
uncheck "TOS"
click_button
end
2009-04-08 00:30:12 +00:00
2008-04-27 16:45:01 +00:00
end