1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
# frozen_string_literal: true
require 'test_helper'
require 'will_paginate/array'
require 'kaminari'
require 'kaminari/hooks'
::Kaminari::Hooks.init
module ActiveModelSerializers
module Adapter
class JsonApi
class PaginationLinksTest < ActiveSupport::TestCase
URI = 'http://example.com'.freeze
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'),
Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
]
end
def mock_request(query_parameters = {}, original_url = URI)
context = Minitest::Mock.new
context.expect(:request_url, original_url)
context.expect(:query_parameters, query_parameters)
context.expect(:key_transform, nil)
end
def load_adapter(paginated_collection, mock_request = nil)
render_options = { adapter: :json_api }
render_options[:serialization_context] = mock_request if mock_request
serializable(paginated_collection, render_options)
end
def using_kaminari(page = 2)
Kaminari.paginate_array(@array).page(page).per(2)
end
def using_will_paginate(page = 2)
@array.paginate(page: page, per_page: 2)
end
def data
{
data: [
{ id: '1', type: 'profiles', attributes: { name: 'Name 1', description: 'Description 1' } },
{ id: '2', type: 'profiles', attributes: { name: 'Name 2', description: 'Description 2' } },
{ id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } },
{ id: '4', type: 'profiles', attributes: { name: 'Name 4', description: 'Description 4' } },
{ id: '5', type: 'profiles', attributes: { name: 'Name 5', description: 'Description 5' } }
]
}
end
def empty_collection_links
{
self: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
prev: nil,
next: nil,
last: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2"
}
end
def links
{
links: {
self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2"
}
}
end
def last_page_links
{
links: {
self: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
prev: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
next: nil,
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2"
}
}
end
def expected_response_when_unpaginatable
data
end
def expected_response_with_pagination_links
{}.tap do |hash|
hash[:data] = data.values.flatten[2..3]
hash.merge! links
end
end
def expected_response_without_pagination_links
{}.tap do |hash|
hash[:data] = data.values.flatten[2..3]
end
end
def expected_response_with_pagination_links_and_additional_params
new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" }
{}.tap do |hash|
hash[:data] = data.values.flatten[2..3]
hash.merge! links: new_links
end
end
def expected_response_with_last_page_pagination_links
{}.tap do |hash|
hash[:data] = [data.values.flatten.last]
hash.merge! last_page_links
end
end
def expected_response_with_empty_collection_pagination_links
{}.tap do |hash|
hash[:data] = []
hash.merge! links: empty_collection_links
end
end
def test_pagination_links_using_kaminari
adapter = load_adapter(using_kaminari, mock_request)
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
end
def test_pagination_links_using_will_paginate
adapter = load_adapter(using_will_paginate, mock_request)
assert_equal expected_response_with_pagination_links, adapter.serializable_hash
end
def test_pagination_links_with_additional_params
adapter = load_adapter(using_will_paginate, mock_request(test: 'test'))
assert_equal expected_response_with_pagination_links_and_additional_params,
adapter.serializable_hash
end
def test_pagination_links_when_zero_results_kaminari
@array = []
adapter = load_adapter(using_kaminari(1), mock_request)
assert_equal expected_response_with_empty_collection_pagination_links, adapter.serializable_hash
end
def test_pagination_links_when_zero_results_will_paginate
@array = []
adapter = load_adapter(using_will_paginate(1), mock_request)
assert_equal expected_response_with_empty_collection_pagination_links, adapter.serializable_hash
end
def test_last_page_pagination_links_using_kaminari
adapter = load_adapter(using_kaminari(3), mock_request)
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash
end
def test_last_page_pagination_links_using_will_paginate
adapter = load_adapter(using_will_paginate(3), mock_request)
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash
end
def test_not_showing_pagination_links
adapter = load_adapter(@array, mock_request)
assert_equal expected_response_when_unpaginatable, adapter.serializable_hash
end
def test_raises_descriptive_error_when_serialization_context_unset
render_options = { adapter: :json_api }
adapter = serializable(using_kaminari, render_options)
exception_class = ActiveModelSerializers::Adapter::JsonApi::PaginationLinks::MissingSerializationContextError
exception = assert_raises(exception_class) do
adapter.as_json
end
assert_equal exception_class, exception.class
assert_match(/CollectionSerializer#paginated\?/, exception.message)
end
def test_pagination_links_not_present_when_disabled
ActiveModel::Serializer.config.jsonapi_pagination_links_enabled = false
adapter = load_adapter(using_kaminari, mock_request)
assert_equal expected_response_without_pagination_links, adapter.serializable_hash
ensure
ActiveModel::Serializer.config.jsonapi_pagination_links_enabled = true
end
end
end
end
end
|