@@ -9,6 +9,8 @@ class Serializer
9
9
class Adapter
10
10
class JsonApi
11
11
class PaginationLinksTest < Minitest ::Test
12
+ URI = 'http://example.com'
13
+
12
14
def setup
13
15
ActionController ::Base . cache_store . clear
14
16
@array = [
@@ -18,6 +20,14 @@ def setup
18
20
]
19
21
end
20
22
23
+ def mock_request ( query_parameters = { } , original_url = URI )
24
+ context = Minitest ::Mock . new
25
+ context . expect ( :original_url , original_url )
26
+ context . expect ( :query_parameters , query_parameters )
27
+ @options = { }
28
+ @options [ :context ] = context
29
+ end
30
+
21
31
def using_kaminari
22
32
Kaminari . paginate_array ( @array ) . page ( 2 ) . per ( 1 )
23
33
end
@@ -38,11 +48,11 @@ def data
38
48
def links
39
49
{
40
50
links :{
41
- self : "http://example.com ?page%5Bnumber%5D=2&page%5Bsize%5D=1" ,
42
- first : "http://example.com ?page%5Bnumber%5D=1&page%5Bsize%5D=1" ,
43
- prev : "http://example.com ?page%5Bnumber%5D=1&page%5Bsize%5D=1" ,
44
- next : "http://example.com ?page%5Bnumber%5D=3&page%5Bsize%5D=1" ,
45
- last : "http://example.com ?page%5Bnumber%5D=3&page%5Bsize%5D=1"
51
+ self : "#{ URI } ?page%5Bnumber%5D=2&page%5Bsize%5D=1" ,
52
+ first : "#{ URI } ?page%5Bnumber%5D=1&page%5Bsize%5D=1" ,
53
+ prev : "#{ URI } ?page%5Bnumber%5D=1&page%5Bsize%5D=1" ,
54
+ next : "#{ URI } ?page%5Bnumber%5D=3&page%5Bsize%5D=1" ,
55
+ last : "#{ URI } ?page%5Bnumber%5D=3&page%5Bsize%5D=1"
46
56
}
47
57
}
48
58
end
@@ -59,7 +69,7 @@ def expected_response_with_pagination_links
59
69
end
60
70
61
71
def expected_response_with_pagination_links_and_additional_params
62
- new_links = links [ :links ] . each_with_object ( { } ) { |( key , value ) , hash | hash [ key ] = "#{ value } &teste=teste " }
72
+ new_links = links [ :links ] . each_with_object ( { } ) { |( key , value ) , hash | hash [ key ] = "#{ value } &test=test " }
63
73
{ } . tap do |hash |
64
74
hash [ :data ] = [ data . values . flatten . second ]
65
75
hash . merge! links : new_links
@@ -70,25 +80,25 @@ def test_pagination_links_using_kaminari
70
80
serializer = ArraySerializer . new ( using_kaminari )
71
81
adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new ( serializer )
72
82
73
- assert_equal expected_response_with_pagination_links ,
74
- adapter . serializable_hash ( pagination : { original_url : "http://example.com" } )
83
+ mock_request
84
+ assert_equal expected_response_with_pagination_links , adapter . serializable_hash ( @options )
75
85
end
76
86
77
87
def test_pagination_links_using_will_paginate
78
88
serializer = ArraySerializer . new ( using_will_paginate )
79
89
adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new ( serializer )
80
90
81
- assert_equal expected_response_with_pagination_links ,
82
- adapter . serializable_hash ( pagination : { original_url : "http://example.com" } )
91
+ mock_request
92
+ assert_equal expected_response_with_pagination_links , adapter . serializable_hash ( @options )
83
93
end
84
94
85
95
def test_pagination_links_with_additional_params
86
96
serializer = ArraySerializer . new ( using_will_paginate )
87
97
adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new ( serializer )
88
- assert_equal expected_response_with_pagination_links_and_additional_params ,
89
- adapter . serializable_hash ( pagination : { original_url : "http://example.com" ,
90
- query_parameters : { teste : "teste" } } )
91
98
99
+ mock_request ( { test : 'test' } )
100
+ assert_equal expected_response_with_pagination_links_and_additional_params ,
101
+ adapter . serializable_hash ( @options )
92
102
end
93
103
94
104
def test_not_showing_pagination_links
0 commit comments