@@ -4,16 +4,18 @@ module ActiveModel
44 class Serializer
55 class MetaTest < Minitest ::Test
66 def setup
7- ActionController ::Base . cache_store . clear
87 @blog = Blog . new ( id : 1 ,
98 name : 'AMS Hints' ,
109 writer : Author . new ( id : 2 , name : 'Steve' ) ,
1110 articles : [ Post . new ( id : 3 , title : 'AMS' ) ] )
1211 end
1312
1413 def test_meta_is_present_with_root
15- serializer = AlternateBlogSerializer . new ( @blog , meta : { total : 10 } )
16- adapter = ActiveModel ::Serializer ::Adapter ::Json . new ( serializer )
14+ actual = ActiveModel ::SerializableResource . new ( @blog ,
15+ adapter : :json ,
16+ serializer : AlternateBlogSerializer ,
17+ meta : { total : 10 } )
18+ . as_json
1719 expected = {
1820 blog : {
1921 id : 1 ,
@@ -23,22 +25,29 @@ def test_meta_is_present_with_root
2325 total : 10
2426 }
2527 }
26- assert_equal expected , adapter . as_json
28+ assert_equal expected , actual
2729 end
2830
2931 def test_meta_is_not_included_when_root_is_missing
30- # load_adapter uses Attributes Adapter
31- adapter = load_adapter ( meta : { total : 10 } )
32+ actual = ActiveModel ::SerializableResource . new ( @blog ,
33+ adapter : :attributes ,
34+ serializer : AlternateBlogSerializer ,
35+ meta : { total : 10 } )
36+ . as_json
3237 expected = {
3338 id : 1 ,
3439 title : 'AMS Hints'
3540 }
36- assert_equal expected , adapter . as_json
41+ assert_equal expected , actual
3742 end
3843
3944 def test_meta_key_is_used
40- serializer = AlternateBlogSerializer . new ( @blog , meta : { total : 10 } , meta_key : 'haha_meta' )
41- adapter = ActiveModel ::Serializer ::Adapter ::Json . new ( serializer )
45+ actual = ActiveModel ::SerializableResource . new ( @blog ,
46+ adapter : :json ,
47+ serializer : AlternateBlogSerializer ,
48+ meta : { total : 10 } ,
49+ meta_key : 'haha_meta' )
50+ . as_json
4251 expected = {
4352 blog : {
4453 id : 1 ,
@@ -48,12 +57,16 @@ def test_meta_key_is_used
4857 total : 10
4958 }
5059 }
51- assert_equal expected , adapter . as_json
60+ assert_equal expected , actual
5261 end
5362
5463 def test_meta_key_is_used_with_json_api
55- serializer = AlternateBlogSerializer . new ( @blog , meta : { total : 10 } , meta_key : 'haha_meta' )
56- adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new ( serializer )
64+ actual = ActiveModel ::SerializableResource . new ( @blog ,
65+ adapter : :json_api ,
66+ serializer : AlternateBlogSerializer ,
67+ meta : { total : 10 } ,
68+ meta_key : 'haha_meta' )
69+ . as_json
5770 expected = {
5871 data : {
5972 id : '1' ,
@@ -62,13 +75,14 @@ def test_meta_key_is_used_with_json_api
6275 } ,
6376 'haha_meta' => { total : 10 }
6477 }
65- assert_equal expected , adapter . as_json
78+ assert_equal expected , actual
6679 end
6780
6881 def test_meta_is_not_present_on_arrays_without_root
69- serializer = ArraySerializer . new ( [ @blog ] , meta : { total : 10 } )
70- # Attributes doesn't have support to root
71- adapter = ActiveModel ::Serializer ::Adapter ::Attributes . new ( serializer )
82+ actual = ActiveModel ::SerializableResource . new ( [ @blog ] ,
83+ adapter : :attributes ,
84+ meta : { total : 10 } )
85+ . as_json
7286 expected = [ {
7387 id : 1 ,
7488 name : 'AMS Hints' ,
@@ -82,13 +96,15 @@ def test_meta_is_not_present_on_arrays_without_root
8296 body : nil
8397 } ]
8498 } ]
85- assert_equal expected , adapter . as_json
99+ assert_equal expected , actual
86100 end
87101
88102 def test_meta_is_present_on_arrays_with_root
89- serializer = ArraySerializer . new ( [ @blog ] , meta : { total : 10 } , meta_key : 'haha_meta' )
90- # JSON adapter adds root by default
91- adapter = ActiveModel ::Serializer ::Adapter ::Json . new ( serializer )
103+ actual = ActiveModel ::SerializableResource . new ( [ @blog ] ,
104+ adapter : :json ,
105+ meta : { total : 10 } ,
106+ meta_key : 'haha_meta' )
107+ . as_json
92108 expected = {
93109 blogs : [ {
94110 id : 1 ,
@@ -107,14 +123,7 @@ def test_meta_is_present_on_arrays_with_root
107123 total : 10
108124 }
109125 }
110- assert_equal expected , adapter . as_json
111- end
112-
113- private
114-
115- def load_adapter ( options )
116- options = options . merge ( adapter : :attributes , serializer : AlternateBlogSerializer )
117- ActiveModel ::SerializableResource . new ( @blog , options )
126+ assert_equal expected , actual
118127 end
119128 end
120129 end
0 commit comments