Subversion Repositories mildred

Rev

Blame | Compare with Previous | Last modification | View Log | RSS feed

require File.dirname(__FILE__) + '/../spec_helper'

describe "ApplicationHelper" do
  include ApplicationHelper

  describe "a bar graph", :shared => true do
    it "should return two images" do
      @html.should have_tag("img", 2)
    end

    it "should have images with different CSS classes" do
      @html.should have_tag("img.left")
      @html.should have_tag("img.right")
    end
  end

  describe "a drag'n'drop bin", :shared => true do
    it "should have a working indicator" do
      @html.should have_tag("img", :id => /.*_indicator/)
    end

    it "should have an onDrop method" do
      @html.should have_tag("script", :text => /.*Droppables\.add.*/)
    end
  end

  describe ApplicationHelper do
    describe "dequeue_div" do
      before(:each) do
        @html = dequeue_div
      end

      it_should_behave_like "a drag'n'drop bin"
    end

    describe "requests" do
      before(:each) do
        @html = requests
      end

      it_should_behave_like "a drag'n'drop bin"
    end

    describe "queue_history" do
      before(:each) do
        @playlist = {
          :history => [],
        }
        pending("render call in helper makes things barf")
        @html = queue_history
      end

      it "should render the queue history" do
        @html.should have_tag("ul")
      end
    end

    describe "limit_string" do
      before(:each) do
        @string = "testing limit_string"
        @len = 10
        @result = limit_string(@string, @len)
      end

      it "should not modify a short string" do
        @string = "short"
        @result = limit_string(@string, @len)
        @result.should == @string
      end

      it "should modify a long string" do
        @string = "a " * (@len + 1)
        @result = limit_string(@string, @len)
        @result.should_not == @string
      end

      it "should have an ellipsis on the end of a shortened string" do
        @result.should match(/…$/)
      end
    end

    describe "classify_media" do
      it "should find 2 CDs" do
        @media = [
          mock_model(Medium, :medium => "CD5", :format => "LP"),
          mock_model(Medium, :medium => "CD3", :format => "SP")
        ]
        classify_media(@media)[:cds].should have(2).items
        classify_media(@media)[:internet].should be_false
      end

      it "should find Internet tracks" do
        @media = [
          mock_model(Medium, :medium => "Internet", :format => nil)
        ]
        classify_media(@media)[:cds].should have(0).items
        classify_media(@media)[:internet].should be_true
      end

      it "should find both a CD and Internet tracks" do
        @media = [
          mock_model(Medium, :medium => "CD5", :format => "LP"),
          mock_model(Medium, :medium => "Internet"),
        ]
        classify_media(@media)[:cds].should have(1).item
        classify_media(@media)[:internet].should be_true
      end
    end

    describe "build_media_text" do
      it "should report a three-disc album" do
        @classes = {
          :cds => ["LP", "LP", "SP"],
          :internet => false,
        }
        build_media_text(@classes).should include("3×CD")
        build_media_text(@classes).should have(1).item
      end

      it "should report a single disc album" do
        @classes = {
          :cds => ["EP"],
          :internet => false,
        }
        build_media_text(@classes).should include("EP")
        build_media_text(@classes).should have(1).item
      end

      it "should report a single disc album, with Internet tracks" do
        @classes = {
          :cds => ["SP"],
          :internet => true,
        }
        build_media_text(@classes).should include("SP")
        build_media_text(@classes).should include("Internet")
        build_media_text(@classes).should have(2).items
      end
    end

    describe "album_format" do
      before(:each) do
        self.stub!(:classify_media).and_return({})
        self.stub!(:build_media_text).and_return("")
        self.stub!(:logger).and_return(mock(Logger, :debug => nil))
        @text = album_format(mock_model(Album, :media => nil))
      end

      it "should return a ? when no media are found" do
        @text.should == "?"
      end

      it "should join the results with a comma" do
        self.stub!(:build_media_text).and_return(["foo", "bar"])
        @text = album_format(mock_model(Album, :media => nil))
        @text.should == "foo, bar"
      end
    end

    describe "divide_object" do
      before(:each) do
        @objects = (1..8).to_a
        @divided = divide_objects(@objects)
      end

      it "should return a list of size 2" do
        @divided.should have(2).items
      end

      it "should return lists" do
        @divided.each {|x| x.should be_an_instance_of(Array)}
      end

      it "should return equal-sized lists when @objects.size is even" do
        @divided[0].size.should == @divided[1].size
      end

      it "should have lists that differ by only 1 when @objects.size is odd" do
        @divided = divide_objects(@objects << 9)
        @divided[0].size.should == @divided[1].size + 1
      end

      it "should divide properly" do
        @divided[0].should == (1..4).to_a
        @divided[1].should == (5..8).to_a
      end
    end

    describe "format_last_played" do
      it "should return \"More than a month ago.\"" do
        @text = format_last_played(1.month.ago - 1.second)
        @text.should == "More than a month ago."
      end

      it "should return ? when nil" do
        format_last_played(nil).should == "?"
      end

      it "should be yesterday" do
        format_last_played(Time.now.yesterday).should include("Yesterday")
      end

      it "should be today" do
        format_last_played(Time.now.midnight).should include("Today")
      end

      it "should be x hours ago" do
        format_last_played(7.days.ago).should == "7 days ago."
      end

      it "should be \"More than a month ago\"" do
        format_last_played(1.month.ago).should == "More than a month ago."
      end
    end

    describe "formatted_time" do
      it "should not have an hour spot" do
        formatted_time(59.minutes + 59.seconds).should == "59:59"
      end

      it "should have an hour spot" do
        formatted_time(60.minutes + 1.seconds).should == "1:00:01"
      end
    end

    describe "get_title" do
      it "should return \"Mildred\" by default" do
        @title = nil
        get_title.should == "Mildred"
      end

      it "should append \"Mildred\" when a value is given" do
        @title = "Foo"
        get_title.should include("Foo")
        get_title.should match(/- Mildred$/)
      end
    end

    describe "logged_in?" do
      it "should return a boolean" do
        [true, false].should include(logged_in?)
      end
    end

    describe "current_user" do
      it "should delegate to its controller" do
        @controller.should_receive(:send).with(:current_user)
        current_user
      end
    end

    describe "playlist_track_queue_id" do
      before(:each) do
        self.stub!(:playlist_track).and_return("<!-- playlist track -->")
        @user = mock_model(User, valid_user_attributes)
        self.stub!(:current_user).and_return(@user)
        @track = mock_model(Track, valid_track_attributes)
        @track.stub!(:last_played)
      end

      describe "with an user that has dequeue, but not reorder perms" do
        before(:each) do
          @user.stub!(:has_perm?).with(:reorder).and_return(false)
          @user.stub!(:has_perm?).with(:dequeue).and_return(true)
          @html = playlist_track_queue_id(@track, 1)
        end

        it "should include a draggable element" do
          @html.should include("Draggable")
        end
      end

      describe "with a normal user" do
        before(:each) do
          @user.stub!(:has_perm?).with(:reorder).and_return(false)
          @user.stub!(:has_perm?).with(:dequeue).and_return(false)
          @html = playlist_track_queue_id(@track, 1)
        end

        it "should return a LI element" do
          @html.should have_tag("li", :id => @track.id)
        end

        it "should not include a draggable element" do
          @html.should_not include("Draggable")
        end
      end
    end

    describe "link_to_album" do
      it "should set a link title" do
        @album = mock_model(Album, valid_album_attributes)
        @link = link_to_album(@album)
        @link.should have_tag("a", :title => /Album: .*/)
      end
    end

    describe "link_to_artist" do
      it "should set a link title" do
        @artist = mock_model(Artist, valid_artist_attributes)
        @link = link_to_artist(@artist)
        @link.should have_tag("a", :title => /Artist: .*/)
      end
    end

    describe "link_to_mood" do
      it "should set a link title" do
        @mood = mock_model(Mood, valid_mood_attributes)
        @link = link_to_mood(@mood)
        @link.should have_tag("a", :title => /Mood: .*/)
      end
    end

    describe "smart_quote" do
      it "should be trivial" do
        smart_quote("foo").should == "&ldquo;foo&rdquo;"
      end
    end

    describe "histogram" do
      before(:each) do
        @html = histogram(300, 60)
      end

      it_should_behave_like "a bar graph"
    end

    describe "rank_graph" do
      before(:each) do
        @html = rank_graph("10th", 60)
      end

      it "should have a span with the rank in it" do
        @html.should have_tag("span", :count => 1, :text => "10th")
      end

      it_should_behave_like "a bar graph"
    end

    describe "rating_graph" do
      before(:each) do
        @html = rating_graph(9.222)
      end

      it "should have a span with the rating in it" do
        @html.should have_tag("span", :count => 1, :text => "9.2/10")
      end

      it_should_behave_like "a bar graph"
      it "should handle nil ratings by replacing it with a \"?\"" do
        rating_graph(nil).should have_tag("span", :count => 1, :text => "?/10")
      end
    end

    describe "playlist_track" do
      it "should..." do
        pending("has renders, which cause things to barf")
      end
    end

    describe "image_dimensions" do
      it "should return \"<height>x<width>\"" do
        image = mock("image", :columns => 1, :rows => 2)
        Magick::Image.stub!(:read).and_return(mock("dimensions", :first => image))
        image_dimensions("foo").should == "1x2"
      end
    end

    describe "google_image_search" do
      it "should return an anchor tag" do
        google_image_search(["foo bar"]).should have_tag("a", "Google Image Search")
      end

      it "should use the \"new\" target to popup a new window or tab" do
        google_image_search(["foo bar"]).should have_tag("a", :target => "new")
      end
    end

    describe "rating_slider" do
      before(:each) do
        @track = mock_model(Track, valid_track_attributes)
        @playlist = {:now_playing => @track}
        @html = rating_slider(@playlist, 5)
      end

      it "should return a span element" do
        @html.should have_tag("span")
      end

      [:onclick, :onmouseover, :onmouseout].each do |event|
        it "should have an #{event} event callback" do
          @html.should have_tag("span", event => /.+/)
        end
      end
    end

    describe "artist_thumb when no art exists" do
      before(:each) do
        art = mock_model(Art, :public_filename => "noimage.png")
        @artist = mock_model(Artist, :art => [art], :name => "Foo Monkey")
        @html = artist_thumb(@artist)
      end

      it "should return an image element" do
        @html.should have_tag("img", 1)
      end

      it "should set the alt and title attributes" do
        @html.should have_tag("img", :alt => /.+/, :title => /.+/)
      end
    end

    describe "artist_thumb" do
      before(:each) do
        art = mock_model(Art, :public_filename => "has_art.jpg")
        @artist = mock_model(Artist, :art => [art], :name => "Foo Monkey")
        @html = artist_thumb(@artist)
      end

      it "should return an image element" do
        @html.should have_tag("img", 1)
      end

      it "should set the alt and title attributes" do
        @html.should have_tag("img", :alt => "Artist image: Foo Monkey", :title => /.*Foo Monkey.*/)
      end
    end

    describe "geenerate_alpha_indices" do

      def mock_albums
        [
          mock_model(Album, :title => "1 Doo", :sorted_title => "1 doo", :obtained => 1.days.ago),
          mock_model(Album, :title => "2 Foo", :sorted_title => "2 foo", :obtained => 1.days.ago),
          mock_model(Album, :title => "Doo", :sorted_title => "doo", :obtained => 4.days.ago),
          mock_model(Album, :title => "Foo", :sorted_title => "foo", :obtained => 4.days.ago),
          mock_model(Album, :title => "Goo", :sorted_title => "goo", :obtained => 8.days.ago),
          mock_model(Album, :title => "Poo", :sorted_title => "poo", :obtained => 8.days.ago),
          mock_model(Album, :title => "Roo", :sorted_title => "roo", :obtained => 8.days.ago),
          mock_model(Album, :title => "Zoo", :sorted_title => "zoo", :obtained => 8.days.ago),
        ]
      end

      before(:each) do
        @albums = mock_albums
        Album.stub!(:find).and_return(@albums)
        @links = generate_alpha_indices(@albums, :sorted_title)
      end

      it "should have the proper number of indices" do
        @links.size.should == (@albums.size - 1) # -1 because there are two #
      end

      it "should have a # link" do
        @links.should include("#")
      end

      it "should not have a # link" do
        @albums.slice!(2..-1)
        Album.stub!(:find).and_return(@albums)
        @links = generate_alpha_indices(@albums, :sorted_title)
        @links.should include("#")
      end
    end

  end
end