require 'spec_helper' module Qcourses describe ViewHelpers do require 'sinatra' let(:template_cache) { Tilt::Cache.new } let(:app) { Sinatra::Base.new } include Sinatra::Templates include Sinatra::Helpers include ViewHelpers describe 'human_period' do let(:event) { Event.new(from: "30-11-2012", to: "02-12-2013") } let(:subject) { human_period(event.from, event.to) } it "formats both dates short with a hyphen inbetween" do subject.should == "30/11/2012 - 2/12/2013" end context "when dates are in same year" do before { event.to = "02-12-2012" } it "formats both dates with a hyphen when dates are in different months" do subject.should == '30/11 - 2/12 2012' end context "when dates are in same month" do it "formats both dates with a hyphen" do event.from = "1-12-2012" subject.should == '1 - 2 December 2012' end end end end describe "input_for" do def html_for(result) Capybara.string(result) end before do @employee = Employee.new :name => 'gijs' @employee.valid? end describe "on an existing object" do it 'creates an input tag' do html_for(input_for(:employee, :name)).should have_selector("input[type='text'][name='employee[name]'][value='gijs']") html_for(input_for(@employee, :name)).should have_selector("input[type='text'][name='employee[name]'][value='gijs']") end it 'creates an an error class if attribute has error' do result = html_for(input_for(:employee, :email)) result.should have_selector("input[type='text'][name='employee[email]'][class='error']") result.should have_selector("span[class='error']", :text => @employee.errors.on(:email).first) end it 'can suppress error messagee if attribute has error' do result = html_for(input_for(:employee, :email, :suppress_error_messages => true)) result.should have_selector("input[type='text'][name='employee[email]'][class='error']") result.should_not have_selector("span[class='error']", :text => @employee.errors.on(:email).first) end it 'can have a class' do html_for(input_for(:employee, :name, :class => 'short')).should have_selector("input[name='employee[name]'][class='short']") html_for(input_for(:employee, :email, :class => 'short')).should have_selector("input[name='employee[email]'][class='error short']") end end describe "on an array object" do it "creates an input tag with an extra [] inthe name" do html_for(input_for(:employee, :name, :array_element=>true)).should have_selector("input[type='text'][name='employee[][name]'][value='gijs']") end end end describe "text_field_for" do def html_for(result) Capybara.string(result) end before do @company = Company.new end describe "on an existing object" do it 'creates a textarea tag' do html_for(textarea_for(:company, :invoice_address)).should have_selector("textarea[name='company[invoice_address]']") html_for(textarea_for(@company, :invoice_address)).should have_selector("textarea[name='company[invoice_address]']") end it "contains the value" do @company.invoice_address = "Gondelstraat\nTilburg" html_for(textarea_for(:company, :invoice_address)).should have_selector("textarea[name='company[invoice_address]']", :text=>@company.invoice_address) end it 'creates an an error class if attribute has error' do @company.errors.add(:invoice_address, "wrong address") html_for(textarea_for(:company, :invoice_address)).should have_selector("textarea[name='company[invoice_address]'][class='error']") end end end describe "select_for" do def html_for(result) Capybara.string(result) end before do @employee = Rubory.build :employee @employee.valid? end describe "on an existing object" do it 'creates a select tag' do html_for(select_for(:employee, :company_id)).should have_selector("select[name='employee[company_id]']") html_for(select_for(@employee, :company_id)).should have_selector("select[name='employee[company_id]']") end it 'creates options' do options = [["99", "Other Company"]] options << [@employee.company_id.to_s, "Company"] html_for(select_for(:employee, :company_id, options)).should have_selector("select[name='employee[company_id]'] option[value='99']", :text => "Other Company") html_for(select_for(:employee, :company_id, options)).should have_selector("select[name='employee[company_id]'] option[value='#{@employee.company_id}'][selected='selected']", :text => "Company") end it 'accepts integers for option values' do options = [[99, "Other Company"]] options << [@employee.company_id, "Company"] html_for(select_for(:employee, :company_id, options)).should have_selector("select[name='employee[company_id]'] option[value='99']", :text => "Other Company") html_for(select_for(:employee, :company_id, options)).should have_selector("select[name='employee[company_id]'] option[value='#{@employee.company_id}'][selected='selected']", :text => "Company") end it 'can have a class and or id' do html_for(select_for(:employee, :company_id, nil, :class => 'short')).should have_selector("select[name='employee[company_id]'][class='short']") html_for(select_for(:employee, :company_id, nil, :id => 'my_id')).should have_selector("select#my_id") end end describe "on an array object" do it "creates an input tag with an extra [] inthe name" do html_for(select_for(:employee, :company_id, nil, :array_element=>true)).should have_selector("select[name='employee[][company_id]']") end end end describe "event_registration_link" do def html_for(result) Capybara.string(result) end def url(addr = nil, absolute = true, add_script_name = true) addr end let(:event) { Rubory.build :event } it "contains a registration link" do result = html_for(event_registration_link(event)) result.should have_selector("a[href='/registrations/new/#{event.id}']") end it "contains no registration link if event is closed" do event.stub :open? => false html_for(event_registration_link(event)).should_not have_selector("a[href='/registrations/new/#{event.id}']") html_for(event_registration_link(event)).should have_content("closed") end end end end