Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
add HasCheckbox matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
dcuddeback committed Nov 14, 2011
1 parent b9da3f7 commit 2d1cf72
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rspec/tag_matchers.rb
Expand Up @@ -3,3 +3,4 @@
require 'rspec/tag_matchers/has_tag'
require 'rspec/tag_matchers/has_form'
require 'rspec/tag_matchers/has_input'
require 'rspec/tag_matchers/has_checkbox'
22 changes: 22 additions & 0 deletions lib/rspec/tag_matchers/has_checkbox.rb
@@ -0,0 +1,22 @@
module RSpec::TagMatchers
def have_checkbox
HasCheckbox.new
end

class HasCheckbox < HasInput
def initialize
super
with_attribute(:type => :checkbox)
end

def checked
with_attribute(:checked => true)
self
end

def not_checked
with_attribute(:checked => false)
self
end
end
end
45 changes: 45 additions & 0 deletions spec/lib/rspec/tag_matchers/has_checkbox_spec.rb
@@ -0,0 +1,45 @@
require 'spec_helper'

describe RSpec::TagMatchers::HasCheckbox do
include RSpec::TagMatchers

describe "matching checkbox tags" do
context "have_checkbox" do
subject { have_checkbox }
it { should_not match("<input />") }
it { should match("<input type='checkbox' />") }
it { should match("<input TYPE='CHECKBOX' />") }
it { should_not match("<input type='text' />") }
it { should_not match("<input type='checkboxer' />") }
it { should_not match("input checkbox") }
end
end

describe "matching checkbox state" do
context "have_checkbox.checked" do
subject { have_checkbox.checked }
it { should_not match("<input type='checkbox' />") }
it { should match("<input type='checkbox' checked />") }
it { should match("<input type='checkbox' checked='checked' />") }
it { should match("<input type='checkbox' checked='1' />") }
end

context "have_checkbox.not_checked" do
subject { have_checkbox.not_checked }
it { should match("<input type='checkbox' />") }
it { should_not match("<input type='checkbox' checked />") }
it { should_not match("<input type='checkbox' checked='checked' />") }
it { should_not match("<input type='checkbox' checked='1' />") }
end
end

describe "matching input names" do
context "have_checkbox.for(:user => :admin)" do
subject { have_checkbox.for(:user => :admin) }
it { should_not match("<input type='checkbox' />") }
it { should_not match("<input type='checkbox' name='user' />") }
it { should match("<input type='checkbox' name='user[admin]' />") }
it { should_not match("<input type='checkbox' name='user[admin][root]' />") }
end
end
end

0 comments on commit 2d1cf72

Please sign in to comment.