test/unit/gravatarify_helper_test.rb in gravatarify-1.2.0 vs test/unit/gravatarify_helper_test.rb in gravatarify-1.2.1
- old
+ new
@@ -17,62 +17,93 @@
end
context "#gravatar_attrs" do
should "return hash with :height, :width, :alt and :src defined" do
hash = gravatar_attrs('bella@gmail.com', :size => 16)
- assert_equal "#{BELLA_AT_GMAIL_JPG}?s=16", hash[:src]
- assert_equal 16, hash[:width]
- assert_equal 16, hash[:height]
- assert_equal 'bella@gmail.com', hash[:alt]
+ expected = { :alt => "", :src => "#{BELLA_AT_GMAIL_JPG}?s=16", :width => 16, :height => 16 }
+ assert_equal expected, hash
assert_nil hash[:size]
end
should "allow any param to be defined/overridden, except src, width and heigth" do
hash = gravatar_attrs('bella@gmail.com', :size => 20, :r => :x, :height => 40, :alt => 'bella', :id => 'test', :title => 'something', :class => 'gravatar')
- assert_equal "#{BELLA_AT_GMAIL_JPG}?r=x&s=20", hash[:src]
- assert_equal 20, hash[:width]
- assert_equal 20, hash[:height]
- assert_equal 'bella', hash[:alt]
- assert_equal 'test', hash[:id]
- assert_equal 'something', hash[:title]
- assert_equal 'gravatar', hash[:class]
+ expected = {
+ :alt => 'bella', :src => "#{BELLA_AT_GMAIL_JPG}?r=x&s=20", :width => 20, :height => 20,
+ :id => 'test', :title => 'something', :class => 'gravatar'
+ }
+ assert_equal expected, hash
assert_nil hash[:size]
assert_nil hash[:r]
end
end
context "#gravatar_tag helper" do
should "create <img/> tag with correct gravatar urls" do
- assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />', gravatar_tag('bella@gmail.com')
+ assert_equal '<img alt="" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />', gravatar_tag('bella@gmail.com')
end
should "create <img/> tags and handle all options correctly, other options should be passed to Rails' image_tag" do
- assert_equal '<img alt="bella@gmail.com" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
+ assert_equal '<img alt="" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
gravatar_tag('bella@gmail.com', :size => 16)
- assert_equal '<img alt="bella@gmail.com" class="gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=x&s=16" width="16" />',
+ assert_equal '<img alt="" class="gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=x&s=16" width="16" />',
gravatar_tag('bella@gmail.com', :class => "gravatar", :size => 16, :d => "x")
end
should "ensure that all values are correctly html-esacped!" do
- assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" title="<>" width="80" />',
+ assert_equal '<img alt="" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" title="<>" width="80" />',
gravatar_tag('bella@gmail.com', :title => '<>')
end
end
context "#gravatar_tag when passed in an object" do
should "create <img/>-tag based on :email field" do
obj = Object.new
- mock(obj).email.times(2) { "bella@gmail.com" }
+ mock(obj).email { "bella@gmail.com" }
- assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
+ assert_equal '<img alt="" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
gravatar_tag(obj)
end
should "create <img/>-tag based on gravatar_url from object if object responds to gravatar_url" do
obj = Object.new
+ mock(obj).name { "Mr. X" }
mock(obj).gravatar_url({ :size => 16 }) { "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" }
- assert_equal '<img alt="Gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
- gravatar_tag(obj, :size => 16, :alt => "Gravatar")
+ assert_equal '<img alt="Gravatar for Mr. X" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
+ gravatar_tag(obj, :size => 16, :alt => "Gravatar for #{obj.name}")
end
- end
+ end
+
+ context "Gravatarify::Helper#html_options" do
+ should "add be added to all tags/hashes created by gravatar_tag or gravatar_attrs" do
+ Gravatarify::Helper.html_options[:title] = "Gravatar" # add a title attribute, yeah neat-o!
+ Gravatarify::Helper.html_options[:class] = "gravatar"
+
+ assert_equal '<img alt="" class="gravatar" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" title="Gravatar" width="80" />',
+ gravatar_tag('bella@gmail.com')
+ hash = gravatar_attrs('bella@gmail.com', :size => 20, :title => "Gravatar for Bella", :id => "test")
+ expected = {
+ :alt => "", :width => 20, :height => 20, :src => "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=20",
+ :title => "Gravatar for Bella", :id => "test", :class => "gravatar"
+ }
+ assert_equal expected, hash
+ end
+
+ should "not allow :src, :height or :width to be set via global options and all local options should override!" do
+ Gravatarify::Helper.html_options[:src] = "avatar-30.jpg"
+ Gravatarify::Helper.html_options[:width] = 30
+ Gravatarify::Helper.html_options[:title] = "Avatar"
+
+ assert_equal '<img alt="" height="25" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=25" title="Gravatar" width="25" />',
+ gravatar_tag('bella@gmail.com', :size => 25, :title => 'Gravatar')
+ end
+
+ should "allow :alt to be set globally" do
+ Gravatarify::Helper.html_options[:alt] = "Gravatar"
+
+ assert_equal '<img alt="Gravatar" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
+ gravatar_tag('bella@gmail.com')
+ assert_equal '<img alt="Avatar" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d" width="80" />',
+ gravatar_tag('bella@gmail.com', :filetype => false, :alt => 'Avatar')
+ end
+ end
end
\ No newline at end of file