-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsetupHostConfig.spec.tsx
2305 lines (1873 loc) · 73.7 KB
/
setupHostConfig.spec.tsx
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
import { createFacet, Facet } from '@react-facet/core'
import React, { ReactElement, useEffect, useState } from 'react'
import { Fiber } from 'react-reconciler'
import { createFiberRoot } from './createFiberRoot'
import { createReconciler } from './createReconciler'
import { setupHostConfig } from './setupHostConfig'
import { InputType, ElementContainer, ElementProps, Props } from './types'
document.body.innerHTML = `<div id="root"></div>`
const reconcilerInstance = createReconciler()
const root = document.getElementById('root') as HTMLElement
const fiberRoot = createFiberRoot(reconcilerInstance)(root)
/**
* Render function local to testing that shared the same instance of the reconciler.
*
* This is needed otherwise React complains that we are sharing a context across different renderers.
*/
const render = function render(ui: ReactElement) {
reconcilerInstance.updateContainer(ui, fiberRoot, null, () => {})
}
afterEach(() => {
reconcilerInstance.updateContainer(null, fiberRoot, null, () => {})
})
jest.useFakeTimers()
describe('mount', () => {
describe('without facet', () => {
it('renders text', () => {
render(<div>Hello World</div>)
expect(root).toContainHTML('<div id="root"><div>Hello World</div></div>')
})
it('sets the className', () => {
render(<div className="testing">Hello World</div>)
expect(root).toContainHTML('<div id="root"><div class="testing">Hello World</div></div>')
})
it('sets the id', () => {
render(<div id="testing">Hello World</div>)
expect(root).toContainHTML('<div id="root"><div id="testing">Hello World</div></div>')
})
it('sets the style', () => {
render(<div style={{ background: 'red' }}>Hello World</div>)
expect(root).toContainHTML('<div id="root"><div style="background: red;">Hello World</div></div>')
})
it('sets the src', () => {
render(<img className="image" src="bananafrita.png" />)
const img = document.getElementsByClassName('image')[0] as HTMLImageElement | undefined
// jsdom is adding the http://localhost
expect(img && img.src).toEqual('http://localhost/bananafrita.png')
})
it('sets the value', () => {
render(<input value="foo" />)
expect(root?.innerHTML ?? '').toBe('<input value="foo">')
})
it('sets the type', () => {
render(
<>
<input type="text" />
<input type="password" />
<input type="button" />
</>,
)
expect(root?.innerHTML ?? '').toBe('<input type="text"><input type="password"><input type="button">')
})
it('sets disabled', () => {
render(<input disabled />)
expect(root?.innerHTML ?? '').toBe('<input disabled="">')
})
it('sets cohinline', () => {
render(<fast-p cohinline />)
expect(root?.innerHTML ?? '').toBe('<p cohinline=""></p>')
})
it('sets maxlength', () => {
render(<input type="text" maxLength={10} />)
expect(root?.innerHTML ?? '').toBe('<input maxlength="10" type="text">')
})
it('sets the data-droppable', () => {
render(<div data-droppable />)
expect(root?.innerHTML ?? '').toBe('<div data-droppable=""></div>')
})
it('sets the data-narrate', () => {
render(<div data-narrate />)
expect(root?.innerHTML ?? '').toBe('<div data-narrate=""></div>')
})
it('sets the data-narrate (with value)', () => {
render(<div data-narrate="hello world" />)
expect(root?.innerHTML ?? '').toBe('<div data-narrate="hello world"></div>')
})
it('sets the data-narrate-after', () => {
render(<div data-narrate-after="this comes after" />)
expect(root?.innerHTML ?? '').toBe('<div data-narrate-after="this comes after"></div>')
})
it('sets the data-narrate-before', () => {
render(<div data-narrate-before="and this comes before" />)
expect(root?.innerHTML ?? '').toBe('<div data-narrate-before="and this comes before"></div>')
})
it('sets the data-narrate-as', () => {
render(<div data-narrate-as="title" />)
expect(root?.innerHTML ?? '').toBe('<div data-narrate-as="title"></div>')
})
it('sets the href and target', () => {
render(<a href="url" target="__blank"></a>)
expect(root?.innerHTML ?? '').toBe('<a href="url" target="__blank"></a>')
})
it('sets dangerouslySetInnerHTML', () => {
render(<div dangerouslySetInnerHTML={{ __html: '<span/>' }} />)
expect(root?.innerHTML ?? '').toBe('<div><span></span></div>')
})
describe('for svg', () => {
it('renders foreignObject and sets dimensions', () => {
render(
<svg>
<foreignObject x="50" y="100" width="150" height="200" />
</svg>,
)
expect(root?.innerHTML ?? '').toBe(
'<svg><foreignObject height="200" x="50" width="150" y="100"></foreignObject></svg>',
)
})
it('sets the d', () => {
render(<fast-path d="M0,0 L0,10 Z" />)
expect(root?.innerHTML ?? '').toBe('<path d="M0,0 L0,10 Z"></path>')
})
it('sets the fill', () => {
render(<fast-path fill="#ff0000" />)
expect(root?.innerHTML ?? '').toBe('<path fill="#ff0000"></path>')
})
it('sets the stroke', () => {
render(<fast-path stroke="#ff0000" />)
expect(root?.innerHTML ?? '').toBe('<path stroke="#ff0000"></path>')
})
it('sets the width', () => {
render(<fast-svg width="500" />)
expect(root?.innerHTML ?? '').toBe('<svg width="500"></svg>')
})
it('sets the height', () => {
render(<fast-svg height="500" />)
expect(root?.innerHTML ?? '').toBe('<svg height="500"></svg>')
})
it('sets the x', () => {
render(<fast-rect x="500" />)
expect(root?.innerHTML ?? '').toBe('<rect x="500"></rect>')
})
it('sets the y', () => {
render(<fast-rect y="500" />)
expect(root?.innerHTML ?? '').toBe('<rect y="500"></rect>')
})
it('sets the cx', () => {
render(<fast-circle cx="500" />)
expect(root?.innerHTML ?? '').toBe('<circle cx="500"></circle>')
})
it('sets the cy', () => {
render(<fast-circle cy="500" />)
expect(root?.innerHTML ?? '').toBe('<circle cy="500"></circle>')
})
it('sets the r', () => {
render(<fast-circle r="500" />)
expect(root?.innerHTML ?? '').toBe('<circle r="500"></circle>')
})
it('sets the rx', () => {
render(<fast-ellipse rx="500" />)
expect(root?.innerHTML ?? '').toBe('<ellipse rx="500"></ellipse>')
})
it('sets the ry', () => {
render(<fast-ellipse ry="500" />)
expect(root?.innerHTML ?? '').toBe('<ellipse ry="500"></ellipse>')
})
it('sets the x1', () => {
render(<fast-line x1="500" />)
expect(root?.innerHTML ?? '').toBe('<line x1="500"></line>')
})
it('sets the x2', () => {
render(<fast-line x2="500" />)
expect(root?.innerHTML ?? '').toBe('<line x2="500"></line>')
})
it('sets the y1', () => {
render(<fast-line y1="500" />)
expect(root?.innerHTML ?? '').toBe('<line y1="500"></line>')
})
it('sets the y2', () => {
render(<fast-line y2="500" />)
expect(root?.innerHTML ?? '').toBe('<line y2="500"></line>')
})
it('sets the strokeWidth', () => {
render(<fast-line strokeWidth="500" />)
expect(root?.innerHTML ?? '').toBe('<line stroke-width="500"></line>')
})
it('sets the viewBox', () => {
render(<fast-svg viewBox="0 0 100 100" />)
expect(root?.innerHTML ?? '').toBe('<svg viewBox="0 0 100 100"></svg>')
})
it('sets the xLinkHref', () => {
render(<fast-use xLinkHref="#test" />)
expect(root?.innerHTML ?? '').toBe('<use xlink:href="#test"></use>')
})
it('sets the fillOpacity', () => {
render(<fast-path fillOpacity="0.5" />)
expect(root?.innerHTML ?? '').toBe('<path fill-opacity="0.5"></path>')
})
it('sets the strokeOpacity', () => {
render(<fast-path strokeOpacity="0.5" />)
expect(root?.innerHTML ?? '').toBe('<path stroke-opacity="0.5"></path>')
})
it('sets the strokeLinecap', () => {
render(<fast-path strokeLinecap="round" />)
expect(root?.innerHTML ?? '').toBe('<path stroke-linecap="round"></path>')
})
it('sets the strokeLinejoin', () => {
render(<fast-path strokeLinejoin="round" />)
expect(root?.innerHTML ?? '').toBe('<path stroke-linejoin="round"></path>')
})
it('sets the points', () => {
render(<fast-polygon points="0,0 0,10 10,10" />)
expect(root?.innerHTML ?? '').toBe('<polygon points="0,0 0,10 10,10"></polygon>')
})
it('sets the offset', () => {
render(<fast-stop offset="0" />)
expect(root?.innerHTML ?? '').toBe('<stop offset="0"></stop>')
})
it('sets the stopColor', () => {
render(<fast-stop stopColor="#ff0000" />)
expect(root?.innerHTML ?? '').toBe('<stop stop-color="#ff0000"></stop>')
})
it('sets the stopOpacity', () => {
render(<fast-stop stopOpacity="0" />)
expect(root?.innerHTML ?? '').toBe('<stop stop-opacity="0"></stop>')
})
it('sets the fontFamily', () => {
render(<fast-svg-text fontFamily="verdana" />)
expect(root?.innerHTML ?? '').toBe('<text font-family="verdana"></text>')
})
it('sets the fontSize', () => {
render(<fast-svg-text fontSize="10" />)
expect(root?.innerHTML ?? '').toBe('<text font-size="10"></text>')
})
})
})
describe('with facets', () => {
it('renders text', () => {
const textFacet = createFacet({ initialValue: 'Hello World' })
render(<fast-text text={textFacet} />)
expect(root).toContainHTML('<div id="root">Hello World</div>')
textFacet.set('Updated World')
expect(root).toContainHTML('<div id="root">Updated World</div>')
})
it('sets the className', () => {
const classNameFacet = createFacet({ initialValue: 'testing' })
render(<fast-div className={classNameFacet}>Hello World</fast-div>)
expect(root).toContainHTML('<div id="root"><div class="testing">Hello World</div></div>')
classNameFacet.set('updated testing')
expect(root).toContainHTML('<div id="root"><div class="updated testing">Hello World</div></div>')
})
it('sets the id', () => {
const idFacet = createFacet({ initialValue: 'testing' })
render(<fast-div id={idFacet}>Hello World</fast-div>)
expect(root).toContainHTML('<div id="root"><div id="testing">Hello World</div></div>')
idFacet.set('updated testing')
expect(root).toContainHTML('<div id="root"><div id="updated testing">Hello World</div></div>')
})
it('sets the style', () => {
const backgroundFacet = createFacet({ initialValue: 'red' })
render(<fast-div style={{ background: backgroundFacet }}>Hello World</fast-div>)
expect(root).toContainHTML('<div id="root"><div style="background: red;">Hello World</div></div>')
backgroundFacet.set('yellow')
expect(root).toContainHTML('<div id="root"><div style="background: yellow;">Hello World</div></div>')
})
it('sets the src', () => {
const srcFacet = createFacet({ initialValue: 'bananafrita.png' })
render(<fast-img className="image" src={srcFacet} />)
const img = document.getElementsByClassName('image')[0] as HTMLImageElement | undefined
// jsdom is adding the http://localhost
expect(img && img.src).toEqual('http://localhost/bananafrita.png')
srcFacet.set('updated.png')
expect(img && img.src).toEqual('http://localhost/updated.png')
})
it('sets the value', () => {
const valueFacet = createFacet({ initialValue: 'foo' })
render(<fast-input value={valueFacet} />)
expect(root?.innerHTML ?? '').toBe('<input value="foo">')
valueFacet.set('updated')
expect(root?.innerHTML ?? '').toBe('<input value="updated">')
})
it('sets the type', () => {
const textFacet = createFacet<InputType>({ initialValue: 'text' })
const passwordFacet = createFacet<InputType>({ initialValue: 'password' })
const buttonFacet = createFacet<InputType>({ initialValue: 'button' })
const radioFacet = createFacet<InputType>({ initialValue: 'radio' })
const checkboxFacet = createFacet<InputType>({ initialValue: 'checkbox' })
render(
<>
<fast-input type={textFacet} />
<fast-input type={passwordFacet} />
<fast-input type={buttonFacet} />
<fast-input type={radioFacet} />
<fast-input type={checkboxFacet} />
</>,
)
expect(root?.innerHTML ?? '').toBe(
'<input type="text"><input type="password"><input type="button"><input type="radio"><input type="checkbox">',
)
textFacet.set('checkbox')
expect(root?.innerHTML ?? '').toBe(
'<input type="checkbox"><input type="password"><input type="button"><input type="radio"><input type="checkbox">',
)
})
it('sets disabled', () => {
const disabledFacet = createFacet({ initialValue: true })
render(<fast-input disabled={disabledFacet} />)
expect(root?.innerHTML ?? '').toBe('<input disabled="">')
disabledFacet.set(false)
expect(root?.innerHTML ?? '').toBe('<input>')
})
it('sets cohinline', () => {
const cohinlineFacet = createFacet({ initialValue: true })
render(<fast-p cohinline={cohinlineFacet} />)
expect(root?.innerHTML ?? '').toBe('<p cohinline=""></p>')
cohinlineFacet.set(false)
expect(root?.innerHTML ?? '').toBe('<p></p>')
})
it('sets maxlength', () => {
const maxLengthFacet = createFacet({ initialValue: 10 })
render(<fast-input type="text" maxLength={maxLengthFacet} />)
expect(root?.innerHTML ?? '').toBe('<input maxlength="10" type="text">')
maxLengthFacet.set(20)
expect(root?.innerHTML ?? '').toBe('<input maxlength="20" type="text">')
})
it('should only accept numerical characters if the input type is number', () => {
const valueFacet = createFacet({ initialValue: 'e' })
render(<fast-input type="number" value={valueFacet} />)
expect(root?.innerHTML ?? '').toBe('<input type="number">')
valueFacet.set('1')
expect(root?.innerHTML ?? '').toBe('<input type="number" value="1">')
valueFacet.set('1e')
expect(root?.innerHTML ?? '').toBe('<input type="number" value="1">')
valueFacet.set('12')
expect(root?.innerHTML ?? '').toBe('<input type="number" value="12">')
})
it('sets rows', () => {
const rowsFacet = createFacet({ initialValue: 10 })
render(<fast-textarea type="text" rows={rowsFacet} />)
expect(root?.innerHTML ?? '').toBe('<textarea rows="10" type="text"></textarea>')
rowsFacet.set(20)
expect(root?.innerHTML ?? '').toBe('<textarea rows="20" type="text"></textarea>')
})
it('sets the data-droppable', () => {
const dataDroppableFacet = createFacet({ initialValue: true })
render(<fast-div data-droppable={dataDroppableFacet} />)
expect(root?.innerHTML ?? '').toBe('<div data-droppable=""></div>')
dataDroppableFacet.set(false)
expect(root?.innerHTML ?? '').toBe('<div></div>')
})
it('sets the data narration properties', () => {
const dataNarrateFacet = createFacet<string | boolean>({ initialValue: true })
const dataNarrateBeforeFacet = createFacet({ initialValue: 'this comes before' })
const dataNarrateAsFacet = createFacet({ initialValue: 'title' })
const dataNarrateAfterFacet = createFacet({ initialValue: 'this comes after' })
render(
<fast-div
data-narrate={dataNarrateFacet}
data-narrate-before={dataNarrateBeforeFacet}
data-narrate-as={dataNarrateAsFacet}
data-narrate-after={dataNarrateAfterFacet}
/>,
)
const fastDivNode = root?.children[0]
expect(fastDivNode.getAttribute('data-narrate')).toBe('')
expect(fastDivNode.getAttribute('data-narrate-before')).toBe('this comes before')
expect(fastDivNode.getAttribute('data-narrate-as')).toBe('title')
expect(fastDivNode.getAttribute('data-narrate-after')).toBe('this comes after')
dataNarrateFacet.set('some content')
dataNarrateAsFacet.set('subtitle')
dataNarrateBeforeFacet.set('also called prefix')
dataNarrateAfterFacet.set('also called suffix')
expect(fastDivNode.getAttribute('data-narrate')).toBe('some content')
expect(fastDivNode.getAttribute('data-narrate-before')).toBe('also called prefix')
expect(fastDivNode.getAttribute('data-narrate-as')).toBe('subtitle')
expect(fastDivNode.getAttribute('data-narrate-after')).toBe('also called suffix')
dataNarrateFacet.set(false)
expect(fastDivNode.getAttribute('data-narrate')).toBe(null)
})
it('sets the data-testid', () => {
const dataFacet = createFacet({ initialValue: 'test-id' })
render(<fast-div data-testid={dataFacet} />)
expect(root?.innerHTML ?? '').toBe('<div data-testid="test-id"></div>')
dataFacet.set('updated-test-id')
expect(root?.innerHTML ?? '').toBe('<div data-testid="updated-test-id"></div>')
})
it('sets the data-x-ray', () => {
const dataFacet = createFacet({ initialValue: true })
render(<fast-div data-x-ray={dataFacet} />)
expect(root?.innerHTML ?? '').toBe('<div data-x-ray=""></div>')
dataFacet.set(false)
expect(root?.innerHTML ?? '').toBe('<div></div>')
})
it('sets the href and target', () => {
const hrefFacet = createFacet({ initialValue: 'url' })
const targetFacet = createFacet({ initialValue: '_blank' })
render(<fast-a href={hrefFacet} target={targetFacet}></fast-a>)
expect(root?.innerHTML ?? '').toBe('<a href="url" target="_blank"></a>')
hrefFacet.set('updated')
targetFacet.set('_top')
expect(root?.innerHTML ?? '').toBe('<a href="updated" target="_top"></a>')
})
it('sets the autoPlay', () => {
const dataFacet = createFacet({ initialValue: true })
render(<fast-div autoPlay={dataFacet} />)
expect(root?.innerHTML ?? '').toBe('<div autoplay=""></div>')
dataFacet.set(false)
expect(root?.innerHTML ?? '').toBe('<div></div>')
})
it('sets the loop', () => {
const dataFacet = createFacet({ initialValue: true })
render(<fast-div loop={dataFacet} />)
expect(root?.innerHTML ?? '').toBe('<div loop=""></div>')
dataFacet.set(false)
expect(root?.innerHTML ?? '').toBe('<div></div>')
})
describe('for svg', () => {
it('renders foreignObject and sets dimensions', () => {
const xFacet = createFacet({ initialValue: '50' })
const yFacet = createFacet({ initialValue: '100' })
const widthFacet = createFacet({ initialValue: '150' })
const heightFacet = createFacet({ initialValue: '200' })
render(
<svg>
<fast-foreignObject x={xFacet} y={yFacet} width={widthFacet} height={heightFacet} />
</svg>,
)
expect(root?.innerHTML ?? '').toBe(
'<svg><foreignObject height="200" x="50" width="150" y="100"></foreignObject></svg>',
)
})
it('sets the d', () => {
const dFacet = createFacet({ initialValue: 'M0,0 L0,10 Z' })
render(<fast-path d={dFacet} />)
expect(root?.innerHTML ?? '').toBe('<path d="M0,0 L0,10 Z"></path>')
dFacet.set('M0,10 L0,10 Z')
expect(root?.innerHTML ?? '').toBe('<path d="M0,10 L0,10 Z"></path>')
})
it('sets the fill', () => {
const fillFacet = createFacet({ initialValue: '#ff0000' })
render(<fast-path fill={fillFacet} />)
expect(root?.innerHTML ?? '').toBe('<path fill="#ff0000"></path>')
fillFacet.set('#00ff00')
expect(root?.innerHTML ?? '').toBe('<path fill="#00ff00"></path>')
})
it('sets the stroke', () => {
const strokeFacet = createFacet({ initialValue: '#ff0000' })
render(<fast-path stroke={strokeFacet} />)
expect(root?.innerHTML ?? '').toBe('<path stroke="#ff0000"></path>')
strokeFacet.set('#00ff00')
expect(root?.innerHTML ?? '').toBe('<path stroke="#00ff00"></path>')
})
it('sets the width', () => {
const widthFacet = createFacet({ initialValue: '500' })
render(<fast-svg width={widthFacet} />)
expect(root?.innerHTML ?? '').toBe('<svg width="500"></svg>')
widthFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<svg width="600"></svg>')
})
it('sets the height', () => {
const heightFacet = createFacet({ initialValue: '500' })
render(<fast-svg height={heightFacet} />)
expect(root?.innerHTML ?? '').toBe('<svg height="500"></svg>')
heightFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<svg height="600"></svg>')
})
it('sets the x', () => {
const xFacet = createFacet({ initialValue: '500' })
render(<fast-rect x={xFacet} />)
expect(root?.innerHTML ?? '').toBe('<rect x="500"></rect>')
xFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<rect x="600"></rect>')
})
it('sets the y', () => {
const yFacet = createFacet({ initialValue: '500' })
render(<fast-rect y={yFacet} />)
expect(root?.innerHTML ?? '').toBe('<rect y="500"></rect>')
yFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<rect y="600"></rect>')
})
it('sets the cx', () => {
const cxFacet = createFacet({ initialValue: '500' })
render(<fast-circle cx={cxFacet} />)
expect(root?.innerHTML ?? '').toBe('<circle cx="500"></circle>')
cxFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<circle cx="600"></circle>')
})
it('sets the cy', () => {
const cyFacet = createFacet({ initialValue: '500' })
render(<fast-circle cy={cyFacet} />)
expect(root?.innerHTML ?? '').toBe('<circle cy="500"></circle>')
cyFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<circle cy="600"></circle>')
})
it('sets the r', () => {
const rFacet = createFacet({ initialValue: '500' })
render(<fast-circle r={rFacet} />)
expect(root?.innerHTML ?? '').toBe('<circle r="500"></circle>')
rFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<circle r="600"></circle>')
})
it('sets the rx', () => {
const rxFacet = createFacet({ initialValue: '500' })
render(<fast-ellipse rx={rxFacet} />)
expect(root?.innerHTML ?? '').toBe('<ellipse rx="500"></ellipse>')
rxFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<ellipse rx="600"></ellipse>')
})
it('sets the ry', () => {
const ryFacet = createFacet({ initialValue: '500' })
render(<fast-ellipse ry={ryFacet} />)
expect(root?.innerHTML ?? '').toBe('<ellipse ry="500"></ellipse>')
ryFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<ellipse ry="600"></ellipse>')
})
it('sets the x1', () => {
const x1Facet = createFacet({ initialValue: '500' })
render(<fast-line x1={x1Facet} />)
expect(root?.innerHTML ?? '').toBe('<line x1="500"></line>')
x1Facet.set('600')
expect(root?.innerHTML ?? '').toBe('<line x1="600"></line>')
})
it('sets the x2', () => {
const x2Facet = createFacet({ initialValue: '500' })
render(<fast-line x2={x2Facet} />)
expect(root?.innerHTML ?? '').toBe('<line x2="500"></line>')
x2Facet.set('600')
expect(root?.innerHTML ?? '').toBe('<line x2="600"></line>')
})
it('sets the y1', () => {
const y1Facet = createFacet({ initialValue: '500' })
render(<fast-line y1={y1Facet} />)
expect(root?.innerHTML ?? '').toBe('<line y1="500"></line>')
y1Facet.set('600')
expect(root?.innerHTML ?? '').toBe('<line y1="600"></line>')
})
it('sets the y2', () => {
const y2Facet = createFacet({ initialValue: '500' })
render(<fast-line y2={y2Facet} />)
expect(root?.innerHTML ?? '').toBe('<line y2="500"></line>')
y2Facet.set('600')
expect(root?.innerHTML ?? '').toBe('<line y2="600"></line>')
})
it('sets the strokeWidth', () => {
const strokeWidthFacet = createFacet({ initialValue: '500' })
render(<fast-line strokeWidth={strokeWidthFacet} />)
expect(root?.innerHTML ?? '').toBe('<line stroke-width="500"></line>')
strokeWidthFacet.set('600')
expect(root?.innerHTML ?? '').toBe('<line stroke-width="600"></line>')
})
it('sets the viewBox', () => {
const viewBoxFacet = createFacet({ initialValue: '0 0 10 10' })
render(<fast-svg viewBox={viewBoxFacet} />)
expect(root?.innerHTML ?? '').toBe('<svg viewBox="0 0 10 10"></svg>')
viewBoxFacet.set('0 0 20 20')
expect(root?.innerHTML ?? '').toBe('<svg viewBox="0 0 20 20"></svg>')
})
it('sets the xLinkHref', () => {
const xLinkHrefFacet = createFacet({ initialValue: '#test1' })
render(<fast-use xLinkHref={xLinkHrefFacet} />)
expect(root?.innerHTML ?? '').toBe('<use xlink:href="#test1"></use>')
xLinkHrefFacet.set('#test2')
expect(root?.innerHTML ?? '').toBe('<use xlink:href="#test2"></use>')
})
it('sets the fillOpacity', () => {
const fillOpacityFacet = createFacet({ initialValue: '0.5' })
render(<fast-rect fillOpacity={fillOpacityFacet} />)
expect(root?.innerHTML ?? '').toBe('<rect fill-opacity="0.5"></rect>')
fillOpacityFacet.set('0.6')
expect(root?.innerHTML ?? '').toBe('<rect fill-opacity="0.6"></rect>')
})
it('sets the strokeOpacity', () => {
const strokeOpacityFacet = createFacet({ initialValue: '0.5' })
render(<fast-rect strokeOpacity={strokeOpacityFacet} />)
expect(root?.innerHTML ?? '').toBe('<rect stroke-opacity="0.5"></rect>')
strokeOpacityFacet.set('0.6')
expect(root?.innerHTML ?? '').toBe('<rect stroke-opacity="0.6"></rect>')
})
it('sets the strokeLinecap', () => {
const strokeLinecapFacet = createFacet({ initialValue: 'round' })
render(<fast-rect strokeLinecap={strokeLinecapFacet} />)
expect(root?.innerHTML ?? '').toBe('<rect stroke-linecap="round"></rect>')
strokeLinecapFacet.set('square')
expect(root?.innerHTML ?? '').toBe('<rect stroke-linecap="square"></rect>')
})
it('sets the strokeLinejoin', () => {
const strokeLinejoinFacet = createFacet({ initialValue: 'round' })
render(<fast-rect strokeLinejoin={strokeLinejoinFacet} />)
expect(root?.innerHTML ?? '').toBe('<rect stroke-linejoin="round"></rect>')
strokeLinejoinFacet.set('square')
expect(root?.innerHTML ?? '').toBe('<rect stroke-linejoin="square"></rect>')
})
it('sets the points', () => {
const pointsFacet = createFacet({ initialValue: '0 0 10 10' })
render(<fast-polygon points={pointsFacet} />)
expect(root?.innerHTML ?? '').toBe('<polygon points="0 0 10 10"></polygon>')
pointsFacet.set('0 0 20 20')
expect(root?.innerHTML ?? '').toBe('<polygon points="0 0 20 20"></polygon>')
})
it('sets the offset', () => {
const offsetFacet = createFacet({ initialValue: '0 0' })
render(<fast-stop offset={offsetFacet} />)
expect(root?.innerHTML ?? '').toBe('<stop offset="0 0"></stop>')
offsetFacet.set('1 1')
expect(root?.innerHTML ?? '').toBe('<stop offset="1 1"></stop>')
})
it('sets the stopColor', () => {
const stopColorFacet = createFacet({ initialValue: '#000' })
render(<fast-stop stopColor={stopColorFacet} />)
expect(root?.innerHTML ?? '').toBe('<stop stop-color="#000"></stop>')
stopColorFacet.set('#fff')
expect(root?.innerHTML ?? '').toBe('<stop stop-color="#fff"></stop>')
})
it('sets the stopOpacity', () => {
const stopOpacityFacet = createFacet({ initialValue: '0' })
render(<fast-stop stopOpacity={stopOpacityFacet} />)
expect(root?.innerHTML ?? '').toBe('<stop stop-opacity="0"></stop>')
stopOpacityFacet.set('1')
expect(root?.innerHTML ?? '').toBe('<stop stop-opacity="1"></stop>')
})
it('sets the fontFamily', () => {
const fontFamilyFacet = createFacet({ initialValue: 'Arial' })
render(<fast-svg-text fontFamily={fontFamilyFacet} />)
expect(root?.innerHTML ?? '').toBe('<text font-family="Arial"></text>')
fontFamilyFacet.set('Helvetica')
expect(root?.innerHTML ?? '').toBe('<text font-family="Helvetica"></text>')
})
it('sets the fontSize', () => {
const fontSizeFacet = createFacet({ initialValue: '10' })
render(<fast-svg-text fontSize={fontSizeFacet} />)
expect(root?.innerHTML ?? '').toBe('<text font-size="10"></text>')
fontSizeFacet.set('20')
expect(root?.innerHTML ?? '').toBe('<text font-size="20"></text>')
})
})
})
describe('setting listeners', () => {
let onClick: jest.Mock
let onFocus: jest.Mock
let onBlur: jest.Mock
let onMouseDown: jest.Mock
let onMouseMove: jest.Mock
let onMouseUp: jest.Mock
let onTouchStart: jest.Mock
let onTouchMove: jest.Mock
let onTouchEnd: jest.Mock
let onMouseEnter: jest.Mock
let onMouseLeave: jest.Mock
let onKeyPress: jest.Mock
let onKeyDown: jest.Mock
let onKeyUp: jest.Mock
let div: Element
beforeEach(() => {
onClick = jest.fn()
onFocus = jest.fn()
onBlur = jest.fn()
onMouseDown = jest.fn()
onMouseMove = jest.fn()
onMouseUp = jest.fn()
onTouchStart = jest.fn()
onTouchMove = jest.fn()
onTouchEnd = jest.fn()
onMouseEnter = jest.fn()
onMouseLeave = jest.fn()
onKeyPress = jest.fn()
onKeyDown = jest.fn()
onKeyUp = jest.fn()
render(
<div
className="testing"
onClick={onClick}
onFocus={onFocus}
onBlur={onBlur}
onMouseDown={onMouseDown}
onMouseMove={onMouseMove}
onMouseUp={onMouseUp}
onTouchStart={onTouchStart}
onTouchMove={onTouchMove}
onTouchEnd={onTouchEnd}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onKeyPress={onKeyPress}
onKeyDown={onKeyDown}
onKeyUp={onKeyUp}
>
Hello World
</div>,
)
div = document.getElementsByClassName('testing')[0]
if (div === undefined) throw new Error('Root element not found')
})
it('supports onClick', () => {
div.dispatchEvent(new Event('click'))
expect(onClick).toHaveBeenCalled()
})
it('supports onFocus', () => {
div.dispatchEvent(new Event('focus'))
expect(onFocus).toHaveBeenCalled()
})
it('supports onBlur', () => {
div.dispatchEvent(new Event('blur'))
expect(onBlur).toHaveBeenCalled()
})
it('supports onMouseDown', () => {
div.dispatchEvent(new Event('mousedown'))
expect(onMouseDown).toHaveBeenCalled()
})
it('supports onMouseMove', () => {
div.dispatchEvent(new Event('mousemove'))
expect(onMouseMove).toHaveBeenCalled()
})
it('supports onMouseUp', () => {
div.dispatchEvent(new Event('mouseup'))
expect(onMouseUp).toHaveBeenCalled()
})
it('supports onTouchStart', () => {
div.dispatchEvent(new Event('touchstart'))
expect(onTouchStart).toHaveBeenCalled()
})
it('supports onTouchMove', () => {
div.dispatchEvent(new Event('touchmove'))
expect(onTouchMove).toHaveBeenCalled()
})
it('supports onTouchEnd', () => {
div.dispatchEvent(new Event('touchend'))
expect(onTouchEnd).toHaveBeenCalled()
})
it('supports onMouseEnter', () => {
div.dispatchEvent(new Event('mouseenter'))
expect(onMouseEnter).toHaveBeenCalled()
})
it('supports onMouseLeave', () => {
div.dispatchEvent(new Event('mouseleave'))
expect(onMouseLeave).toHaveBeenCalled()
})
it('supports onKeyPress', () => {
div.dispatchEvent(new Event('keypress'))
expect(onKeyPress).toHaveBeenCalled()
})
it('supports onKeyDown', () => {
div.dispatchEvent(new Event('keydown'))
expect(onKeyDown).toHaveBeenCalled()
})
it('supports onKeyUp', () => {
div.dispatchEvent(new Event('keyup'))
expect(onKeyUp).toHaveBeenCalled()
})
})
})
describe('update', () => {
it('updates text', () => {
function TestComponent() {
const [hello, setHello] = useState(false)
useEffect(() => {
setTimeout(() => {
setHello(true)
}, 1000)
}, [])
return <div>{hello ? 'Hello' : 'Goodbye'} World</div>
}
render(<TestComponent />)
expect(root).toContainHTML('<div id="root"><div>Goodbye World</div></div>')
jest.runAllTimers()
expect(root).toContainHTML('<div id="root"><div>Hello World</div></div>')
})
it('updates className', () => {
function TestComponent() {
const [hello, setHello] = useState(false)
useEffect(() => {
setTimeout(() => {
setHello(true)
}, 1000)
}, [])