hyper-react 0.12.7 → 0.99.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +48 -34
  3. data/CODE_OF_CONDUCT.md +49 -0
  4. data/Gemfile +5 -6
  5. data/README.md +47 -98
  6. data/Rakefile +6 -28
  7. data/hyper-react.gemspec +36 -43
  8. data/lib/hyper-react.rb +4 -73
  9. data/lib/react/version.rb +3 -0
  10. metadata +91 -249
  11. data/.codeclimate.yml +0 -27
  12. data/.rubocop.yml +0 -1159
  13. data/.travis.yml +0 -62
  14. data/Appraisals +0 -31
  15. data/CHANGELOG.md +0 -143
  16. data/LICENSE +0 -19
  17. data/UPGRADING.md +0 -24
  18. data/component-name-lookup.md +0 -145
  19. data/config.ru +0 -26
  20. data/gemfiles/opal_0.10_react_13.gemfile +0 -15
  21. data/gemfiles/opal_0.10_react_14.gemfile +0 -15
  22. data/gemfiles/opal_0.10_react_15.gemfile +0 -15
  23. data/gemfiles/opal_0.8_react_13.gemfile +0 -15
  24. data/gemfiles/opal_0.8_react_14.gemfile +0 -15
  25. data/gemfiles/opal_0.8_react_15.gemfile +0 -15
  26. data/gemfiles/opal_0.9_react_13.gemfile +0 -15
  27. data/gemfiles/opal_0.9_react_14.gemfile +0 -15
  28. data/gemfiles/opal_0.9_react_15.gemfile +0 -15
  29. data/gemfiles/opal_master_react_15.gemfile +0 -16
  30. data/lib/generators/reactive_ruby/test_app/templates/assets/javascripts/components.rb +0 -3
  31. data/lib/generators/reactive_ruby/test_app/templates/assets/javascripts/test_application.rb +0 -2
  32. data/lib/generators/reactive_ruby/test_app/templates/boot.rb.erb +0 -6
  33. data/lib/generators/reactive_ruby/test_app/templates/script/rails +0 -5
  34. data/lib/generators/reactive_ruby/test_app/templates/test_application.rb.erb +0 -13
  35. data/lib/generators/reactive_ruby/test_app/templates/views/components/hello_world.rb +0 -11
  36. data/lib/generators/reactive_ruby/test_app/templates/views/components/todo.rb +0 -14
  37. data/lib/generators/reactive_ruby/test_app/templates/views/layouts/test_layout.html.erb +0 -0
  38. data/lib/generators/reactive_ruby/test_app/test_app_generator.rb +0 -111
  39. data/lib/rails-helpers/top_level_rails_component.rb +0 -54
  40. data/lib/react-sources/react-server.js +0 -2
  41. data/lib/react/api.rb +0 -173
  42. data/lib/react/callbacks.rb +0 -41
  43. data/lib/react/children.rb +0 -30
  44. data/lib/react/component.rb +0 -168
  45. data/lib/react/component/api.rb +0 -50
  46. data/lib/react/component/base.rb +0 -13
  47. data/lib/react/component/class_methods.rb +0 -189
  48. data/lib/react/component/dsl_instance_methods.rb +0 -23
  49. data/lib/react/component/params.rb +0 -6
  50. data/lib/react/component/props_wrapper.rb +0 -78
  51. data/lib/react/component/should_component_update.rb +0 -94
  52. data/lib/react/component/tags.rb +0 -129
  53. data/lib/react/config.rb +0 -5
  54. data/lib/react/config/client.rb.erb +0 -19
  55. data/lib/react/config/server.rb +0 -23
  56. data/lib/react/element.rb +0 -169
  57. data/lib/react/event.rb +0 -76
  58. data/lib/react/ext/hash.rb +0 -9
  59. data/lib/react/ext/opal-jquery/element.rb +0 -26
  60. data/lib/react/ext/string.rb +0 -8
  61. data/lib/react/hash.rb +0 -13
  62. data/lib/react/native_library.rb +0 -87
  63. data/lib/react/object.rb +0 -15
  64. data/lib/react/react-source-browser.rb +0 -3
  65. data/lib/react/react-source-server.rb +0 -3
  66. data/lib/react/react-source.rb +0 -20
  67. data/lib/react/ref_callback.rb +0 -31
  68. data/lib/react/rendering_context.rb +0 -144
  69. data/lib/react/server.rb +0 -23
  70. data/lib/react/state_wrapper.rb +0 -23
  71. data/lib/react/test.rb +0 -16
  72. data/lib/react/test/dsl.rb +0 -17
  73. data/lib/react/test/matchers/render_html_matcher.rb +0 -56
  74. data/lib/react/test/rspec.rb +0 -15
  75. data/lib/react/test/session.rb +0 -37
  76. data/lib/react/test/utils.rb +0 -25
  77. data/lib/react/top_level.rb +0 -118
  78. data/lib/react/top_level_render.rb +0 -29
  79. data/lib/react/validator.rb +0 -136
  80. data/lib/reactive-ruby/component_loader.rb +0 -50
  81. data/lib/reactive-ruby/isomorphic_helpers.rb +0 -212
  82. data/lib/reactive-ruby/rails.rb +0 -7
  83. data/lib/reactive-ruby/rails/component_mount.rb +0 -46
  84. data/lib/reactive-ruby/rails/controller_helper.rb +0 -15
  85. data/lib/reactive-ruby/rails/railtie.rb +0 -33
  86. data/lib/reactive-ruby/serializers.rb +0 -15
  87. data/lib/reactive-ruby/server_rendering/contextual_renderer.rb +0 -42
  88. data/lib/reactive-ruby/version.rb +0 -3
  89. data/lib/reactrb/auto-import.rb +0 -27
  90. data/lib/reactrb/deep-compare.rb +0 -24
  91. data/lib/reactrb/new-event-name-convention.rb +0 -11
  92. data/logo1.png +0 -0
  93. data/logo2.png +0 -0
  94. data/logo3.png +0 -0
  95. data/path_release_steps.md +0 -9
  96. data/spec/controller_helper_spec.rb +0 -34
  97. data/spec/index.html.erb +0 -11
  98. data/spec/react/callbacks_spec.rb +0 -138
  99. data/spec/react/children_spec.rb +0 -76
  100. data/spec/react/component/base_spec.rb +0 -32
  101. data/spec/react/component_spec.rb +0 -884
  102. data/spec/react/dsl_spec.rb +0 -303
  103. data/spec/react/element_spec.rb +0 -136
  104. data/spec/react/event_spec.rb +0 -24
  105. data/spec/react/native_library_spec.rb +0 -322
  106. data/spec/react/observable_spec.rb +0 -42
  107. data/spec/react/opal_jquery_extensions_spec.rb +0 -68
  108. data/spec/react/param_declaration_spec.rb +0 -269
  109. data/spec/react/react_spec.rb +0 -215
  110. data/spec/react/refs_callback_spec.rb +0 -56
  111. data/spec/react/server_spec.rb +0 -25
  112. data/spec/react/state_spec.rb +0 -55
  113. data/spec/react/test/dsl_spec.rb +0 -43
  114. data/spec/react/test/matchers/render_html_matcher_spec.rb +0 -83
  115. data/spec/react/test/rspec_spec.rb +0 -62
  116. data/spec/react/test/session_spec.rb +0 -88
  117. data/spec/react/test/utils_spec.rb +0 -28
  118. data/spec/react/top_level_component_spec.rb +0 -101
  119. data/spec/react/tutorial/tutorial_spec.rb +0 -36
  120. data/spec/react/validator_spec.rb +0 -124
  121. data/spec/reactive-ruby/component_loader_spec.rb +0 -77
  122. data/spec/reactive-ruby/isomorphic_helpers_spec.rb +0 -160
  123. data/spec/reactive-ruby/rails/asset_pipeline_spec.rb +0 -10
  124. data/spec/reactive-ruby/rails/component_mount_spec.rb +0 -66
  125. data/spec/reactive-ruby/server_rendering/contextual_renderer_spec.rb +0 -35
  126. data/spec/spec_helper.rb +0 -149
  127. data/spec/support/react/spec_helpers.rb +0 -44
  128. data/spec/vendor/es5-shim.min.js +0 -6
  129. data/spec/vendor/jquery-2.2.4.min.js +0 -4
