spec/paginate/action_view_spec.rb in paginate-1.0.1 vs spec/paginate/action_view_spec.rb in paginate-2.0.0

- old
+ new

@@ -7,11 +7,14 @@ @params = Hash.new @controller = ThingsController.new @controller.params = @params - @view = ActionView::Base.new + @view = ActionView::Base.new( + File.expand_path("../../support/views", __FILE__) + ) + @view.lookup_context.prefixes << "application" @view.controller = @controller @view.extend(Paginate::Helper) @view.stub :request => @request @helper = Object.new @@ -23,138 +26,147 @@ end I18n.locale = :en end - it "should display pagination list" do + it "displays pagination list" do @request.fullpath = "/some/path?page=1" html = render(:default, []) - html.css("ul.paginate").count.should == 1 - html.css("ul.paginate > li").count.should == 3 + expect(html.css("ul.paginate").count).to eql(1) + expect(html.css("ul.paginate > li").count).to eql(3) end - it "should add .disabled class when have no items" do + it "adds .disabled class when have no items" do @request.fullpath = "/some/path" html = render(:default, []) - html.css("ul.paginate.disabled").first.should_not be_nil + expect(html.css("ul.paginate.disabled").first).to be end - it "should display next page link" do + it "displays next page link" do @request.fullpath = "/some/path?page=1" html = render(:default, Array.new(11)) link = html.css("li.next-page > a").first - link.should_not be_nil - link["href"].should == "/some/path?page=2" - link.text.should == "Next page" + expect(link).to be + expect(link["href"]).to eql("/some/path?page=2") + expect(link.text).to eql("Next page") end - it "should display next page link using proc as url" do + it "displays next page link using proc as url" do @request.fullpath = "/some/path?page=1" html = render(:block_as_url, Array.new(11)) link = html.css("li.next-page > a").first - link.should_not be_nil - link["href"].should == "/some/path/2" - link.text.should == "Next page" + expect(link).to be + expect(link["href"]).to eql("/some/path/2") + expect(link.text).to eql("Next page") end - it "should display previous page link" do + it "displays previous page link" do @params[:page] = 2 @request.fullpath = "/some/path?page=2" html = render(:default, Array.new(11)) link = html.css("li.previous-page > a").first - link.should_not be_nil - link["href"].should == "/some/path?page=1" - link.text.should == "Previous page" + expect(link).to be + expect(link["href"]).to eql("/some/path?page=1") + expect(link.text).to eql("Previous page") end - it "should disable element when have no next page" do + it "disables element when have no next page" do @request.fullpath = "/some/path?page=1" html = render(:default, Array.new(10)) link = html.css("li.next-page > a").first span = html.css("li.next-page.disabled > span").first - link.should be_nil - span.should_not be_nil - span.text.should == "Next page" + expect(link).not_to be + expect(span).to be + expect(span.text).to eql("Next page") end - it "should disable element when have no previous page" do + it "disables element when have no previous page" do @request.fullpath = "/some/path?page=1" html = render(:default, Array.new(10)) link = html.css("li.previous-page > a").first span = html.css("li.previous-page.disabled > span").first - link.should be_nil - span.should_not be_nil - span.text.should == "Previous page" + expect(link).not_to be + expect(span).to be + expect(span.text).to eql("Previous page") end - it "should display current page" do + it "displays current page" do @params[:page] = 10 @request.fullpath = "/some/path?page=10" html = render(:default, []) span = html.css("li.page > span").first - span.should_not be_nil - span.text.should == "Page 10" + expect(span).to be + expect(span.text).to eql("Page 10") end - it "should translate strings" do + it "overrides render method" do + items = [*1..11].map do |i| + OpenStruct.new(:to_partial_path => "number", :value => i) + end + + html = render(:render, items) + end + + it "translates strings" do I18n.locale = :"pt-BR" @params[:page] = 10 @request.fullpath = "/some/path?page=10" html = render(:default, Array.new(11)) - html.css("li.page > span").text.should == "Página 10" - html.css("li.next-page > a").text.should == "Próxima página" - html.css("li.previous-page > a").text.should == "Página anterior" + expect(html.css("li.page > span").text).to eql("Página 10") + expect(html.css("li.next-page > a").text).to eql("Próxima página") + expect(html.css("li.previous-page > a").text).to eql("Página anterior") end - it "should skip the last item while iterating" do + it "skips the last item while iterating" do values = [] items = Array.new(11) {|i| "User#{i}" } @helper.iterate items do |item| values << item end - values.should == items[0, 10] + expect(values).to eql(items[0, 10]) end - it "should iterate all items when have less records than size" do + it "iterates all items when have less records than size" do values = [] items = Array.new(8) {|i| "User#{i}" } @helper.iterate items do |item| values << item end - values.should == items[0, 8] + expect(values).to eql(items[0, 8]) end - it "should yield iteration counter" do + it "yields iteration counter" do values = [] indices = [] items = Array.new(11) {|i| "User#{i}" } @helper.iterate items do |item, i| values << item indices << i end - values.should == items[0, 10] - indices.should == (0...10).to_a + expect(values).to eql(items[0, 10]) + expect(indices).to eql([*0...10]) end private def render(view_name, items) - Nokogiri @view.render(:inline => load_view(view_name), :locals => {:items => items}) + view_info = Struct.new(:to_partial_path).new("#{view_name}") + Nokogiri @view.render(view_info, :items => items) end def load_view(name) File.read(File.dirname(__FILE__) + "/../support/views/#{name}.erb") end