spec/collection_spec.rb in zendesk_api-0.1.2 vs spec/collection_spec.rb in zendesk_api-0.1.3
- old
+ new
@@ -86,10 +86,55 @@
end
end
end
end
+ context "pagination with data" do
+ before(:each) do
+ stub_json_request(:get, %r{test_resources}, json(
+ :test_resources => [{ :id => 1 }]
+ ))
+ subject.fetch(true)
+ end
+
+ context "on #page" do
+ context "with nil" do
+ before(:each) { subject.page(nil) }
+
+ it "should not empty the cache" do
+ subject.instance_variable_get(:@resources).should_not be_empty
+ end
+ end
+
+ context "with a number" do
+ before(:each) { subject.page(3) }
+
+ it "should empty the cache" do
+ subject.instance_variable_get(:@resources).should be_nil
+ end
+ end
+ end
+
+ context "on #per_page" do
+ context "with nil" do
+ before(:each) { subject.per_page(nil) }
+
+ it "should not empty the cache" do
+ subject.instance_variable_get(:@resources).should_not be_empty
+ end
+ end
+
+ context "with a number" do
+ before(:each) { subject.per_page(20) }
+
+ it "should empty the cache" do
+ subject.instance_variable_get(:@resources).should be_nil
+ end
+ end
+ end
+ end
+
context "pagination with no options and no data" do
it "should return an empty array on #next" do
subject.next.should be_empty
end
@@ -118,10 +163,103 @@
it "should decreate page option" do
subject.prev.should == 1
end
end
+ context "each_page" do
+ before(:each) do
+ stub_json_request(:get, %r{test_resources$}, json(
+ :test_resources => [{:id => 1}],
+ :next_page => "/test_resources?page=2"
+ ))
+
+ stub_json_request(:get, %r{test_resources\?page=2}, json(
+ :test_resources => [{:id => 2}],
+ :next_page => "/test_resources?page=3"
+ ))
+
+ stub_request(:get, %r{test_resources\?page=3}).to_return(:status => 404)
+ end
+
+ it "should yield resource if arity == 1" do
+ expect do |block|
+ # Needed to make sure the arity == 1
+ block.instance_eval do
+ def to_proc
+ @used = true
+
+ probe = self
+ Proc.new do |arg|
+ probe.num_yields += 1
+ probe.yielded_args << [arg]
+ end
+ end
+ end
+
+ silence_logger { subject.each_page(&block) }
+ end.to yield_successive_args(
+ ZendeskAPI::TestResource.new(client, :id => 1),
+ ZendeskAPI::TestResource.new(client, :id => 2)
+ )
+ end
+
+ it "should yield resource and page" do
+ expect do |b|
+ silence_logger { subject.each_page(&b) }
+ end.to yield_successive_args(
+ [ZendeskAPI::TestResource.new(client, :id => 1), 1],
+ [ZendeskAPI::TestResource.new(client, :id => 2), 2]
+ )
+ end
+ end
+
context "fetch" do
+ context "grabbing the current page" do
+ context "from next_page" do
+ before(:each) do
+ stub_json_request(:get, %r{test_resources}, json(
+ :test_resources => [{:id => 2}],
+ :next_page => "/test_resources?page=2"
+ ))
+
+ subject.fetch(true)
+ @page = subject.instance_variable_get(:@options)["page"]
+ end
+
+ it "should set the page to 1" do
+ @page.should == 1
+ end
+ end
+
+ context "from prev_page" do
+ before(:each) do
+ stub_json_request(:get, %r{test_resources}, json(
+ :test_resources => [{:id => 2}],
+ :previous_page => "/test_resources?page=1"
+ ))
+
+ subject.fetch(true)
+ @page = subject.instance_variable_get(:@options)["page"]
+ end
+
+ it "should set the page to 2" do
+ @page.should == 2
+ end
+ end
+
+ context "with nothing" do
+ before(:each) do
+ stub_json_request(:get, %r{test_resources}, json(:test_resources => [{:id => 2}]))
+ subject.fetch(true)
+ @page = subject.instance_variable_get(:@options)["page"]
+ end
+
+ it "should not set the page" do
+ @page.should be_nil
+ end
+ end
+ end
+
it "does not fetch if associated is a new record" do
ZendeskAPI::Category.new(client).forums.fetch.should == []
ZendeskAPI::Category.new(client).forums.to_a.should == []
end