-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from phantomphildius/page-count
Add page_count when using Paginators
- Loading branch information
Showing
8 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
require 'rails_helper' | ||
|
||
## | ||
# Configs | ||
## | ||
|
||
# Resource | ||
class PageCountTestResource < JSONAPI::Resource; end | ||
|
||
# Controller | ||
class PageCountTestController < BaseController | ||
def index | ||
jsonapi_render json: User.all, options: { resource: UserResource } | ||
end | ||
end | ||
|
||
# Routes | ||
def TestApp.draw_page_count_test_routes | ||
JSONAPI.configuration.json_key_format = :underscored_key | ||
|
||
TestApp.routes.draw do | ||
controller :page_count_test do | ||
get :index | ||
end | ||
end | ||
end | ||
|
||
## | ||
# Feature Tests | ||
## | ||
|
||
|
||
describe PageCountTestController, type: :controller do | ||
include_context 'JSON API headers' | ||
|
||
before(:all) do | ||
TestApp.draw_page_count_test_routes | ||
FactoryGirl.create_list(:user, 3, :with_posts) | ||
end | ||
|
||
describe 'page count with a paged paginator' do | ||
it 'returns the correct count' do | ||
JSONAPI.configuration.default_paginator = :paged | ||
|
||
get :index, params: { page: { size: 2, number: 1 } } | ||
|
||
expect(json.dig('meta', 'page_count')).to eq(2) | ||
end | ||
end | ||
|
||
describe 'page count with an offset paginator' do | ||
it 'returns the correct count' do | ||
JSONAPI.configuration.default_paginator = :offset | ||
|
||
get :index, params: { page: { limit: 2, offset: 1 } } | ||
|
||
expect(json.dig('meta', 'page_count')).to eq(2) | ||
end | ||
end | ||
|
||
describe 'page count with a custom paginator' do | ||
it 'returns the correct count' do | ||
JSONAPI.configuration.default_paginator = :custom_offset | ||
|
||
get :index, params: { page: { limit: 2, offset: 1 } } | ||
|
||
expect(json.dig('meta', 'page_count')).to eq(2) | ||
end | ||
end | ||
|
||
describe 'using default limit param' do | ||
it 'returns the correct count' do | ||
JSONAPI.configuration.default_paginator = :offset | ||
|
||
get :index, params: { page: { offset: 1 } } | ||
|
||
expect(json.dig('meta', 'page_count')).to eq(1) | ||
end | ||
end | ||
|
||
describe 'using a custom page_count key' do | ||
it 'returns the count with the correct key' do | ||
JSONAPI.configuration.default_paginator = :paged | ||
JSONAPI.configuration.top_level_meta_page_count_key = :total_pages | ||
|
||
get :index, params: { page: { limit: 2, offset: 1 } } | ||
|
||
expect(json.dig('meta', 'total_pages')).to eq(2) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters