Permalink
Browse files

Cleanup presenter specs to not look like 💩

  • Loading branch information...
1 parent 0f9daff commit 716662c9415e55d4deb08ae09650fcd80ac62eea @bobbytables committed Dec 27, 2012
Showing with 67 additions and 92 deletions.
  1. +61 −92 spec/lib/presenters/default_spec.rb
  2. +6 −0 spec/support/matchers/element_matchers.rb
@@ -5,162 +5,131 @@
let(:dummy_model) { FactoryGirl.build(:dummy_model) }
let(:objects) do
- 3.times.map {|n| FactoryGirl.build(:dummy_model) }
+ FactoryGirl.build_list(:dummy_model, 3)
end
let(:view_context) { ActionView::Base.new }
subject { TableCloth::Presenters::Default.new(objects, dummy_table, view_context) }
- it 'creates a table head' do
- header = subject.render_header
- doc = Nokogiri::HTML(header)
+ context "header" do
+ let(:column_names) { ["Col1", "Col2"] }
- thead = doc.xpath('.//thead')
- thead.should be_present
+ before(:each) do
+ subject.stub column_names: column_names
+ end
- tr = thead.xpath('.//tr')
- tr.should be_present
+ it "creates a thead" do
+ expect(subject.render_header).to have_tag "thead"
+ end
- th = tr.xpath('.//th')
- th.should be_present
+ it "creates th's" do
+ header = Nokogiri::HTML(subject.render_header)
+ expect(header.css("th").size).to be 2
+ end
- th.length.should == 3
+ it "creates th's with the correct text" do
+ header = Nokogiri::HTML(subject.render_header)
+ header.css("th").each_with_index {|th, i| expect(th.text).to eq(column_names[i]) }
+ end
end
- it 'creates rows' do
- rows = subject.render_rows
- doc = Nokogiri::HTML(rows)
-
- tbody = doc.xpath('//tbody')
- tbody.should be_present
-
- tbody.xpath('.//tr').each_with_index do |row, row_index|
- row.xpath('.//td').each_with_index do |td, td_index|
- object = objects[row_index]
- case td_index
- when 0
- object.id.to_s == td.text
- when 1
- object.name.should == td.text
- when 2
- object.email.should == td.text
- end
- end
+ context "rows" do
+ it 'creates a tbody' do
+ expect(subject.render_rows).to have_tag "tbody"
+ end
+
+ it "creates a row in the tbody" do
+ tbody = Nokogiri::HTML(subject.render_rows)
+ expect(tbody.css('tr').size).to be 3
end
end
context 'escaped values' do
let(:objects) do
- model = DummyModel.new.tap do |d|
- d.id = 1
- d.email = 'robert@example.com'
- d.name = '<script>alert("Im in your columns, snatching your main thread.")</script>'
- end
-
- [model]
+ FactoryGirl.build_list(:dummy_model, 1,
+ name: "<script>alert(\"Im in your columns, snatching your main thread.\")</script>"
+ )
end
it 'does not allow unescaped values in columns' do
- rows = subject.render_rows
- doc = Nokogiri::HTML(rows)
+ tbody = Nokogiri::HTML(subject.render_rows).at_xpath('//tbody')
- tbody = doc.xpath('//tbody')
tbody.xpath('//td').each do |td|
td.at_xpath('.//script').should_not be_present
end
end
end
- it 'creates an entire table' do
- doc = Nokogiri::HTML(subject.render_table)
- table = doc.xpath('//table')
- table.should be_present
-
- thead = table.xpath('.//thead')
- thead.should be_present
-
- tbody = table.at_xpath('.//tbody')
- tbody.should be_present
-
- tbody.xpath('.//tr').length.should == 3
- end
-
context 'configuration' do
- before(:all) do
- TableCloth::Configuration.configure do |config|
- config.table.class = 'table'
- config.thead.class = 'thead'
- config.th.class = 'th'
- config.tbody.class = 'tbody'
- config.tr.class = 'tr'
- config.td.class = 'td'
- end
- end
-
let(:doc) { Nokogiri::HTML(subject.render_table) }
it 'tables have a class attached' do
+ TableCloth.stub config_for: {class: "table"}
doc.at_xpath('//table')[:class].should include 'table'
end
it 'thead has a class attached' do
+ TableCloth.stub config_for: {class: "thead"}
doc.at_xpath('//thead')[:class].should include 'thead'
end
it 'th has a class attached' do
+ TableCloth.stub config_for: {class: "th"}
doc.at_xpath('//th')[:class].should include 'th'
end
it 'tbody has a class attached' do
+ TableCloth.stub config_for: {class: "tbody"}
doc.at_xpath('//tbody')[:class].should include 'tbody'
end
it 'tr has a class attached' do
+ TableCloth.stub config_for: {class: "tr"}
doc.at_xpath('//tr')[:class].should include 'tr'
end
it 'td has a class attached' do
+ TableCloth.stub config_for: {class: "td"}
doc.at_xpath('//td')[:class].should include 'td'
end
end
- context 'specific configuration' do
- let(:dummy_table) do
- Class.new(TableCloth::Base) do
- column :email, td_options: { class: 'email_column' }
- end
- end
+ context "column configuration" do
+ let(:column) { FactoryGirl.build(:column, options: {td_options: {class: "email_column"}}) }
+ let(:model) { FactoryGirl.build(:dummy_model) }
- it 'td has a class set' do
- doc = Nokogiri::HTML(subject.render_table)
- doc.at_xpath('//td')[:class].should include 'email_column'
+ it "td has a class set" do
+ td = Nokogiri::HTML(subject.render_td(column, model)).at_css("td")
+ expect(td[:class]).to eq "email_column"
end
- context 'by value of row column' do
- let(:dummy_table) { Class.new(DummyTableWithValueOptions) }
+ context "by value of row column" do
+ before(:each) do
+ column.stub value: ["robert@example.com", {class: "special-class"}]
+ end
+
+ specify "column has options because of value" do
+ td = Nokogiri::HTML(subject.render_td(column, model)).at_css("td")
- specify 'column has options because of value' do
- doc = Nokogiri::HTML(subject.render_table)
- td = doc.at_xpath('//td')
expect(td.text).to include "robert@example.com"
expect(td[:class]).to eq("special-class")
end
end
end
- context 'table configuration' do
+ context "table configuration" do
let(:doc) { Nokogiri::HTML(subject.render_table) }
let(:dummy_table) do
Class.new(TableCloth::Base) do
column :email
- config.table.class = 'table2'
- config.thead.class = 'thead2'
- config.th.class = 'th2'
- config.tbody.class = 'tbody2'
- config.tr.class = 'tr2'
- config.td.class = 'td2'
+ config.table.class = "table2"
+ config.thead.class = "thead2"
+ config.th.class = "th2"
+ config.tbody.class = "tbody2"
+ config.tr.class = "tr2"
+ config.td.class = "td2"
end
end
@@ -171,12 +140,12 @@
Class.new(TableCloth::Base) do
column :email
- config.table.class = 'table2'
- config.thead.class = 'thead2'
- config.th.class = 'th2'
- config.tbody.class = 'tbody2'
- config.tr.class = 'tr2'
- config.td.class = 'td2'
+ config.table.class = "table2"
+ config.thead.class = "thead2"
+ config.th.class = "th2"
+ config.tbody.class = "tbody2"
+ config.tr.class = "tr2"
+ config.td.class = "td2"
end
end
@@ -0,0 +1,6 @@
+RSpec::Matchers.define :have_tag do |tag|
+ match do |string|
+ document = Nokogiri::HTML(string)
+ !!document.at_xpath(".//#{tag}")
+ end
+end

0 comments on commit 716662c

Please sign in to comment.