Skip to content

Commit

Permalink
Fix jsonapi prev and next keys for unavailable links (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobusjoubert committed Mar 16, 2024
1 parent 2e41f32 commit 74b4e50
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/pagy/extras/jsonapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ module BackendOverride
def pagy_jsonapi_links(pagy, **opts)
{ first: pagy_url_for(pagy, 1, **opts),
last: pagy_url_for(pagy, pagy.last, **opts),
prev: pagy_url_for(pagy, pagy.prev, **opts),
next: pagy_url_for(pagy, pagy.next, **opts) }
prev: pagy.prev ? pagy_url_for(pagy, pagy.prev, **opts) : nil,
next: pagy.next ? pagy_url_for(pagy, pagy.next, **opts) : nil }
end

# Should skip the jsonapi
Expand Down
34 changes: 23 additions & 11 deletions test/pagy/extras/jsonapi_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
_ { _pagy, _records = app.send(:pagy, @collection) }.must_raise Pagy::JsonApiExtra::ReservedParamError
end

describe "JsonApi" do
describe 'JsonApi' do
it 'uses the :jsonapi with page:nil' do
app = MockApp.new(params: { page: nil })
pagy, _records = app.send(:pagy, @collection, items_extra: false)
Expand All @@ -37,46 +37,58 @@
_(app.send(:pagy_url_for, pagy, 2)).must_rematch :url_2
end
end
describe "Skip JsonApi" do
describe 'Skip JsonApi' do
it 'skips the :jsonapi with page:nil' do
Pagy::DEFAULT[:jsonapi] = false
app = MockApp.new(params: { page: nil })
pagy, _records = app.send(:pagy, @collection, items_extra: false)
_(app.send(:pagy_url_for, pagy, 1)).must_equal "/foo?page=1"
_(app.send(:pagy_url_for, pagy, 1)).must_equal '/foo?page=1'
pagy, _records = app.send(:pagy, @collection)
_(app.send(:pagy_url_for, pagy, 1)).must_equal "/foo?page=1&items=20"
_(app.send(:pagy_url_for, pagy, 1)).must_equal '/foo?page=1&items=20'
Pagy::DEFAULT[:jsonapi] = true
end
it 'skips the :jsonapi with page:3' do
Pagy::DEFAULT[:jsonapi] = false
app = MockApp.new(params: { page: 3 })
pagy, _records = app.send(:pagy, @collection, items_extra: false)
_(app.send(:pagy_url_for, pagy, 2)).must_equal "/foo?page=2"
_(app.send(:pagy_url_for, pagy, 2)).must_equal '/foo?page=2'
pagy, _records = app.send(:pagy, @collection)
_(app.send(:pagy_url_for, pagy, 2)).must_equal "/foo?page=2&items=20"
_(app.send(:pagy_url_for, pagy, 2)).must_equal '/foo?page=2&items=20'
Pagy::DEFAULT[:jsonapi] = true
end
end
describe "JsonApi with custom named params" do
it "gets custom named params" do
describe 'JsonApi with custom named params' do
it 'gets custom named params' do
app = MockApp.new(params: { page: { number: 3, size: 10 } })
pagy, _records = app.send(:pagy, @collection, page_param: :number, items_param: :size)
_(pagy.page).must_equal 3
_(pagy.items).must_equal 10
end
it "sets custom named params" do
it 'sets custom named params' do
app = MockApp.new(params: { page: { number: 3, size: 10 } })
pagy, _records = app.send(:pagy, @collection, page_param: :number, items_param: :size)
_(app.send(:pagy_url_for, pagy, 4)).must_rematch :url
end
end
describe "#pagy_jsonapi_links" do
it "returns the ordered links" do
describe '#pagy_jsonapi_links' do
it 'returns the ordered links' do
app = MockApp.new(params: { page: { number: 3, size: 10 } })
pagy, _records = app.send(:pagy, @collection, page_param: :number, items_param: :size)
result = app.send(:pagy_jsonapi_links, pagy)
_(result.keys).must_equal %i[first last prev next] # not sure it's a requirementS
_(result).must_rematch :result
end
it 'sets the prev value to null when the link is unavailable' do
app = MockApp.new(params: { page: { page: 1 } })
pagy, _records = app.send(:pagy, @collection)
result = app.send(:pagy_jsonapi_links, pagy)
_(result[:prev]).must_be_nil
end
it 'sets the next value to null when the link is unavailable' do
app = MockApp.new(params: { page: { page: 50 } })
pagy, _records = app.send(:pagy, @collection)
result = app.send(:pagy_jsonapi_links, pagy)
_(result[:next]).must_be_nil
end
end
end

0 comments on commit 74b4e50

Please sign in to comment.