Skip to content

Commit

Permalink
Adds rspec setup and first pass of view helper specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrisher committed Jun 25, 2013
1 parent c6ad252 commit e956506
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ source "http://rubygems.org"
# Specify your gem's dependencies in simple_calendar.gemspec
gemspec

gem 'rspec'
group :test do
gem 'rspec'
gem 'pry'
end
1 change: 0 additions & 1 deletion lib/simple_calendar/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module ViewHelpers
def calendar(events, options={}, &block)
raise 'SimpleCalendar requires a block to be passed in' unless block_given?


opts = {
:year => (params[:year] || Time.zone.now.year).to_i,
:month => (params[:month] || Time.zone.now.month).to_i,
Expand Down
10 changes: 6 additions & 4 deletions spec/helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
$:.unshift(File.expand_path('../../lib', __FILE__))

require 'pathname'
require 'rubygems'
require 'bundler'
require 'pry'
require 'active_support/all'
require 'action_view'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path = root_path.join('lib')

Bundler.setup(:default)
Dir[root_path.join("spec/support/*.rb")].each { |f| require f }
Dir[lib_path.join("simple_calendar/*.rb")].each { |f| require f }

Dir[root_path.join("spec/support/**/*.rb")].each { |f| require f }
Bundler.setup(:default)

RSpec.configure do |config|
config.fail_fast = true
Expand Down
94 changes: 94 additions & 0 deletions spec/simple_calendar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'helper'

class View
include SimpleCalendar::ViewHelpers
include ActionView::Helpers
include ActionView::Context

def raw(argument)
argument
end

end

describe "SimpleCalendar" do
subject { View.new }

before do
subject.stub(:params => {:year => 2013, :month => 5})
end

describe "#calendar" do
context "with invalid arguments" do
it "should raise an error" do
expect { subject.calendar("foo") }.to raise_error(StandardError, /block/)
end
end

context "with valid arguments" do
it "should draw a calender" do
subject.should_receive(:draw_calendar)
subject.calendar("foo") { "test" }
end

it "should not overwrite passed in arguments" do
subject.should_receive(:build_range).and_return([2,2,3])
subject.should_receive(:draw_calendar)
subject.calendar("foo") { "test" }
end
end
end

describe "#build_range" do
it "should return an array representing the days of the month" do
selected_month = Date.new(2013, 1)
options = {:start_day => :tuesday}
result = subject.send(:build_range, selected_month, options)
result.class.should == Array
result.first.class.should == Date
result.last.class.should == Date
end
end

describe "#draw_calendar" do
it "should render a calendar table" do
#TODO: refactor draw_calendar to not require so much build up
subject.should_receive(:month_header)
selected_month = Date.new(2013, 1)
month = []
current_date = Date.new(2013, 1, 14)
events = {}
options = {:start_day => :monday}
block = Proc.new {}
subject.send(:draw_calendar, selected_month, month, current_date, events, options, block).should match(/^<table/)
end
end

describe "#day_events" do
it "should return an array of events for a given day" do
date = Date.new(2013, 1, 14)
matching_event = stub(:start_time => Date.new(2013, 1, 14))
other_event = stub(:start_time => Date.new(2013,1,15))
events = [matching_event, other_event]
subject.send(:day_events, date, events).should == [matching_event]
end
end

describe "#month_header" do
it "should generate html including the month, next and previous month" do
subject.should_receive(:month_link).exactly(2)
selected_month = Date.new(2013, 1)
options = {}
#TODO: add coverage for actual output other than just the starting tag
subject.send(:month_header, selected_month, options).should match(/^<h2/)
end
end

describe "#month_link" do
it "should return a link" do
#TODO: test even needed?
subject.should_receive(:link_to)
subject.send(:month_link, "previous", Date.new(2013, 1), {})
end
end
end
9 changes: 9 additions & 0 deletions spec/support/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SimpleCalendar
class Rails
class Railtie
def self.initializer(object)
end
end
end
end

0 comments on commit e956506

Please sign in to comment.