Skip to content

Commit 3f8f482

Browse files
committed
Improve tests and add tests for arrays
1 parent 4ee47c3 commit 3f8f482

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/react/rails/component_mount_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,45 @@ def self.react_rails_prerenderer
146146

147147
assert_includes rendered_component, ""bar":null,"content":"bar\\":null,""
148148
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
149189
end
150190
end

0 commit comments

Comments
 (0)