-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathreact_spec.rb
655 lines (541 loc) · 24.4 KB
/
react_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
gem 'minitest'
require 'minitest/autorun'
require 'ruby2js/filter/react'
require 'ruby2js/filter/functions'
require 'ruby2js/filter/jsx'
require 'ruby2js/filter/esm'
describe Ruby2JS::Filter::React do
def to_js(string)
_(Ruby2JS.convert(string, filters: [Ruby2JS::Filter::React],
scope: self).to_s)
end
def to_js6(string)
_(Ruby2JS.convert(string, eslevel: 2015,
filters: [Ruby2JS::Filter::React, Ruby2JS::Filter::Functions,
Ruby2JS::Filter::JSX]).to_s)
end
def to_esm(string)
_(Ruby2JS.convert(string, eslevel: 2015,
filters: [Ruby2JS::Filter::React, Ruby2JS::Filter::ESM]).to_s)
end
describe :createClass do
it "should create classes" do
to_js( 'class Foo<React; end' ).
must_equal 'var Foo = React.createClass({displayName: "Foo"})'
end
it "should create methods" do
to_js( 'class Foo<React; def f(); end; end' ).
must_include 'f: function() {}'
end
it "should convert initialize methods to getInitialState" do
to_js( 'class Foo<React; def initialize(); end; end' ).
must_include 'getInitialState: function() {return {}}'
end
it "should create default getInitialState methods" do
to_js( 'class Foo<React; def foo(); @i=1; end; end' ).
must_include 'getInitialState: function() {return {}}'
end
it "should autobind event handlers" do
to_js( 'class Foo<React; def render; _a onClick: handleClick; end; ' +
'def handleClick(event); end; end' ).
must_include 'this.handleClick = this.handleClick.bind(this)'
end
it "should initialize, accumulate, and return state" do
to_js( 'class Foo<React; def initialize; @a=1; b=2; @b = b; end; end' ).
must_include 'getInitialState: function() {this.state = {a: 1}; ' +
'var b = 2; this.state.b = b; return this.state}'
end
it "should collapse instance variable assignments into a return" do
to_js( 'class Foo<React; def initialize; @a=1; @b=2; end; end' ).
must_include 'getInitialState: function() {return {a: 1, b: 2}}'
end
it "should handle parallel instance variable assignments" do
to_js( 'class Foo<React; def initialize; @a=@b=1; end; end' ).
must_include 'getInitialState: function() {return {a: 1, b: 1}}'
end
it "should handle operator assignments on state values" do
to_js( 'class Foo<React; def initialize; @a+=1; end; end' ).
must_include 'this.state = {}; this.state.a++; return this.state'
end
it "should handle calls to methods" do
to_js( 'class Foo<React; def a(); b(); end; def b(); end; end' ).
must_include 'this.b()'
end
it "should NOT handle local variables" do
to_js( 'class Foo<React; def a(); b; end; def b(); end; end' ).
wont_include 'this.b()'
end
end
describe "React create element calls" do
it "should should be able to render using only React.createElement directly" do
to_js( 'class Foo<React; def render; ' +
'React.createElement("h1", nil, React.createElement("a", nil, href = ".")); end; end' ).
must_include 'return React.createElement("h1", null, React.createElement("a", null, href = "."))'
end
end
describe "Wunderbar/JSX processing" do
it "should create elements for HTML tags" do
to_js( 'class Foo<React; def render; _a; end; end' ).
must_include 'return React.createElement("a")'
end
it "should create elements for React Components" do
to_js( 'class Foo<React; def render; _A; end; end' ).
must_include 'return React.createElement(A)'
end
it "should create elements with attributes and text" do
to_js( 'class Foo<React; def render; _a "name", href: "link"; end; end' ).
must_include 'return React.createElement("a", {href: "link"}, "name")}})'
end
it "should create simple nested elements" do
to_js( 'class Foo<React; def render; _a {_b}; end; end' ).
must_include ' React.createElement("a", null, React.createElement("b"))'
end
it "should handle options with blocks" do
to_js( 'class Foo<React; def render; _a options do _b; end; end; end' ).
must_include ' React.createElement("a", options, ' +
'React.createElement("b"))'
end
unless RUBY_VERSION =~ /^1/
it "should handle **options" do
to_js( 'class Foo<React; def render; _a **options; end; end' ).
must_include ' React.createElement("a", options)'
end
it "should handle **options with blocks" do
to_js('class Foo<React; def render; _a **options do _b; end; end; end').
must_include ' React.createElement("a", options, ' +
'React.createElement("b"))'
end
end
it "should create complex nested elements" do
result = to_js('class Foo<React; def render; _a {c="c"; _b c}; end; end')
result.must_include 'React.createElement.apply(React, function() {'
result.must_include 'var $_ = ["a", null];'
result.must_include '$_.push(React.createElement("b", null, c));'
result.must_include 'return $_'
result.must_include '}())'
end
it "should treat explicit calls to React.createElement as simple" do
to_js( 'class Foo<React; def render; _a {React.createElement("b")}; ' +
'end; end' ).
must_include ' React.createElement("a", null, React.createElement("b"))'
end
it "should push results of explicit calls to React.createElement" do
result = to_js('class Foo<React; def render; _a {c="c"; ' +
'React.createElement("b", null, c)}; end; end')
result.must_include 'React.createElement.apply(React, function() {'
result.must_include 'var $_ = ["a", null];'
result.must_include '$_.push(React.createElement("b", null, c));'
result.must_include 'return $_'
result.must_include '}())'
end
it "should handle call with blocks to React.createElement" do
result = to_js( 'class Foo<React; def render; ' +
'React.createElement("a") {_b}; end; end' )
result.must_include 'React.createElement.apply(React,'
result.must_include 'function() {var $_ = ["a"];'
result.must_include '$_.push(React.createElement("b")'
end
it "should iterate" do
# single element each iteration
result = to_js('class Foo<React; def render; _ul list ' +
'do |i| _li i; end; end; end')
result.must_include 'React.createElement("ul", null,'
result.must_include 'list.map(function(i) {'
result.must_include 'return React.createElement("li", null, i)'
# multiple elements each iteration
result = to_js('class Foo<React; def render; _dl list ' +
'do |i| _dt i.term; _dd i.defn; end; end; end')
result.must_include 'React.createElement.apply(React, function() {'
result.must_include 'var $_ = ["dl", null];'
result.must_include 'list.forEach(function(i)'
result.must_include '$_.push(React.createElement("dt", null, i.term))'
result.must_include '$_.push(React.createElement("dd", null, i.defn))'
result.must_include 'return $_'
result.must_include '}())'
end
it "should iterate with markaby style classes/ids" do
# single element each iteration
result = to_js('class Foo<React; def render; _ul.todos list ' +
'do |i| _li i; end; end; end')
result.must_include 'React.createElement("ul", {className: "todos"},'
result.must_include 'list.map(function(i) {'
result.must_include 'return React.createElement("li", null, i)'
# multiple elements each iteration
result = to_js('class Foo<React; def render; _dl.terms list ' +
'do |i| _dt i.term; _dd i.defn; end; end; end')
result.must_include 'React.createElement.apply(React, function() {'
result.must_include 'var $_ = ["dl", {className: "terms"}];'
result.must_include 'list.forEach(function(i)'
result.must_include '$_.push(React.createElement("dt", null, i.term))'
result.must_include '$_.push(React.createElement("dd", null, i.defn))'
result.must_include 'return $_'
result.must_include '}())'
end
it "should handle text nodes" do
to_js( 'class Foo<React; def render; _a {_ @text}; end; end' ).
must_include 'return React.createElement("a", null, this.state.text)'
end
it "should apply text nodes" do
to_js( 'class Foo<React; def render; _a {text="hi"; _ text}; end; end' ).
must_include 'var text = "hi"; $_.push(text);'
end
it "should handle arbitrary nodes" do
to_js( 'class Foo<React; def render; _a {_[@text]}; end; end' ).
must_include 'return React.createElement("a", null, this.state.text)'
end
it "should handle lists of arbitrary nodes" do
to_js( 'class Foo<React; def render; _a {_[@text, @text]}; end; end' ).
must_include 'return React.createElement.apply(React, ' +
'["a", null].concat([this.state.text, this.state.text])'
end
it "should apply arbitrary nodes" do
to_js( 'class Foo<React; def render; _a {text="hi"; _[text]}; end; end' ).
must_include 'var text = "hi"; $_.push(text);'
end
it "should apply list of arbitrary nodes" do
to_js( 'class Foo<React; def render; _a {text="hi"; _[text, text]}; end; end' ).
must_include 'var text = "hi"; $_.push(text, text);'
end
end
describe "JSX" do
it "should wrap list" do
to_js( 'class Foo<React; def render; %x{<p/><p/>}; end; end' ).
must_include 'React.createElement(React.Fragment, null, ' +
'React.createElement("p"), React.createElement("p"))'
end
it "should handle stateless components" do
to_js( 'Button = ->(x) { %x(<button>{x}</button>)}' ).
must_equal 'var Button = function(x) {return React.createElement("button", null, x)}'
end
end
describe "render method" do
it "should wrap multiple elements with a Fragment" do
result = to_js( 'class Foo<React; def render; _h1 "a"; _p "b"; end; end' )
result.must_include 'return React.createElement(React.Fragment, null, React'
result.must_include ', React.createElement("h1", null, "a"),'
result.must_include ', React.createElement("p", null, "b"))}})'
end
it "should wrap anything that is not a method or block call with a span" do
result = to_js( 'class Foo<React; def render; if @a; _p "a"; else;_p "b"; end; end;end' )
result.must_include 'return React.createElement.apply(React'
result.must_include 'push(React.createElement("p", null, "a"))} else {'
result.must_include 'push(React.createElement("p", null, "b"))};'
end
end
describe "class attributes" do
it "should handle class attributes" do
to_js( 'class Foo<React; def render; _a class: "b"; end; end' ).
must_include 'React.createElement("a", {className: "b"})'
end
it "should handle className attributes" do
to_js( 'class Foo<React; def render; _a className: "b"; end; end' ).
must_include 'React.createElement("a", {className: "b"})'
end
it "should handle markaby syntax" do
to_js( 'class Foo<React; def render; _a.b.c href: "d"; end; end' ).
must_include 'React.createElement("a", {className: "b c", href: "d"})'
end
it "should handle mixed strings" do
to_js( 'class Foo<React; def render; _a.b class: "c"; end; end' ).
must_include 'React.createElement("a", {className: "b c"})'
end
it "should handle mixed strings and a value" do
to_js( 'class Foo<React; def render; _a.b class: c; end; end' ).
must_include 'React.createElement("a", {className: "b " + (c || "")})'
end
it "should handle mixed strings and a conditional value" do
to_js( 'class Foo<React; def render; _a.b class: ("c" if d); end; end' ).
must_include 'React.createElement("a", {className: "b " + (d ? "c" : "")})'
end
it "should handle only a value" do
to_js( 'class Foo<React; def render; _a class: c; end; end' ).
must_include 'React.createElement("a", {className: c})'
end
it "should handle a constant string" do
to_js( 'class Foo<React; def render; _a class: "x"; end; end' ).
must_include 'React.createElement("a", {className: "x"})'
end
end
describe "other attributes" do
it "should handle markaby syntax ids" do
to_js( 'class Foo<React; def render; _a.b! href: "c"; end; end' ).
must_include 'React.createElement("a", {id: "b", href: "c"})'
end
it "should map for attributes to htmlFor" do
to_js( 'class Foo<React; def render; _a for: "b"; end; end' ).
must_include 'React.createElement("a", {htmlFor: "b"})'
end
it "should map case insensitive attributes to javascript properties" do
to_js( 'class Foo<React; def render; _input tabindex: 1; end; end' ).
must_include 'React.createElement("input", {tabIndex: 1})'
end
it "should map style string attributes to hashes" do
to_js( 'class Foo<React; def render; _a ' +
'style: "color: blue; margin-top: 0"; end; end' ).
must_include '{style: {color: "blue", marginTop: 0}}'
end
end
describe "~refs" do
it "should handle ~ symbols properties" do
to_js( 'class Foo<React; def method; ~x.textContent; end; end' ).
must_include 'this.refs.x.textContent'
end
it "should handle ~ lvar properties" do
to_js( 'class Foo<React; def method; text = ~x.textContent; end; end' ).
must_include 'text = this.refs.x.textContent'
end
it "should handle ~ methods" do
to_js( 'class Foo<React; def method; ~x.remove(); end; end' ).
must_include 'this.refs.x.remove()'
end
it "should convert ~(expression) to querySelector calls" do
to_js( 'class Foo<React; def method; ~(x).remove(); end; end' ).
must_include 'document.querySelector(x).remove()'
end
it "should convert ~'a b' to querySelector calls" do
to_js( 'class Foo<React; def method; ~"a b".remove(); end; end' ).
must_include 'document.querySelector("a b").remove()'
end
it "should convert ~'.a.b_c' to getElementsByClassName calls" do
to_js( 'class Foo<React; def method; ~".a.b_c".remove(); end; end' ).
must_include 'document.getElementsByClassName("a b-c")[0].remove()'
end
it "should convert ~'#a_b' to getElementById calls" do
to_js( 'class Foo<React; def method; ~"#a_b".remove(); end; end' ).
must_include 'document.getElementById("a-b").remove()'
end
it "should convert ~'a_b' to getElementsByTagName calls" do
to_js( 'class Foo<React; def method; ~"a_b".remove(); end; end' ).
must_include 'document.getElementsByTagName("a-b")[0].remove()'
end
it "should leave ~~a alone" do
to_js( 'class Foo<React; def method; ~~a; end; end' ).
must_include '~~a'
end
it "should convert ~~~a to ~a" do
to_js( 'class Foo<React; def method; ~~~a; end; end' ).
must_include '~a'
end
end
describe "map gvars/ivars/cvars to refs/state/prop" do
it "should map global variables to refs" do
to_js( 'class Foo<React; def method; $x; end; end' ).
must_include 'this.refs.x'
end
it "should map instance variables to state" do
to_js( 'class Foo<React; def method; @x; end; end' ).
must_include 'this.state.x'
end
it "should map setting instance variables to setState" do
to_js( 'class Foo<React; def method; @x=1; end; end' ).
must_include 'this.setState({x: 1})'
end
it "should map parallel instance variables to setState" do
to_js( 'class Foo<React; def method(); @x=@y=1; end; end' ).
must_include 'this.setState({x: 1, y: 1})'
end
it "should map consecutive instance variables to setState" do
to_js( 'class Foo<React; def method(); @x=1; @y=2; end; end' ).
must_include 'this.setState({x: 1, y: 2})'
end
it "should create temporary variables when needed" do
to_js( 'class Foo<React; def f; @a+=1; b=@a; end; end' ).
must_include 'var $a = this.state.a; $a++; var b = $a; ' +
'return this.setState({a: $a})'
end
it "should create temporary variables when conditionals are involved" do
to_js( 'class Foo<React; def f; @a+=1 if 1; b=@a; end; end' ).
must_include 'var $a = this.state.a; if (1) {$a++}; var b = $a; ' +
'return this.setState({a: $a})'
end
it "should create temporary variables when blocks are involved" do
to_js( 'class Foo<React; def f; foo {@a=1}; b=@a; end; end' ).
must_include 'foo(function() {self.setState({a: $a = 1})}); '
'var b = $a; return this.setState({a: $a})'
end
it "should create temporary variables when blocks+opasgn are involved" do
to_js( 'class Foo<React; def f; foo {@a+=1}; b=@a; end; end' ).
must_include 'var $a = this.state.a; ' +
'foo(function() {self.setState({a: ++$a})}); var b = $a; ' +
'return this.setState({a: $a})'
end
it "shouldn't produce temporary variables for inline event handlers" do
js = to_js( 'class F < React; def render; _input value: @draft; ' +
'_button "Cancel", onClick:-> {@draft = @base}; ' +
'_button "Save", disabled: @draft == @base; end; end' )
js.must_include 'self.setState({draft: event.target.value})'
js.must_include '{onClick: function() ' +
'{self.setState({draft: self.state.base})}}'
js.must_include '{disabled: this.state.draft == this.state.base}'
end
it "should treat singleton method definitions as a separate scope" do
js = to_js( 'class F < React; def m(); def x.a; @i=1; end; @i; end; end' )
js.must_include 'this.setState({i: 1})'
js.must_include 'this.state.i'
end
it "should generate code to handle instance vars within singleton method" do
js = to_js('class F < React; def m(); def x.a; @i=1; @i+1; end; end; end')
js.must_include '$i = 1'
js.must_include '$i + 1'
js.must_include 'this.setState({i: $i}'
end
it "should map class variables to properties" do
to_js( 'class Foo<React; def method; @@x; end; end' ).
must_include 'this.props.x'
end
it "should not support assigning to class variables" do
_(proc {
to_js( 'class Foo<React; def method; @@x=1; end; end' )
}).must_raise NotImplementedError
end
end
describe "method calls" do
it "should handle ivars" do
to_js( 'class Foo<React; def method; @x.(); end; end' ).
must_include 'this.state.x()'
end
it "should handle cvars" do
to_js( 'class Foo<React; def method; @@x.(); end; end' ).
must_include 'this.props.x()'
end
it "should handle gvars" do
to_js( 'class Foo<React; def method; $x.(); end; end' ).
must_include 'this.refs.x()'
end
end
describe 'react calls' do
it 'should create elements' do
to_js( 'ReactDOM.render _Element, document.getElementById("sidebar")' ).
must_include 'React.createElement(Element)'
end
it 'should substitute scope instance variables / props' do
@data = 5
to_js( "ReactDOM.render _Element(data: @data),
document.getElementById('sidebar')" ).
must_include 'React.createElement(Element, {data: 5})'
end
end
describe "react statics" do
it "should handle static properties" do
to_js( 'class Foo<React; def self.one; 1; end; end' ).
must_include 'statics: {one: 1}'
end
it "should handle computed static properties" do
to_js( 'class Foo<React; def self.one; return 1; end; end' ).
must_include 'statics: {get one() {return 1}}'
end
it "should handle static methods" do
to_js( 'class Foo<React; def self.one(); return 1; end; end' ).
must_include 'statics: {one: function() {return 1}}'
end
end
describe "componentWillReceiveProps" do
it "should should insert props on calls to componentWillReceiveProps" do
to_js( 'class Foo<React; def componentWillMount();' +
'self.componentWillReceiveProps(); end; end' ).
must_include 'this.componentWillReceiveProps(this.props)'
end
it "should should insert props arg on componentWillReceiveProps" do
to_js( 'class Foo<React; def componentWillReceiveProps();' +
'@foo = @@foo; end; end' ).
must_include 'function($$props) {this.setState({foo: $$props.foo})}'
end
it "should should use props arg on componentWillReceiveProps" do
to_js( 'class Foo<React; def componentWillReceiveProps(props);' +
'@foo = @@foo; end; end' ).
must_include 'function(props) {this.setState({foo: props.foo})}'
end
end
describe "controlled components" do
it "should should automatically create onChange value functions" do
to_js( 'class Foo<React; def render; _input value: @x; end; end' ).
must_include 'onChange: function(event) {self.setState({x: event.target.value}'
end
it "should should automatically create onChange checked functions" do
to_js( 'class Foo<React; def render; _input checked: @x; end; end' ).
must_include 'onChange: function() {self.setState({x: !self.state.x}'
end
it "should should retain onChange functions" do
to_js( 'class Foo<React; def render; _input checked: @x, onChange: self.change; end; end' ).
must_include 'onChange: this.change'
end
end
describe "es6 support" do
it "should create classes" do
to_js6( 'class Foo<React::Component; end' ).
must_equal 'class Foo extends React.Component {}'
end
it "should handle contructors" do
to_js6( 'class Foo<React::Component; def initialize; @x=1; end; end' ).
must_include 'constructor() {super(); this.state = {x: 1}}'
end
it "should add props arg to contructors if needed" do
to_js6( 'class Foo<React::Component; def initialize; @x=@@y; end; end' ).
must_include 'constructor(prop$) {super(prop$); this.state = {x: this.props.y}}'
end
it "should handle static properties" do
to_js6( 'class Foo<React; def self.one; 1; end; end' ).
must_include 'static get one() {return 1}'
end
it "should handle calls to getters" do
result = to_js6( 'class Foo<React; def a(); console.log b; end; def b; end; end' )
result.must_include 'get b() {'
result.must_include 'console.log(this.b)'
end
it "should handle calls to setters" do
result = to_js6( 'class Foo<React; def a(); b=1; end; def b=(b); @b=b; end; end' )
result.must_include 'set b(b) {'
result.must_include 'this.b = 1'
end
it "should not treat lifecycle methods as getters" do
result = to_js6( 'class Foo<React::Component; def render; _br; end; end' )
result.must_include 'render() {'
result.wont_include 'get render() {'
end
end
describe "wunderbar filter/JSX integration" do
it "should handle simple calls" do
to_js6( 'class Foo<React::Component; def render; _br; end; end' ).
must_include 'render() {return <br/>}'
end
it "should handle multiple calls" do
to_js6( 'class Foo<React::Component; def render; _br; _br; end; end' ).
must_include 'render() {return <><br/><br/></>}'
end
it "should not wrap non wunderbar calls" do
to_js6( 'class Foo<React::Component; def render; x="a"; _p x; end; end' ).
must_include 'render() {let x = "a"; return <><p>{x}</p></>}}'
end
it "should handle if statements" do
to_js6( 'class Foo<React::Component; def render; _br if @@x; end; end' ).
must_include '{return <>{this.props.x ? <br/> : null}</>}'
end
it "should handle loops" do
to_js6( 'class Foo<React::Component; def render; _ul {@@x.each {|i| _li i; }}; end; end' ).
must_include '<ul>{this.props.x.map(i => (<li>{i}</li>))}</ul>'
end
end
describe :autoimports do
it "should not autoimport React unless ESM is included" do
to_js6( 'class Foo<React; end' ).
wont_include 'import React from "react";'
end
it "should autoimport React if ESM is included" do
to_esm( 'class Foo<React; end' ).
must_include 'import React from "react";'
end
it "should not autoimport ReactDOM unless ESM is included" do
to_js( 'ReactDOM.render _h1("hello world"), document.getElementById("root")' ).
wont_include 'import ReactDOM from "react-dom";'
end
it "should autoimport ReactDOM if ESM is included" do
to_esm( 'ReactDOM.render _h1("hello world"), document.getElementById("root")' ).
must_include 'import ReactDOM from "react-dom";'
end
end
describe Ruby2JS::Filter::DEFAULTS do
it "should include React" do
_(Ruby2JS::Filter::DEFAULTS).must_include Ruby2JS::Filter::React
end
end
end