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