|
2 | 2 |
|
3 | 3 | if opal?
|
4 | 4 | describe 'the param macro' do
|
5 |
| - |
6 | 5 | it 'defines collect_other_params_as method on params proxy' do
|
7 | 6 | stub_const 'Foo', Class.new(React::Component::Base)
|
8 | 7 | Foo.class_eval do
|
@@ -168,6 +167,65 @@ def render
|
168 | 167 | expect(`window.dummy_log`).to eq(["Warning: Failed propType: In component `Foo`\nProvided prop `foo` could not be converted to BazWoggle"])
|
169 | 168 | end
|
170 | 169 |
|
| 170 | + describe "converts params only once" do |
| 171 | + it "not on every access" do |
| 172 | + pending 'Fix after merging' |
| 173 | + stub_const "BazWoggle", Class.new |
| 174 | + BazWoggle.class_eval do |
| 175 | + def initialize(kind) |
| 176 | + @kind = kind |
| 177 | + end |
| 178 | + attr_accessor :kind |
| 179 | + def self._react_param_conversion(json, validate_only) |
| 180 | + new(json[:bazwoggle]) if json[:bazwoggle] |
| 181 | + end |
| 182 | + end |
| 183 | + Foo.class_eval do |
| 184 | + param :foo, type: BazWoggle |
| 185 | + def render |
| 186 | + params.foo.kind = params.foo.kind+1 |
| 187 | + "#{params.foo.kind}" |
| 188 | + end |
| 189 | + end |
| 190 | + expect(Foo).to render |
| 191 | + expect(React.render_to_static_markup(React.create_element(Foo, foo: {bazwoggle: 1}))).to eq('<span>2</span>') |
| 192 | + end |
| 193 | + |
| 194 | + it "even if contains an embedded native object" do |
| 195 | + pending 'Fix after merging' |
| 196 | + stub_const "Bar", Class.new(React::Component::Base) |
| 197 | + stub_const "BazWoggle", Class.new |
| 198 | + BazWoggle.class_eval do |
| 199 | + def initialize(kind) |
| 200 | + @kind = kind |
| 201 | + end |
| 202 | + attr_accessor :kind |
| 203 | + def self._react_param_conversion(json, validate_only) |
| 204 | + new(JSON.from_object(json[0])[:bazwoggle]) if JSON.from_object(json[0])[:bazwoggle] |
| 205 | + end |
| 206 | + end |
| 207 | + Bar.class_eval do |
| 208 | + param :foo, type: BazWoggle |
| 209 | + def render |
| 210 | + params.foo.kind.to_s |
| 211 | + end |
| 212 | + end |
| 213 | + Foo.class_eval do |
| 214 | + export_state :change_me |
| 215 | + before_mount do |
| 216 | + Foo.change_me! "initial" |
| 217 | + end |
| 218 | + def render |
| 219 | + Bar(foo: Native([`{bazwoggle: #{Foo.change_me}}`])) |
| 220 | + end |
| 221 | + end |
| 222 | + div = `document.createElement("div")` |
| 223 | + React.render(React.create_element(Foo, {}), div) |
| 224 | + Foo.change_me! "updated" |
| 225 | + expect(`div.children[0].innerHTML`).to eq("updated") |
| 226 | + end |
| 227 | + end |
| 228 | + |
171 | 229 | it "will alias a Proc type param" do
|
172 | 230 | Foo.class_eval do
|
173 | 231 | param :foo, type: Proc
|
|
0 commit comments