1
- import { NOOP } from '@vue/shared'
1
+ import { NOOP , toDisplayString } from '@vue/shared'
2
2
import {
3
3
setDynamicProp as _setDynamicProp ,
4
4
setAttr ,
5
+ setBlockHtml ,
6
+ setBlockText ,
5
7
setClass ,
6
8
setDynamicProps ,
7
9
setElementText ,
@@ -11,8 +13,15 @@ import {
11
13
setValue ,
12
14
} from '../../src/dom/prop'
13
15
import { setStyle } from '../../src/dom/prop'
14
- import { VaporComponentInstance } from '../../src/component'
16
+ import { VaporComponentInstance , createComponent } from '../../src/component'
15
17
import { ref , setCurrentInstance } from '@vue/runtime-dom'
18
+ import { makeRender } from '../_utils'
19
+ import {
20
+ createDynamicComponent ,
21
+ defineVaporComponent ,
22
+ renderEffect ,
23
+ template ,
24
+ } from '../../src'
16
25
17
26
let removeComponentInstance = NOOP
18
27
beforeEach ( ( ) => {
@@ -24,6 +33,8 @@ afterEach(() => {
24
33
removeComponentInstance ( )
25
34
} )
26
35
36
+ const define = makeRender ( )
37
+
27
38
describe ( 'patchProp' , ( ) => {
28
39
describe ( 'setClass' , ( ) => {
29
40
test ( 'should set class' , ( ) => {
@@ -444,4 +455,150 @@ describe('patchProp', () => {
444
455
expect ( el . innerHTML ) . toBe ( '<p>bar</p>' )
445
456
} )
446
457
} )
458
+
459
+ describe ( 'setBlockText' , ( ) => {
460
+ test ( 'with dynamic component' , async ( ) => {
461
+ const Comp = defineVaporComponent ( {
462
+ setup ( ) {
463
+ return template ( '<div>child</div>' , true ) ( )
464
+ } ,
465
+ } )
466
+ const value = ref ( 'foo' )
467
+ const { html } = define ( {
468
+ setup ( ) {
469
+ const n1 = createDynamicComponent ( ( ) => Comp , null , null , true )
470
+ renderEffect ( ( ) => setBlockText ( n1 , toDisplayString ( value ) ) )
471
+ return n1
472
+ } ,
473
+ } ) . render ( )
474
+
475
+ expect ( html ( ) ) . toBe ( '<div>foo</div><!--dynamic-component-->' )
476
+ } )
477
+
478
+ test ( 'with dynamic component with fallback' , async ( ) => {
479
+ const value = ref ( 'foo' )
480
+ const { html } = define ( {
481
+ setup ( ) {
482
+ const n1 = createDynamicComponent ( ( ) => 'button' , null , null , true )
483
+ renderEffect ( ( ) => setBlockText ( n1 , toDisplayString ( value ) ) )
484
+ return n1
485
+ } ,
486
+ } ) . render ( )
487
+
488
+ expect ( html ( ) ) . toBe ( '<button>foo</button><!--dynamic-component-->' )
489
+ } )
490
+
491
+ test ( 'with component' , async ( ) => {
492
+ const Comp = defineVaporComponent ( {
493
+ setup ( ) {
494
+ return template ( '<div>child</div>' , true ) ( )
495
+ } ,
496
+ } )
497
+ const value = ref ( 'foo' )
498
+ const { html } = define ( {
499
+ setup ( ) {
500
+ const n1 = createComponent ( Comp , null , null , true )
501
+ renderEffect ( ( ) => setBlockText ( n1 , toDisplayString ( value ) ) )
502
+ return n1
503
+ } ,
504
+ } ) . render ( )
505
+
506
+ expect ( html ( ) ) . toBe ( '<div>foo</div>' )
507
+ } )
508
+
509
+ test ( 'with multiple roots component' , async ( ) => {
510
+ const Comp = defineVaporComponent ( {
511
+ setup ( ) {
512
+ return [
513
+ template ( '<div>child</div>' ) ( ) ,
514
+ template ( '<div>child</div>' ) ( ) ,
515
+ ]
516
+ } ,
517
+ } )
518
+ const value = ref ( 'foo' )
519
+ const { html } = define ( {
520
+ setup ( ) {
521
+ const n1 = createComponent ( Comp , null , null , true )
522
+ renderEffect ( ( ) => setBlockText ( n1 , toDisplayString ( value ) ) )
523
+ return n1
524
+ } ,
525
+ } ) . render ( )
526
+
527
+ expect ( html ( ) ) . toBe ( '<div>child</div><div>child</div>' )
528
+ expect ( 'Extraneous non-props attributes (textContent)' ) . toHaveBeenWarned ( )
529
+ } )
530
+ } )
531
+
532
+ describe ( 'setBlockHtml' , ( ) => {
533
+ test ( 'with dynamic component' , async ( ) => {
534
+ const Comp = defineVaporComponent ( {
535
+ setup ( ) {
536
+ return template ( '<div>child</div>' , true ) ( )
537
+ } ,
538
+ } )
539
+ const value = ref ( '<p>foo</p>' )
540
+ const { html } = define ( {
541
+ setup ( ) {
542
+ const n1 = createDynamicComponent ( ( ) => Comp , null , null , true )
543
+ renderEffect ( ( ) => setBlockHtml ( n1 , value . value ) )
544
+ return n1
545
+ } ,
546
+ } ) . render ( )
547
+
548
+ expect ( html ( ) ) . toBe ( '<div><p>foo</p></div><!--dynamic-component-->' )
549
+ } )
550
+
551
+ test ( 'with dynamic component with fallback' , async ( ) => {
552
+ const value = ref ( '<p>foo</p>' )
553
+ const { html } = define ( {
554
+ setup ( ) {
555
+ const n1 = createDynamicComponent ( ( ) => 'button' , null , null , true )
556
+ renderEffect ( ( ) => setBlockHtml ( n1 , value . value ) )
557
+ return n1
558
+ } ,
559
+ } ) . render ( )
560
+
561
+ expect ( html ( ) ) . toBe ( '<button><p>foo</p></button><!--dynamic-component-->' )
562
+ } )
563
+
564
+ test ( 'with component' , async ( ) => {
565
+ const Comp = defineVaporComponent ( {
566
+ setup ( ) {
567
+ return template ( '<div>child</div>' , true ) ( )
568
+ } ,
569
+ } )
570
+ const value = ref ( '<p>foo</p>' )
571
+ const { html } = define ( {
572
+ setup ( ) {
573
+ const n1 = createComponent ( Comp , null , null , true )
574
+ renderEffect ( ( ) => setBlockHtml ( n1 , value . value ) )
575
+ return n1
576
+ } ,
577
+ } ) . render ( )
578
+
579
+ expect ( html ( ) ) . toBe ( '<div><p>foo</p></div>' )
580
+ } )
581
+
582
+ test ( 'with multiple roots component' , async ( ) => {
583
+ const Comp = defineVaporComponent ( {
584
+ setup ( ) {
585
+ return [
586
+ template ( '<div>child</div>' ) ( ) ,
587
+ template ( '<div>child</div>' ) ( ) ,
588
+ ]
589
+ } ,
590
+ } )
591
+ const value = ref ( '<p>foo</p>' )
592
+ const { html } = define ( {
593
+ setup ( ) {
594
+ const n1 = createComponent ( Comp , null , null , true )
595
+ renderEffect ( ( ) => setBlockHtml ( n1 , value . value ) )
596
+ return n1
597
+ } ,
598
+ } ) . render ( )
599
+
600
+ expect ( html ( ) ) . toBe ( '<div>child</div><div>child</div>' )
601
+ expect ( 'Extraneous non-props attributes (innerHTML)' ) . toHaveBeenWarned ( )
602
+ } )
603
+ } )
447
604
} )
0 commit comments