@@ -1,322 +0,0 @@
1
- require 'spec_helper'
2
- require 'reactrb/auto-import'
3
-
4
- if opal?
5
-
6
- module NativeLibraryTestModule
7
- class Component < React::Component::Base
8
- param :time_stamp
9
- backtrace :none
10
- render { NativeComponent(name: "There - #{params.time_stamp}") }
11
- end
12
-
13
- class NestedComponent < React::Component::Base
14
- param :time_stamp
15
- backtrace :none
16
- render { NativeLibrary::NativeNestedLibrary::NativeComponent(name: "There - #{params.time_stamp}") }
17
- end
18
- end
19
-
20
- describe "React::NativeLibrary", type: :component do
21
-
22
- after(:each) do
23
- %x{
24
- delete window.NativeLibrary;
25
- delete window.NativeComponent;
26
- delete window.nativeLibrary;
27
- delete window.nativeComponent;
28
- delete window.NativeObject;
29
- }
30
- Object.send :remove_const, :NativeLibrary
31
- Object.send :remove_const, :NativeComponent
32
- end
33
-
34
- describe "functional stateless component (supported in reactjs v14+ only)" do
35
- it "is not detected as native React.js component by `native_react_component?`", v13_only: true do
36
- expect(React::API.native_react_component?(`function C(){ return null }`)).to be_falsy
37
- end
38
-
39
- it "is detected as native React.js component by `native_react_component?`", v13_exclude: true do
40
- expect(React::API.native_react_component?(`function C(){ return null }`)).to be_truthy
41
- end
42
-
43
- it "imports a React.js functional stateless component", v13_exclude: true do
44
- %x{
45
- window.NativeLibrary = {
46
- FunctionalComponent: function HelloMessage(props){
47
- return React.createElement("div", null, "Hello ", props.name);
48
- }
49
- }
50
- }
51
- stub_const 'Foo', Class.new(React::Component::Base)
52
- Foo.class_eval do
53
- imports "NativeLibrary.FunctionalComponent"
54
- end
55
- expect(Foo).to render_static_html('<div>Hello There</div>').with_params(name: "There")
56
- end
57
- end
58
-
59
- it "can use native_react_component? to detect a native React.js component" do
60
- %x{
61
- window.NativeComponent = React.createClass({
62
- displayName: "HelloMessage",
63
- render: function render() {
64
- return React.createElement("div", null, "Hello ", this.props.name);
65
- }
66
- })
67
- }
68
- expect(React::API.native_react_component?(`window.NativeComponent`)).to be_truthy
69
- expect(React::API.native_react_component?(`{render: function render() {}}`)).to be_falsy
70
- expect(React::API.native_react_component?(`window.DoesntExist`)).to be_falsy
71
- expect(React::API.native_react_component?()).to be_falsy
72
- end
73
-
74
- it "will import a React.js library into the Ruby name space" do
75
- %x{
76
- window.NativeLibrary = {
77
- NativeComponent: React.createClass({
78
- displayName: "HelloMessage",
79
- render: function render() {
80
- return React.createElement("div", null, "Hello ", this.props.name);
81
- }
82
- })
83
- }
84
- }
85
- stub_const 'Foo', Class.new(React::NativeLibrary)
86
- Foo.class_eval do
87
- imports "NativeLibrary"
88
- end
89
- expect(Foo::NativeComponent)
90
- .to render_static_html('<div>Hello There</div>').with_params(name: "There")
91
- end
92
-
93
- it "will import a nested React.js library into the Ruby name space" do
94
- %x{
95
- window.NativeLibrary = {
96
- NestedLibrary: {
97
- NativeComponent: React.createClass({
98
- displayName: "HelloMessage",
99
- render: function render() {
100
- return React.createElement("div", null, "Hello ", this.props.name);
101
- }
102
- })}
103
- }
104
- }
105
- stub_const 'Foo', Class.new(React::NativeLibrary)
106
- Foo.class_eval do
107
- imports "NativeLibrary"
108
- end
109
- expect(Foo::NestedLibrary::NativeComponent)
110
- .to render_static_html('<div>Hello There</div>').with_params(name: "There")
111
- end
112
-
113
- it "will rename an imported a React.js component" do
114
- %x{
115
- window.NativeLibrary = {
116
- NativeComponent: React.createClass({
117
- displayName: "HelloMessage",
118
- render: function render() {
119
- return React.createElement("div", null, "Hello ", this.props.name);
120
- }
121
- })
122
- }
123
- }
124
- stub_const 'Foo', Class.new(React::NativeLibrary)
125
- Foo.class_eval do
126
- imports "NativeLibrary"
127
- rename "NativeComponent" => "Bar"
128
- end
129
- expect(Foo::Bar)
130
- .to render_static_html('<div>Hello There</div>').with_params(name: "There")
131
- end
132
-
133
- it "will give a reasonable error when failing to import a renamed component" do
134
- %x{
135
- window.NativeLibrary = {
136
- NativeComponent: React.createClass({
137
- displayName: "HelloMessage",
138
- render: function render() {
139
- return React.createElement("div", null, "Hello ", this.props.name);
140
- }
141
- })
142
- }
143
- }
144
- stub_const 'Foo', Class.new(React::NativeLibrary)
145
- expect do
146
- Foo.class_eval do
147
- imports "NativeLibrary"
148
- rename "MispelledComponent" => "Bar"
149
- end
150
- end.to raise_error(/could not import MispelledComponent/)
151
- end
152
-
153
- it "will import a single React.js component into the ruby name space" do
154
- %x{
155
- window.NativeComponent = React.createClass({
156
- displayName: "HelloMessage",
157
- render: function render() {
158
- return React.createElement("div", null, "Hello ", this.props.name);
159
- }
160
- })
161
- }
162
- stub_const 'Foo', Class.new(React::Component::Base)
163
- Foo.class_eval do
164
- imports "NativeComponent"
165
- end
166
- expect(Foo)
167
- .to render_static_html('<div>Hello There</div>').with_params(name: "There")
168
-
169
- end
170
-
171
- it "will import a name scoped React.js component into the ruby name space" do
172
- %x{
173
- window.NativeLibrary = {
174
- NativeComponent: React.createClass({
175
- displayName: "HelloMessage",
176
- render: function render() {
177
- return React.createElement("div", null, "Hello ", this.props.name);
178
- }
179
- })
180
- }
181
- }
182
- stub_const 'Foo', Class.new(React::Component::Base)
183
- Foo.class_eval do
184
- imports "NativeLibrary.NativeComponent"
185
- end
186
- expect(Foo)
187
- .to render_static_html('<div>Hello There</div>').with_params(name: "There")
188
-
189
- end
190
-
191
- it "will give a meaningful error if the React.js component is invalid" do
192
- %x{
193
- window.NativeObject = {}
194
- }
195
- stub_const 'Foo', Class.new(React::Component::Base)
196
- expect do
197
- Foo.class_eval do
198
- imports "NativeObject"
199
- end
200
- end.to raise_error("Foo cannot import 'NativeObject': does not appear to be a native react component.")
201
- expect do
202
- Foo.class_eval do
203
- imports "window.Baz"
204
- end
205
- end.to raise_error(/^Foo cannot import \'window\.Baz\'\: (?!does not appear to be a native react component)..*$/)
206
- end
207
-
208
- it "allows passing native object as props" do
209
- %x{
210
- window.NativeComponent = React.createClass({
211
- displayName: "HelloMessage",
212
- render: function render() {
213
- return React.createElement("div", null, "Hello ", this.props.user.name);
214
- }
215
- })
216
- }
217
- stub_const 'Foo', Class.new(React::Component::Base)
218
- Foo.class_eval do
219
- imports "NativeComponent"
220
- end
221
- stub_const 'Wrapper', Class.new(React::Component::Base)
222
- Wrapper.class_eval do
223
- def render
224
- Foo(user: `{name: 'David'}`)
225
- end
226
- end
227
- expect(Wrapper).to render_static_html('<div>Hello David</div>')
228
- end
229
-
230
- context "automatic importing" do
231
-
232
- it "will automatically import a React.js component when referenced in another component" do
233
- %x{
234
- window.NativeComponent = React.createClass({
235
- displayName: "HelloMessage",
236
- render: function render() {
237
- return React.createElement("div", null, "Hello ", this.props.name);
238
- }
239
- })
240
- }
241
- expect(React::Server.render_to_static_markup(
242
- React.create_element(NativeLibraryTestModule::Component, time_stamp: Time.now))).to match(/<div>Hello There.*<\/div>/)
243
- end
244
-
245
- it "will automatically import a React.js component when referenced in another component" do
246
- stub_const 'Foo', Class.new(React::Component::Base)
247
- Foo.class_eval do
248
- render { NativeComponent(name: "There") }
249
- end
250
- %x{
251
- window.NativeComponent = React.createClass({
252
- displayName: "HelloMessage",
253
- render: function render() {
254
- return React.createElement("div", null, "Hello ", this.props.name);
255
- }
256
- })
257
- }
258
- expect(Foo).to render_static_html('<div>Hello There</div>')
259
- end
260
-
261
- it "will automatically import a React.js component when referenced as a constant" do
262
- %x{
263
- window.NativeComponent = React.createClass({
264
- displayName: "HelloMessage",
265
- render: function render() {
266
- return React.createElement("div", null, "Hello ", this.props.name);
267
- }
268
- })
269
- }
270
- expect(NativeComponent)
271
- .to render_static_html('<div>Hello There</div>').with_params(name: "There")
272
- end
273
-
274
- it "will automatically import a native library containing a React.js component" do
275
- %x{
276
- window.NativeLibrary = {
277
- NativeNestedLibrary: {
278
- NativeComponent: React.createClass({
279
- displayName: "HelloMessage",
280
- render: function render() {
281
- return React.createElement("div", null, "Hello ", this.props.name);
282
- }
283
- })
284
- }
285
- }
286
- }
287
-
288
- expect(React::Server.render_to_static_markup(
289
- React.create_element(NativeLibraryTestModule::NestedComponent, time_stamp: Time.now))).to match(/<div>Hello There.*<\/div>/)
290
- end
291
-
292
- it "the library and components can begin with lower case letters" do
293
- %x{
294
- window.nativeLibrary = {
295
- nativeComponent: React.createClass({
296
- displayName: "HelloMessage",
297
- render: function render() {
298
- return React.createElement("div", null, "Hello ", this.props.name);
299
- }
300
- })
301
- }
302
- }
303
- expect(NativeLibrary::NativeComponent)
304
- .to render_static_html('<div>Hello There</div>').with_params(name: "There")
305
- end
306
-
307
- it "will produce a sensible error if the component is not in the library" do
308
- %x{
309
- window.NativeLibrary = {
310
- NativeNestedLibrary: {
311
- }
312
- }
313
- }
314
- expect do
315
- React::Server.render_to_static_markup(React.create_element(NativeLibraryTestModule::NestedComponent, time_stamp: Time.now))
316
- end.to raise_error("could not import a react component named: NativeLibrary.NativeNestedLibrary.NativeComponent")
317
-
318
- end
319
-
320
- end
321
- end
322
- end
@@ -1,42 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if opal?
4
-
5
- describe 'React::Observable' do
6
- it "allows to set value on Observable" do
7
- stub_const 'Zoo', Class.new {
8
- include React::Component
9
- param :foo, type: React::Observable
10
- before_mount do
11
- params.foo! 4
12
- end
13
-
14
- def render
15
- nil
16
- end
17
- }
18
-
19
- stub_const 'Foo', Class.new
20
- Foo.class_eval do
21
- include React::Component
22
-
23
- def render
24
- div do
25
- Zoo(foo: state.foo! )
26
- span { state.foo.to_s }
27
- end
28
- end
29
- end
30
-
31
- instance = React::Test::Utils.render_into_document(React.create_element(Foo))
32
- html = `#{instance.dom_node}.innerHTML`
33
- # data-reactid appear in earlier versions of reactjs
34
- %x{
35
- var REGEX_REMOVE_IDS = /\s?data-reactid="[^"]+"/g;
36
- html = html.replace(REGEX_REMOVE_IDS, '');
37
- }
38
- expect(html).to eq('<span></span><span>4</span>')
39
- end
40
- end
41
-
42
- end
@@ -1,68 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if opal?
4
- describe 'opal-jquery extensions' do
5
- describe 'Element' do
6
- after(:each) do
7
- React::API.clear_component_class_cache
8
- end
9
-
10
- it 'will reuse the wrapper componet class for the same Element' do
11
- stub_const 'Foo', Class.new(React::Component::Base)
12
- Foo.class_eval do
13
- param :name
14
- def render
15
- "hello #{params.name}"
16
- end
17
-
18
- def component_will_unmount
19
-
20
- end
21
- end
22
-
23
- expect_any_instance_of(Foo).to_not receive(:component_will_unmount)
24
-
25
- test_div = Element.new(:div)
26
- test_div.render { Foo(name: 'fred') }
27
- test_div.render { Foo(name: 'freddy') }
28
- expect(Element[test_div].find('span').html).to eq('hello freddy')
29
- end
30
-
31
- it 'renders a top level component using render with a block' do
32
- stub_const 'Foo', Class.new(React::Component::Base)
33
- Foo.class_eval do
34
- param :name
35
- def render
36
- "hello #{params.name}"
37
- end
38
- end
39
- test_div = Element.new(:div)
40
- test_div.render { Foo(name: 'fred') }
41
- expect(Element[test_div].find('span').html).to eq('hello fred')
42
- end
43
-
44
- it 'renders a top level component using render with a container and params ' do
45
- test_div = Element.new(:div)
46
- test_div.render(:span, id: :render_test_span) { 'hello' }
47
- expect(Element[test_div].find('#render_test_span').html).to eq('hello')
48
- end
49
-
50
- it 'will find the DOM node given a react element' do
51
- stub_const 'Foo', Class.new(React::Component::Base)
52
- Foo.class_eval do
53
- def render
54
- div { 'hello' }
55
- end
56
- end
57
-
58
- expect(Element[renderToDocument(Foo)].html).to eq('hello')
59
- end
60
-
61
- it "accepts plain js object as selector" do
62
- expect {
63
- Element[`window`]
64
- }.not_to raise_error
65
- end
66
- end
67
- end
68
- end
@@ -1,269 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if opal?
4
- describe 'the param macro', type: :component do
5
- it 'defines collect_other_params_as method on params proxy' do
6
- stub_const 'Foo', Class.new(React::Component::Base)
7
- Foo.class_eval do
8
- collect_other_params_as :foo
9
-
10
- def render
11
- div { params.foo[:bar] }
12
- end
13
- end
14
-
15
- expect(Foo).to render_static_html('<div>biz</div>').with_params(bar: 'biz')
16
- end
17
-
18
- it "can create and access a required param" do
19
- stub_const 'Foo', Class.new(React::Component::Base)
20
- Foo.class_eval do
21
- param :foo
22
-
23
- def render
24
- div { params.foo }
25
- end
26
- end
27
-
28
- expect(Foo).to render_static_html('<div>bar</div>').with_params(foo: :bar)
29
- end
30
-
31
- it "can create and access an optional params" do
32
- stub_const 'Foo', Class.new(React::Component::Base)
33
- Foo.class_eval do
34
-
35
- param foo1: :no_bar1
36
- param foo2: :no_bar2
37
- param :foo3, default: :no_bar3
38
- param :foo4, default: :no_bar4
39
-
40
- def render
41
- div { "#{params.foo1}-#{params.foo2}-#{params.foo3}-#{params.foo4}" }
42
- end
43
- end
44
-
45
- expect(Foo).to render_static_html('<div>bar1-no_bar2-bar3-no_bar4</div>').with_params(foo1: :bar1, foo3: :bar3)
46
- end
47
-
48
- it 'can specify validation rules with the type option' do
49
- stub_const 'Foo', Class.new(React::Component::Base)
50
- Foo.class_eval do
51
- param :foo, type: String
52
- end
53
-
54
- expect(Foo.prop_types).to have_key(:_componentValidator)
55
- end
56
-
57
- it "can type check params" do
58
- stub_const 'Foo', Class.new(React::Component::Base)
59
- Foo.class_eval do
60
-
61
- param :foo1, type: String
62
- param :foo2, type: String
63
-
64
- def render
65
- div { "#{params.foo1}-#{params.foo2}" }
66
- end
67
- end
68
-
69
- %x{
70
- var log = [];
71
- var org_warn_console = window.console.warn;
72
- var org_error_console = window.console.error;
73
- window.console.warn = window.console.error = function(str){log.push(str)}
74
- }
75
- expect(Foo).to render_static_html('<div>12-string</div>').with_params(foo1: 12, foo2: "string")
76
- `window.console.warn = org_warn_console; window.console.error = org_error_console;`
77
-
78
- expect(`log[0]`).to match(/Warning: Failed prop( type|Type): In component `Foo`\nProvided prop `foo1` could not be converted to String/)
79
- end
80
-
81
- it 'logs error in warning if validation failed' do
82
- stub_const 'Lorem', Class.new
83
- stub_const 'Foo2', Class.new(React::Component::Base)
84
- Foo2.class_eval do
85
- param :foo
86
- param :lorem, type: Lorem
87
- param :bar, default: nil, type: String
88
- def render; div; end
89
- end
90
-
91
- %x{
92
- var log = [];
93
- var org_warn_console = window.console.warn;
94
- var org_error_console = window.console.error;
95
- window.console.warn = window.console.error = function(str){log.push(str)}
96
- }
97
- renderToDocument(Foo2, bar: 10, lorem: Lorem.new)
98
- `window.console.warn = org_warn_console; window.console.error = org_error_console;`
99
-
100
- expect(`log[0]`).to match(/Warning: Failed prop( type|Type): In component `Foo2`\nRequired prop `foo` was not specified\nProvided prop `bar` could not be converted to String/)
101
- end
102
-
103
- it 'should not log anything if validation passes' do
104
- stub_const 'Lorem', Class.new
105
- stub_const 'Foo', Class.new(React::Component::Base)
106
- Foo.class_eval do
107
- param :foo
108
- param :lorem, type: Lorem
109
- param :bar, default: nil, type: String
110
-
111
- def render; div; end
112
- end
113
-
114
- %x{
115
- var log = [];
116
- var org_warn_console = window.console.warn;
117
- var org_error_console = window.console.error;
118
- window.console.warn = window.console.error = function(str){log.push(str)}
119
- }
120
- renderToDocument(Foo, foo: 10, bar: '10', lorem: Lorem.new)
121
- `window.console.warn = org_warn_console; window.console.error = org_error_console;`
122
- expect(`log`).to eq([])
123
- end
124
-
125
- describe 'advanced type handling' do
126
- before(:each) do
127
- %x{
128
- window.dummy_log = [];
129
- window.org_warn_console = window.console.warn;
130
- window.org_error_console = window.console.warn
131
- window.console.warn = window.console.error = function(str){window.dummy_log.push(str)}
132
- }
133
- stub_const 'Foo', Class.new(React::Component::Base)
134
- Foo.class_eval { def render; ""; end }
135
- end
136
- after(:each) do
137
- `window.console.warn = window.org_warn_console; window.console.error = window.org_error_console`
138
- end
139
-
140
- it "can use the [] notation for arrays" do
141
- Foo.class_eval do
142
- param :foo, type: []
143
- param :bar, type: []
144
- end
145
- renderToDocument(Foo, foo: 10, bar: [10])
146
- expect(`window.dummy_log[0]`).to match(/Warning: Failed prop( type|Type): In component `Foo`\nProvided prop `foo` could not be converted to Array/)
147
- end
148
-
149
- it "can use the [xxx] notation for arrays of a specific type" do
150
- Foo.class_eval do
151
- param :foo, type: [String]
152
- param :bar, type: [String]
153
- end
154
- renderToDocument(Foo, foo: [10], bar: ["10"])
155
- expect(`window.dummy_log[0]`).to match(/Warning: Failed prop( type|Type): In component `Foo`\nProvided prop `foo`\[0\] could not be converted to String/)
156
- end
157
-
158
- it "can convert a json hash to a type" do
159
- stub_const "BazWoggle", Class.new
160
- BazWoggle.class_eval do
161
- def initialize(kind)
162
- @kind = kind
163
- end
164
- attr_accessor :kind
165
- def self._react_param_conversion(json, validate_only)
166
- new(json[:bazwoggle]) if json[:bazwoggle]
167
- end
168
- end
169
- Foo.class_eval do
170
- param :foo, type: BazWoggle
171
- param :bar, type: BazWoggle
172
- param :baz, type: [BazWoggle]
173
- def render
174
- "#{params.bar.kind}, #{params.baz[0].kind}"
175
- end
176
- end
177
-
178
- params = { foo: "", bar: { bazwoggle: 1 }, baz: [{ bazwoggle: 2 }] }
179
- expect(Foo).to render_static_html('<span>1, 2</span>').with_params(params)
180
- expect(`window.dummy_log[0]`).to match(/Warning: Failed prop( type|Type): In component `Foo`\nProvided prop `foo` could not be converted to BazWoggle/)
181
- end
182
-
183
- describe "converts params only once" do
184
- it "not on every access" do
185
- stub_const "BazWoggle", Class.new
186
- BazWoggle.class_eval do
187
- def initialize(kind)
188
- @kind = kind
189
- end
190
- attr_accessor :kind
191
- def self._react_param_conversion(json, validate_only)
192
- new(json[:bazwoggle]) if json[:bazwoggle]
193
- end
194
- end
195
- Foo.class_eval do
196
- param :foo, type: BazWoggle
197
- def render
198
- params.foo.kind = params.foo.kind+1
199
- "#{params.foo.kind}"
200
- end
201
- end
202
- expect(Foo).to render_static_html('<span>2</span>').with_params(foo: {bazwoggle: 1})
203
- end
204
-
205
- it "even if contains an embedded native object"
206
- # its not clear what this test was trying to accomplish...
207
- # do
208
- # pending 'Fix after merging'
209
- # stub_const "Bar", Class.new(React::Component::Base)
210
- # stub_const "BazWoggle", Class.new
211
- # BazWoggle.class_eval do
212
- # def initialize(kind)
213
- # @kind = kind
214
- # end
215
- # attr_accessor :kind
216
- # def self._react_param_conversion(json, validate_only)
217
- # new(JSON.from_object(json[0])[:bazwoggle]) if JSON.from_object(json[0])[:bazwoggle]
218
- # end
219
- # end
220
- # Bar.class_eval do
221
- # param :foo, type: BazWoggle
222
- # def render
223
- # params.foo.kind.to_s
224
- # end
225
- # end
226
- # Foo.class_eval do
227
- # export_state :change_me
228
- # before_mount do
229
- # Foo.change_me! "initial"
230
- # end
231
- # def render
232
- # Bar(foo: Native([`{bazwoggle: #{Foo.change_me}}`]))
233
- # end
234
- # end
235
- # div = `document.createElement("div")`
236
- # React.render(React.create_element(Foo, {}), div)
237
- # Foo.change_me! "updated"
238
- # expect(`div.children[0].innerHTML`).to eq("updated")
239
- # end
240
- end
241
-
242
- it "will alias a Proc type param" do
243
- Foo.class_eval do
244
- param :foo, type: Proc
245
- def render
246
- params.foo
247
- end
248
- end
249
- expect(Foo).to render_static_html('<span>works!</span>').with_params(foo: lambda { 'works!' })
250
- end
251
-
252
- it "will create a 'bang' (i.e. update) method if the type is React::Observable" do
253
- Foo.class_eval do
254
- param :foo, type: React::Observable
255
- before_mount do
256
- params.foo! "ha!"
257
- end
258
- def render
259
- params.foo
260
- end
261
- end
262
- current_state = ""
263
- observer = React::Observable.new(current_state) { |new_state| current_state = new_state }
264
- expect(Foo).to render_static_html('<span>ha!</span>').with_params(foo: observer)
265
- expect(current_state).to eq("ha!")
266
- end
267
- end
268
- end
269
- end