Permalink
Browse files

+ specs for the view helpers

  • Loading branch information...
1 parent dfac125 commit 7ad9535bba99019e325d8f3a2c4a5ed081ba89bb @niko committed Mar 8, 2011
Showing with 63 additions and 3 deletions.
  1. +3 −3 config.ru
  2. +60 −0 view_spec.rb
View
@@ -9,11 +9,11 @@ use Rack::Static, :urls => ['/stylesheets','/javascript'], :root => 'public'
class TinySite::View
def link_to(url, name=url, opts={})
- opts, name = name, url if name.is_a? Hash # oder so
+ opts, name = name, url if name.is_a? Hash
- o = opts.map{|k,v| "#{k}=#{v}" }.join(' ')
+ o = opts.map{|k,v| %Q{ #{k}="#{v}"} }.join
- %Q{<a href="#{url}" #{o}>#{name}</a>}
+ %Q{<a href="#{url}"#{o}>#{name}</a>}
end
def navigation
View
@@ -0,0 +1,60 @@
+require 'rspec'
+require 'rack'
+
+$LOAD_PATH << File.dirname(File.expand_path __FILE__)
+
+class TinySite; end
+
+#noops
+def run(*a); end
+def use(*a); end
+
+load 'config.ru'
+
+describe TinySite::View do
+ before(:each) do
+ @app = stub(:app)
+ @view = TinySite::View.new(@app)
+ end
+ describe "#link_to" do
+ describe "with just one argument" do
+ it "uses this for href and text" do
+ link = '<a href="http://foo">http://foo</a>'
+ @view.link_to('http://foo').should == link
+ end
+ it "renders opts" do
+ link = '<a href="http://foo" class="class" id="id">http://foo</a>'
+ @view.link_to('http://foo', :class => 'class', :id => 'id').should == link
+ end
+ end
+ describe "with two arguments" do
+ it "uses these for href and text" do
+ link = '<a href="http://foo">bar</a>'
+ @view.link_to('http://foo', 'bar').should == link
+ end
+ it "renders opts, too" do
+ link = '<a href="http://foo" class="class" id="id">bar</a>'
+ @view.link_to('http://foo', 'bar', :class => 'class', :id => 'id').should == link
+ end
+ end
+ end
+ describe "#navigation" do
+ before(:each) do
+ nav = {
+ '/' => 'Home',
+ '/page_one' => 'one',
+ '/page_two' => 'two',
+ }
+ @view.stub! :global => {:navigation => nav}, :request_path => '/page_one'
+ end
+ it "returns a ul for the given navigation" do
+ @view.navigation.should == <<-EON
+<ul>
+ <li><a href="/">Home</a></li>
+ <li><a href="/page_one" class="active">one</a></li>
+ <li><a href="/page_two">two</a></li>
+</ul>
+EON
+ end
+ end
+end

0 comments on commit 7ad9535

Please sign in to comment.