@@ -146,5 +146,45 @@ def self.react_rails_prerenderer
146
146
147
147
assert_includes rendered_component , ""bar":null,"content":"bar\\ ":null,""
148
148
end
149
+
150
+ test "#props_to_json doesn't converts null values to undefined be default" do
151
+ props = { name : nil }
152
+ expected_json = '{"name":null}'
153
+ component_mount = React ::Rails ::ComponentMount . new
154
+
155
+ actual_json = component_mount . send ( :props_to_json , props )
156
+
157
+ assert_equal ( expected_json , actual_json )
158
+ end
159
+
160
+ test "#props_to_json converts null values to undefined with null_to_undefined: true option" do
161
+ props = { bar : nil , content : 'bar":null,' }
162
+ expected_json = '{"bar":undefined,"content":"bar\\":null,"}'
163
+ component_mount = React ::Rails ::ComponentMount . new
164
+
165
+ actual_json = component_mount . send ( :props_to_json , props , { null_to_undefined : true } )
166
+
167
+ assert_equal ( expected_json , actual_json )
168
+ end
169
+
170
+ test "#props_to_json converts null values in arrays to undefined with null_to_undefined: true option" do
171
+ props = { items1 : [ nil ] , items2 : [ 1 , nil ] , items3 : [ nil , 1 ] , items4 : [ 1 , nil , 2 ] }
172
+ expected_json = '{"items1":[undefined],"items2":[1,undefined],"items3":[undefined,1],"items4":[1,undefined,2]}'
173
+ component_mount = React ::Rails ::ComponentMount . new
174
+
175
+ actual_json = component_mount . send ( :props_to_json , props , { null_to_undefined : true } )
176
+
177
+ assert_equal ( expected_json , actual_json )
178
+ end
179
+
180
+ test "#props_to_json doesnt converts null-like values in arrays to undefined with null_to_undefined: true option" do
181
+ props = { items1 : "[null]" , items2 : "[1, null]" , items3 : "[null, 1]" , items4 : "[1, null, 2]" }
182
+ expected_json = '{"items1":"[null]","items2":"[1,null]","items3":"[null,1]","items4":"[1,null,2]"}'
183
+ component_mount = React ::Rails ::ComponentMount . new
184
+
185
+ actual_json = component_mount . send ( :props_to_json , props , { null_to_undefined : true } )
186
+
187
+ assert_equal ( expected_json , actual_json )
188
+ end
149
189
end
150
190
end
0 commit comments