Skip to content

Commit 5b917ab

Browse files
committed
test: add tests
1 parent da45770 commit 5b917ab

File tree

1 file changed

+159
-2
lines changed

1 file changed

+159
-2
lines changed

packages/runtime-vapor/__tests__/dom/prop.spec.ts

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { NOOP } from '@vue/shared'
1+
import { NOOP, toDisplayString } from '@vue/shared'
22
import {
33
setDynamicProp as _setDynamicProp,
44
setAttr,
5+
setBlockHtml,
6+
setBlockText,
57
setClass,
68
setDynamicProps,
79
setElementText,
@@ -11,8 +13,15 @@ import {
1113
setValue,
1214
} from '../../src/dom/prop'
1315
import { setStyle } from '../../src/dom/prop'
14-
import { VaporComponentInstance } from '../../src/component'
16+
import { VaporComponentInstance, createComponent } from '../../src/component'
1517
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'
1625

1726
let removeComponentInstance = NOOP
1827
beforeEach(() => {
@@ -24,6 +33,8 @@ afterEach(() => {
2433
removeComponentInstance()
2534
})
2635

36+
const define = makeRender()
37+
2738
describe('patchProp', () => {
2839
describe('setClass', () => {
2940
test('should set class', () => {
@@ -444,4 +455,150 @@ describe('patchProp', () => {
444455
expect(el.innerHTML).toBe('<p>bar</p>')
445456
})
446457
})
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+
})
447604
})

0 commit comments

Comments
 (0)