mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
add test to class of pagination links
This commit is contained in:
parent
e040d6fcce
commit
331218d1c3
85
test/adapter/json_api/pagination_links_test.rb
Normal file
85
test/adapter/json_api/pagination_links_test.rb
Normal file
@ -0,0 +1,85 @@
|
||||
require 'test_helper'
|
||||
require 'will_paginate/array'
|
||||
require 'kaminari'
|
||||
require 'kaminari/hooks'
|
||||
::Kaminari::Hooks.init
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class JsonApi
|
||||
class PaginationLinksTest < Minitest::Test
|
||||
def setup
|
||||
ActionController::Base.cache_store.clear
|
||||
@array = [
|
||||
Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||
Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
|
||||
Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
|
||||
]
|
||||
end
|
||||
|
||||
def using_kaminari
|
||||
Kaminari.paginate_array(@array).page(2).per(1)
|
||||
end
|
||||
|
||||
def using_will_paginate
|
||||
@array.paginate(page: 2, per_page: 1)
|
||||
end
|
||||
|
||||
def expected_response_without_pagination_links
|
||||
{
|
||||
data: [{
|
||||
id:"2",
|
||||
type:"profiles",
|
||||
attributes:{
|
||||
name:"Name 2",
|
||||
description:"Description 2"
|
||||
}
|
||||
}]
|
||||
}
|
||||
end
|
||||
|
||||
def expected_response_with_pagination_links
|
||||
{
|
||||
data: [{
|
||||
id:"2",
|
||||
type:"profiles",
|
||||
attributes:{
|
||||
name:"Name 2",
|
||||
description:"Description 2"
|
||||
}
|
||||
}],
|
||||
links:{
|
||||
first:"?page=1&per_page=1",
|
||||
prev:"?page=1&per_page=1",
|
||||
next:"?page=3&per_page=1",
|
||||
last:"?page=3&per_page=1"
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def test_pagination_links_using_kaminari
|
||||
serializer = ArraySerializer.new(using_kaminari, pagination: true)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
|
||||
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
|
||||
end
|
||||
|
||||
def test_pagination_links_using_will_paginate
|
||||
serializer = ArraySerializer.new(using_will_paginate, pagination: true)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
|
||||
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
|
||||
end
|
||||
|
||||
def test_not_showing_pagination_links
|
||||
serializer = ArraySerializer.new(using_will_paginate, pagination: false)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
|
||||
assert_equal expected_response_without_pagination_links, adapter.serializable_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user