|
2 | 2 |
|
3 | 3 | require "test_helper" |
4 | 4 |
|
| 5 | +# rubocop:disable Metrics/ClassLength |
5 | 6 | class ComponentMountTest < ActionDispatch::IntegrationTest |
6 | 7 | module DummyRenderer |
7 | 8 | def self.render(component_name, props, _prerender_options) |
@@ -128,5 +129,92 @@ def self.react_rails_prerenderer |
128 | 129 |
|
129 | 130 | assert_equal %(<div>rendered Foo with {"ok":true}</div>), rendered_component |
130 | 131 | end |
| 132 | + |
| 133 | + test "#react_component sets null props to undefined when null_to_undefined_props set to true" do |
| 134 | + app.config.react.null_to_undefined_props = true |
| 135 | + |
| 136 | + @helper.setup(DummyController) |
| 137 | + rendered_component = @helper.react_component("Foo", { bar: nil, content: 'bar":null,' }) |
| 138 | + |
| 139 | + assert_includes rendered_component, '"bar":undefined,"content":"bar\\":null,"' |
| 140 | + end |
| 141 | + |
| 142 | + test "#react_component passes null props as null when null_to_undefined_props set to false" do |
| 143 | + app.config.react.null_to_undefined_props = false |
| 144 | + |
| 145 | + @helper.setup(DummyController) |
| 146 | + rendered_component = @helper.react_component("Foo", { bar: nil, content: 'bar":null,' }) |
| 147 | + |
| 148 | + assert_includes rendered_component, ""bar":null,"content":"bar\\":null,"" |
| 149 | + end |
| 150 | + |
| 151 | + test "#props_to_json doesn't converts null values to undefined be default" do |
| 152 | + props = { name: nil } |
| 153 | + expected_json = '{"name":null}' |
| 154 | + component_mount = React::Rails::ComponentMount.new |
| 155 | + |
| 156 | + actual_json = component_mount.send(:props_to_json, props) |
| 157 | + |
| 158 | + assert_equal(expected_json, actual_json) |
| 159 | + end |
| 160 | + |
| 161 | + test "#props_to_json converts null values to undefined with null_to_undefined: true option" do |
| 162 | + props = { bar: nil, content: 'bar":null,' } |
| 163 | + expected_json = '{"bar":undefined,"content":"bar\\":null,"}' |
| 164 | + component_mount = React::Rails::ComponentMount.new |
| 165 | + |
| 166 | + actual_json = component_mount.send(:props_to_json, props, { null_to_undefined: true }) |
| 167 | + |
| 168 | + assert_equal(expected_json, actual_json) |
| 169 | + end |
| 170 | + |
| 171 | + test "#props_to_json converts null values in arrays to undefined with null_to_undefined: true option" do |
| 172 | + props = { items1: [nil], items2: [1, nil], items3: [nil, 1], items4: [1, nil, 2] } |
| 173 | + expected_json = '{"items1":[undefined],"items2":[1,undefined],"items3":[undefined,1],"items4":[1,undefined,2]}' |
| 174 | + component_mount = React::Rails::ComponentMount.new |
| 175 | + |
| 176 | + actual_json = component_mount.send(:props_to_json, props, { null_to_undefined: true }) |
| 177 | + |
| 178 | + assert_equal(expected_json, actual_json) |
| 179 | + end |
| 180 | + |
| 181 | + test "#props_to_json doesnt converts null-like values in arrays to undefined with null_to_undefined: true option" do |
| 182 | + props = { |
| 183 | + items1: "[null]", |
| 184 | + items2: "[1,null]", |
| 185 | + items3: "[null,1]", |
| 186 | + items4: "[1,null,2]", |
| 187 | + items5: '["a",null]', |
| 188 | + items6: '[null,"b"]', |
| 189 | + items7: '["a",null,"b"]', |
| 190 | + items8: '["a",nullx,"b"]' |
| 191 | + } |
| 192 | + expected_json = '{"items1":"[null]","items2":"[1,null]","items3":"[null,1]","items4":"[1,null,2]",' \ |
| 193 | + '"items5":"[\"a\",null]","items6":"[null,\"b\"]","items7":"[\"a\",null,\"b\"]"' \ |
| 194 | + ',"items8":"[\"a\",nullx,\"b\"]"}' |
| 195 | + component_mount = React::Rails::ComponentMount.new |
| 196 | + |
| 197 | + actual_json = component_mount.send(:props_to_json, props, { null_to_undefined: true }) |
| 198 | + |
| 199 | + assert_equal(expected_json, actual_json) |
| 200 | + end |
| 201 | + |
| 202 | + test "#props_to_json doesnt converts null values in nested arrays to undefined with null_to_undefined: true" do |
| 203 | + props = { |
| 204 | + items1: nil, |
| 205 | + items2: [1, nil, 2], |
| 206 | + items3: nil, |
| 207 | + items4: "[1, null, 2]", |
| 208 | + items5: nil |
| 209 | + } |
| 210 | + expected_json = '{"items1":undefined,"items2":[1,undefined,2],"items3":undefined,"items4":"[1, null, 2]"' \ |
| 211 | + ',"items5":undefined}' |
| 212 | + component_mount = React::Rails::ComponentMount.new |
| 213 | + |
| 214 | + actual_json = component_mount.send(:props_to_json, props, { null_to_undefined: true }) |
| 215 | + |
| 216 | + assert_equal(expected_json, actual_json) |
| 217 | + end |
131 | 218 | end |
132 | 219 | end |
| 220 | +# rubocop:enable Metrics/ClassLength |
0 commit comments