This repository was archived by the owner on Oct 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcomponent_spec.rb
1114 lines (995 loc) · 32.2 KB
/
component_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'spec_helper'
describe 'React::Component', js: true do
it 'defines component spec methods' do
on_client do
class Foo
include React::Component
def initialize(native = nil)
end
def render
React.create_element('div')
end
end
end
# class methods
expect_evaluate_ruby("Foo.respond_to?(:initial_state)").to be_truthy
expect_evaluate_ruby("Foo.respond_to?(:default_props)").to be_truthy
expect_evaluate_ruby("Foo.respond_to?(:prop_types)").to be_truthy
# instance_methods
expect_evaluate_ruby("Foo.new.respond_to?(:component_will_mount)").to be_truthy
expect_evaluate_ruby("Foo.new.respond_to?(:component_did_mount)").to be_truthy
expect_evaluate_ruby("Foo.new.respond_to?(:component_will_receive_props)").to be_truthy
expect_evaluate_ruby("Foo.new.respond_to?(:should_component_update?)").to be_truthy
expect_evaluate_ruby("Foo.new.respond_to?(:component_will_update)").to be_truthy
expect_evaluate_ruby("Foo.new.respond_to?(:component_did_update)").to be_truthy
expect_evaluate_ruby("Foo.new.respond_to?(:component_will_unmount)").to be_truthy
end
describe 'Life Cycle' do
before(:each) do
on_client do
class Foo
include React::Component
def self.call_history
@call_history ||= []
end
def render
React.create_element('div') { 'lorem' }
end
end
end
end
it 'invokes `before_mount` registered methods when `componentWillMount()`' do
mount 'Foo' do
Foo.class_eval do
before_mount :bar, :bar2
def bar; self.class.call_history << "bar"; end
def bar2; self.class.call_history << "bar2"; end
end
end
expect_evaluate_ruby("Foo.call_history").to eq(["bar", "bar2"])
end
it 'invokes `after_mount` registered methods when `componentDidMount()`' do
mount 'Foo' do
Foo.class_eval do
after_mount :bar3, :bar4
def bar3; self.class.call_history << "bar3"; end
def bar4; self.class.call_history << "bar4"; end
end
end
expect_evaluate_ruby("Foo.call_history").to eq(["bar3", "bar4"])
end
it 'allows multiple class declared life cycle hooker' do
evaluate_ruby do
Foo.class_eval do
before_mount :bar
def bar; self.class.call_history << "bar"; end
end
class FooBar
include React::Component
after_mount :bar2
def self.call_history
@call_history ||= []
end
def bar2; self.class.call_history << "bar2"; end
def render
React.create_element('div') { 'lorem' }
end
end
instance = React::Test::Utils.render_component_into_document(Foo)
instance = React::Test::Utils.render_component_into_document(FooBar)
end
expect_evaluate_ruby("Foo.call_history").to eq(["bar"])
expect_evaluate_ruby("FooBar.call_history").to eq(["bar2"])
end
it 'allows block for life cycle callback' do
expect_evaluate_ruby do
Foo.class_eval do
before_mount do
set_state({ foo: "bar" })
end
end
instance = React::Test::Utils.render_component_into_document(Foo)
instance.state[:foo]
end.to eq('bar')
end
it 'invokes :after_error when componentDidCatch' do
client_option raise_on_js_errors: :off
mount 'Foo' do
class ErrorFoo
include Hyperloop::Component::Mixin
param :just
def render
raise 'ErrorFoo Error'
end
end
Foo.class_eval do
def self.get_error
@@error
end
def self.get_info
@@info
end
def render
DIV { ErrorFoo(just: :a_param) }
end
after_error do |error, info|
@@error = error.message
@@info = info[:componentStack]
end
end
end
expect_evaluate_ruby('Foo.get_error').to eq('ErrorFoo Error')
expect_evaluate_ruby('Foo.get_info').to eq("\n in ErrorFoo\n in div\n in Foo\n in React::TopLevelRailsComponent")
end
end
describe 'Misc Methods' do
it 'has a force_update! method' do
mount 'Foo' do
class Foo < Hyperloop::Component
class << self
attr_accessor :render_counter
attr_accessor :instance
end
before_mount do
Foo.render_counter = 0
Foo.instance = self
end
def render
Foo.render_counter += 1
DIV { "I have been rendered #{Foo.render_counter} times" }
end
end
end
expect_evaluate_ruby do
Foo.instance.force_update!
Foo.render_counter
end.to eq(2)
end
it 'has its force_update! method return itself' do
mount 'Foo' do
class Foo < Hyperloop::Component
class << self
attr_accessor :instance
end
before_mount do
Foo.instance = self
end
def render
DIV { "I have been rendered" }
end
end
end
expect_evaluate_ruby('Foo.instance == Foo.instance.force_update!').to be_truthy
end
end
describe 'New style setter & getter' do
before(:each) do
on_client do
class Foo
include React::Component
def render
div { state.foo }
end
end
end
end
it 'implicitly will create a state variable when first written' do
mount 'Foo' do
Foo.class_eval do
before_mount do
state.foo! 'bar'
end
end
end
# this was a 'have_xpath' check, but these are totally unreliable in capybara with webdrivers
# leading to false positives and negatives
# this simple check for string inclusion makes this checks reliable
expect(page.body[-35..-19]).to include("<div>bar</div>")
end
it 'allows kernal method names like "format" to be used as state variable names' do
mount 'Foo' do
Foo.class_eval do
before_mount do
state.format! 'yes'
state.foo! state.format
end
end
end
expect(page.body[-35..-19]).to include("<div>yes</div>")
end
it 'returns an observer with the bang method and no arguments' do
mount 'Foo' do
Foo.class_eval do
before_mount do
state.foo!(state.baz!.class.name)
end
end
end
expect(page.body[-50..-19]).to include("<div>React::Observable</div>")
end
it 'returns the current value of a state when written' do
mount 'Foo' do
Foo.class_eval do
before_mount do
state.baz! 'bar'
state.foo!(state.baz!('pow'))
end
end
end
expect(page.body[-35..-19]).to include("<div>bar</div>")
end
it 'can access an explicitly defined state`' do
mount 'Foo' do
Foo.class_eval do
define_state foo: :bar
end
end
expect(page.body[-35..-19]).to include("<div>bar</div>")
end
end
describe 'State setter & getter' do
before(:each) do
on_client do
class Foo
include React::Component
def render
React.create_element('div') { 'lorem' }
end
end
end
end
it 'defines setter using `define_state`' do
expect_evaluate_ruby do
Foo.class_eval do
define_state :foo
before_mount :set_up
def set_up
mutate.foo 'bar'
end
end
instance = React::Test::Utils.render_component_into_document(Foo)
instance.state.foo
end.to eq('bar')
end
it 'defines init state by passing a block to `define_state`' do
expect_evaluate_ruby do
element_to_render = React.create_element(Foo)
Foo.class_eval do
define_state(:foo) { 10 }
end
dom_el = JS.call(:eval, "document.body.appendChild(document.createElement('div'))")
instance = React.render(element_to_render, dom_el)
instance.state.foo
end.to eq(10)
end
it 'defines getter using `define_state`' do
expect_evaluate_ruby do
Foo.class_eval do
define_state(:foo) { 10 }
before_mount :bump
def bump
mutate.foo(state.foo + 20)
end
end
instance = React::Test::Utils.render_component_into_document(Foo)
instance.state.foo
end.to eq(30)
end
it 'defines multiple state accessors by passing array to `define_state`' do
expect_evaluate_ruby do
Foo.class_eval do
define_state :foo, :foo2
before_mount :set_up
def set_up
mutate.foo 10
mutate.foo2 20
end
end
instance = React::Test::Utils.render_component_into_document(Foo)
[ instance.state.foo, instance.state.foo2 ]
end.to eq([10, 20])
end
it 'invokes `define_state` multiple times to define states' do
expect_evaluate_ruby do
Foo.class_eval do
define_state(:foo) { 30 }
define_state(:foo2) { 40 }
end
instance = React::Test::Utils.render_component_into_document(Foo)
[ instance.state.foo, instance.state.foo2 ]
end.to eq([30, 40])
end
it 'can initialize multiple state variables with a block' do
expect_evaluate_ruby do
Foo.class_eval do
define_state(:foo, :foo2) { 30 }
end
instance = React::Test::Utils.render_component_into_document(Foo)
[ instance.state.foo, instance.state.foo2 ]
end.to eq([30, 30])
end
it 'can mix multiple state variables with initializers and a block' do
expect_evaluate_ruby do
Foo.class_eval do
define_state(:x, :y, foo: 1, bar: 2) {3}
end
instance = React::Test::Utils.render_component_into_document(Foo)
[ instance.state.x, instance.state.y, instance.state.foo, instance.state.bar ]
end.to eq([3, 3, 1, 2])
end
it 'gets state in render method' do
mount 'Foo' do
Foo.class_eval do
define_state(:foo) { 10 }
def render
React.create_element('div') { state.foo }
end
end
end
expect(page.body[-35..-19]).to include("<div>10</div>")
end
it 'supports original `setState` as `set_state` method' do
expect_evaluate_ruby do
Foo.class_eval do
before_mount do
self.set_state(foo: 'bar')
end
end
instance = React::Test::Utils.render_component_into_document(Foo)
instance.state[:foo]
end.to eq('bar')
end
it '`set_state!` method works and doesnt replace other state' do
# this test changed because the function replaceState is gone in react
expect_evaluate_ruby do
Foo.class_eval do
before_mount do
set_state(foo: 'bar')
set_state!(bar: 'lorem')
end
end
instance = React::Test::Utils.render_component_into_document(Foo)
[ instance.state[:foo], instance.state[:bar] ]
end.to eq(['bar', 'lorem'])
end
it 'supports original `state` method' do
mount 'Foo' do
Foo.class_eval do
before_mount do
self.set_state(foo: 'bar')
end
def render
div { self.state[:foo] }
end
end
end
expect(page.body[-35..-19]).to include("<div>bar</div>")
end
it 'transforms state getter to Ruby object' do
mount 'Foo' do
Foo.class_eval do
define_state :foo
before_mount do
mutate.foo [{a: "Hello"}]
end
def render
div { state.foo[0][:a] }
end
end
end
expect(page.body[-40..-19]).to include("<div>Hello</div>")
end
it 'sets initial state with default value in constructor in @native object state property' do
mount 'StateFoo' do
class StateFoo
include Hyperloop::Component::Mixin
state bar: 25
def initialize(native)
super(native)
@@initial_state = @native.JS[:state].JS[:bar]
end
def self.initial_state
@@initial_state ||= 0
end
def render
React.create_element('div') { 'lorem' }
end
end
end
expect_evaluate_ruby('StateFoo.initial_state').to eq(25)
end
it 'doesnt cause extra render when setting initial state' do
mount 'StateFoo' do
class StateFoo
include Hyperloop::Component::Mixin
state bar: 25
def self.render_count
@@render_count ||= 0
end
def self.incr_render_count
@@render_count ||= 0
@@render_count += 1
end
def render
StateFoo.incr_render_count
React.create_element('div') { 'lorem' }
end
end
end
expect_evaluate_ruby('StateFoo.render_count').to eq(1)
end
it 'doesnt cause extra render when setting state in :before_mount' do
mount 'StateFoo' do
class StateFoo
include Hyperloop::Component::Mixin
def self.render_count
@@render_count ||= 0
end
def self.incr_render_count
@@render_count ||= 0
@@render_count += 1
end
before_mount do
mutate.bar 50
end
def render
StateFoo.incr_render_count
React.create_element('div') { 'lorem' }
end
end
end
expect_evaluate_ruby('StateFoo.render_count').to eq(1)
end
it 'doesnt cause extra render when setting state in :before_receive_props' do
mount 'Foo' do
class StateFoo
include Hyperloop::Component::Mixin
param :drinks
def self.render_count
@@render_count ||= 0
end
def self.incr_render_count
@@render_count ||= 0
@@render_count += 1
end
before_receive_props do |new_params|
mutate.bar 50
end
def render
StateFoo.incr_render_count
React.create_element('div') { 'lorem' }
end
end
Foo.class_eval do
define_state :foo
before_mount do
state.foo 25
end
def render
div { StateFoo(drinks: state.foo) }
end
after_mount do
mutate.foo 50
end
end
end
expect_evaluate_ruby('StateFoo.render_count').to eq(2)
end
end
describe 'Props' do
describe 'this.props could be accessed through `params` method' do
before do
on_client do
class Foo
include React::Component
end
end
end
it 'reads from parent passed properties through `params`' do
mount 'Foo', prop: 'foobar' do
Foo.class_eval do
param :prop
def render
React.create_element('div') { params[:prop] }
end
end
end
expect(page.body[-40..-19]).to include("<div>foobar</div>")
end
it 'accesses nested params as orignal Ruby object' do
mount 'Foo', prop: [{foo: 10}] do
Foo.class_eval do
param :prop
def render
React.create_element('div') { params[:prop][0][:foo] }
end
end
end
expect(page.body[-35..-19]).to include("<div>10</div>")
end
end
describe 'Props Updating', v13_only: true do
before do
on_client do
class Foo
include React::Component
end
end
end
it '`setProps` as method `set_props` is no longer supported' do
expect_evaluate_ruby do
Foo.class_eval do
param :foo
def render
React.create_element('div') { params[:foo] }
end
end
instance = React::Test::Utils.render_component_into_document(Foo, foo: 10)
begin
instance.set_props(foo: 20)
rescue
'got risen'
end
end.to eq('got risen')
end
it 'original `replaceProps` as method `set_props!` is no longer supported' do
expect_evaluate_ruby do
Foo.class_eval do
param :foo
def render
React.create_element('div') { params[:foo] ? 'exist' : 'null' }
end
end
instance = React::Test::Utils.render_component_into_document(Foo, foo: 10)
begin
instance.set_props!(bar: 20)
rescue
'got risen'
end
end.to eq('got risen')
end
end
describe 'Prop validation' do
before do
on_client do
class Foo
include Hyperloop::Component::Mixin
end
end
end
it 'specifies validation rules using `params` class method' do
expect_evaluate_ruby do
Foo.class_eval do
params do
requires :foo, type: String
optional :bar
end
end
Foo.prop_types
end.to have_key('_componentValidator')
end
it 'logs error in warning if validation failed' do
evaluate_ruby do
class Lorem; end
Foo.class_eval do
params do
requires :foo
requires :lorem, type: Lorem
optional :bar, type: String
end
def render; div; end
end
React::Test::Utils.render_component_into_document(Foo, bar: 10, lorem: Lorem.new)
end
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
.to match(/Warning: Failed prop( type|Type): In component `Foo`\nRequired prop `foo` was not specified\nProvided prop `bar` could not be converted to String/)
end
it 'should not log anything if validation pass' do
evaluate_ruby do
class Lorem; end
Foo.class_eval do
params do
requires :foo
requires :lorem, type: Lorem
optional :bar, type: String
end
def render; div; end
end
React::Test::Utils.render_component_into_document(Foo, foo: 10, bar: '10', lorem: Lorem.new)
end
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n")).to_not match(/prop/)
end
end
describe 'Default props' do
it 'sets default props using validation helper' do
on_client do
class Foo
include React::Component
params do
optional :foo, default: 'foo'
optional :bar, default: 'bar'
end
def render
div { params[:foo] + '-' + params[:bar]}
end
end
end
mount 'Foo'
expect(page.body[-40..-19]).to include("<div>foo-bar</div>")
mount 'Foo', foo: 'lorem'
expect(page.body[-40..-19]).to include("<div>lorem-bar</div>")
end
end
end
describe 'Anonymous Component' do
it "will not generate spurious warning messages" do
evaluate_ruby do
foo = Class.new(React::Component::Base)
foo.class_eval do
def render; "hello" end
end
React::Test::Utils.render_component_into_document(foo)
end
expect(page.driver.browser.manage.logs.get(:browser)
.reject { |entry| entry.to_s.include?("Deprecated feature") }
.map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n").size)
.to eq(0)
end
end
describe 'Render Error Handling' do
it "will generate a message if render returns something other than an Element or a String" do
mount 'Foo' do
class Foo < React::Component::Base
def render; Hash.new; end
end
end
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
.to match(/You may need to convert this to a string./)
end
it "will generate a message if render returns a Component class" do
mount 'Foo' do
class Foo < React::Component::Base
def render; Foo; end
end
end
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
.to match(/Did you mean Foo()/)
end
it "will generate a message if more than 1 element is generated" do
mount 'Foo' do
class Foo < React::Component::Base
def render; "hello".span; "goodby".span; end
end
end
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
.to match(/Do you want to wrap your elements in a div?/)
end
it "will generate a message if the element generated is not the element returned" do
mount 'Foo' do
class Foo < React::Component::Base
def render; "hello".span; "goodby".span.delete; end
end
end
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
.to match(/Possibly improper use of Element#delete./)
end
end
describe 'Event handling' do
before do
on_client do
class Foo
include React::Component
end
end
end
it 'works in render method' do
expect_evaluate_ruby do
Foo.class_eval do
define_state(:clicked) { false }
def render
React.create_element('div').on(:click) do
mutate.clicked true
end
end
end
instance = React::Test::Utils.render_component_into_document(Foo)
React::Test::Utils.simulate_click(instance)
instance.state.clicked
end.to eq(true)
end
it 'invokes handler on `this.props` using emit' do
on_client do
Foo.class_eval do
param :on_foo_fubmit, type: Proc
after_mount :setup
def setup
self.emit(:foo_submit, 'bar')
end
def render
React.create_element('div')
end
end
end
evaluate_ruby do
element = React.create_element(Foo).on(:foo_submit) { 'bar' }
React::Test::Utils.render_into_document(element)
end
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
.to_not match(/Exception raised/)
end
it 'invokes handler with multiple params using emit' do
on_client do
Foo.class_eval do
param :on_foo_invoked, type: Proc
after_mount :setup
def setup
self.emit(:foo_invoked, [1,2,3], 'bar')
end
def render
React.create_element('div')
end
end
end
evaluate_ruby do
element = React.create_element(Foo).on(:foo_invoked) { return [1,2,3], 'bar' }
React::Test::Utils.render_into_document(element)
end
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
.to_not match(/Exception raised/)
end
end
describe '#render' do
it 'supports element building helpers' do
on_client do
class Foo
include React::Component
param :foo
def render
div do
span { params[:foo] }
end
end
end
class Bar
include React::Component
def render
div do
React::RenderingContext.render(Foo, foo: 'astring')
end
end
end
end
evaluate_ruby do
React::Test::Utils.render_component_into_document(Bar)
end
expect(page.body[-80..-19]).to include("<div><div><span>astring</span></div></div>")
end
it 'builds single node in top-level render without providing a block' do
mount 'Foo' do
class Foo
include React::Component
def render
div
end
end
end
expect(page.body).to include('<div data-react-class="React.TopLevelRailsComponent" data-react-props="{"render_params":{},"component_name":"Foo","controller":"ReactTest"}"><div></div></div>')
end
it 'redefines `p` to make method missing work' do
mount 'Foo' do
class Foo
include React::Component
def render
div {
p(class_name: 'foo')
p
div { 'lorem ipsum' }
p(id: '10')
}
end
end
end
expect(page.body).to include('<div><p class="foo"></p><p></p><div>lorem ipsum</div><p id="10"></p></div>')
end
it 'only overrides `p` in render context' do
mount 'Foo' do
class Foo
include React::Component
def self.result
@@result ||= 'ooopsy'
end
def self.result_two
@@result_two ||= 'ooopsy'
end
before_mount do
@@result = p 'first'
end
after_mount do
@@result_two = p 'second'
end
def render
p do
'third'
end
end
end
end
expect_evaluate_ruby('Kernel.p "first"').to eq('first')
expect_evaluate_ruby('p "second"').to eq('second')
expect_evaluate_ruby('Foo.result').to eq('first')
expect_evaluate_ruby('Foo.result_two').to eq('second')
expect(page.body[-40..-10]).to include("<p>third</p>")
expect(page.body[-40..-10]).not_to include("<p>first</p>")
end
end
describe 'new react 15/16 custom isMounted implementation' do
it 'returns true if after mounted' do
expect_evaluate_ruby do
class Foo
include React::Component
def render
React.create_element('div')
end
end
component = React::Test::Utils.render_component_into_document(Foo)
component.mounted?
end.to eq(true)
end
end
describe '.params_changed?' do
before(:each) do
on_client do
class Foo < React::Component::Base
def needs_update?(next_params, next_state)
next_params.changed?
end
end
end
end
it "returns false if new and old params are the same" do
expect_evaluate_ruby do
@foo = Foo.new(nil)
@foo.instance_eval { @native.JS[:props] = JS.call(:eval, 'function bla(){return {value1: 1, value2: 2};}bla();') }
@foo.should_component_update?({ value2: 2, value1: 1 }, {})
end.to be_falsy
end
it "returns true if new and old params are have different values" do
expect_evaluate_ruby do
@foo = Foo.new(nil)
@foo.instance_eval { @native.JS[:props] = JS.call(:eval, 'function bla(){return {value1: 1, value2: 2};}bla();') }
@foo.should_component_update?({value2: 2, value1: 2}, {})
end.to be_truthy
end
it "returns true if new and old params are have different keys" do
expect_evaluate_ruby do
@foo = Foo.new(nil)
@foo.instance_eval { @native.JS[:props] = JS.call(:eval, 'function bla(){return {value1: 1, value2: 2};}bla();') }
@foo.should_component_update?({value2: 2, value1: 1, value3: 3}, {})
end.to be_truthy
end
end
describe '#should_component_update?' do
before(:each) do
on_client do
class Foo < React::Component::Base
def needs_update?(next_params, next_state)
next_state.changed?
end
end
EMPTIES = [`{}`, `undefined`, `null`, `false`]
end
end
it "returns false if both new and old states are empty" do
expect_evaluate_ruby do
@foo = Foo.new(nil)
return_values = []
EMPTIES.each do |empty1|
EMPTIES.each do |empty2|
@foo.instance_eval { @native.JS[:state] = JS.call(:eval, "function bla(){return #{empty1};}bla();") }
return_values << @foo.should_component_update?({}, Hash.new(empty2))
end
end