|
| 1 | +require 'test_helper' |
| 2 | +require 'will_paginate/array' |
| 3 | +require 'kaminari' |
| 4 | +require 'kaminari/hooks' |
| 5 | +::Kaminari::Hooks.init |
| 6 | + |
| 7 | +module ActiveModel |
| 8 | + class Serializer |
| 9 | + class Adapter |
| 10 | + class JsonApi |
| 11 | + class PaginationLinksTest < Minitest::Test |
| 12 | + def setup |
| 13 | + ActionController::Base.cache_store.clear |
| 14 | + @array = [ |
| 15 | + Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), |
| 16 | + Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }), |
| 17 | + Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' }) |
| 18 | + ] |
| 19 | + end |
| 20 | + |
| 21 | + def using_kaminari |
| 22 | + Kaminari.paginate_array(@array).page(2).per(1) |
| 23 | + end |
| 24 | + |
| 25 | + def using_will_paginate |
| 26 | + @array.paginate(page: 2, per_page: 1) |
| 27 | + end |
| 28 | + |
| 29 | + def expected_response_without_pagination_links |
| 30 | + { |
| 31 | + data: [{ |
| 32 | + id:"2", |
| 33 | + type:"profiles", |
| 34 | + attributes:{ |
| 35 | + name:"Name 2", |
| 36 | + description:"Description 2" |
| 37 | + } |
| 38 | + }] |
| 39 | + } |
| 40 | + end |
| 41 | + |
| 42 | + def expected_response_with_pagination_links |
| 43 | + { |
| 44 | + data: [{ |
| 45 | + id:"2", |
| 46 | + type:"profiles", |
| 47 | + attributes:{ |
| 48 | + name:"Name 2", |
| 49 | + description:"Description 2" |
| 50 | + } |
| 51 | + }], |
| 52 | + links:{ |
| 53 | + first:"?page=1&per_page=1", |
| 54 | + prev:"?page=1&per_page=1", |
| 55 | + next:"?page=3&per_page=1", |
| 56 | + last:"?page=3&per_page=1" |
| 57 | + } |
| 58 | + } |
| 59 | + end |
| 60 | + |
| 61 | + def test_pagination_links_using_kaminari |
| 62 | + serializer = ArraySerializer.new(using_kaminari, pagination: true) |
| 63 | + adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) |
| 64 | + |
| 65 | + assert_equal expected_response_with_pagination_links, adapter.serializable_hash |
| 66 | + end |
| 67 | + |
| 68 | + def test_pagination_links_using_will_paginate |
| 69 | + serializer = ArraySerializer.new(using_will_paginate, pagination: true) |
| 70 | + adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) |
| 71 | + |
| 72 | + assert_equal expected_response_with_pagination_links, adapter.serializable_hash |
| 73 | + end |
| 74 | + |
| 75 | + def test_not_showing_pagination_links |
| 76 | + serializer = ArraySerializer.new(using_will_paginate, pagination: false) |
| 77 | + adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) |
| 78 | + |
| 79 | + assert_equal expected_response_without_pagination_links, adapter.serializable_hash |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | +end |
0 commit comments