| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import {
- createComponent,
- getCurrentInstance,
- nextTick,
- ref,
- setText,
- template,
- watchEffect,
- } from '../src'
- import { setCurrentInstance } from '../src/component'
- import { makeRender } from './_utils'
- const define = makeRender<any>()
- describe('attribute fallthrough', () => {
- it('should allow attrs to fallthrough', async () => {
- const t0 = template('<div>')
- const { component: Child } = define({
- props: ['foo'],
- render() {
- const instance = getCurrentInstance()!
- const n0 = t0()
- watchEffect(() => setText(n0, instance.props.foo))
- return n0
- },
- })
- const foo = ref(1)
- const id = ref('a')
- const { instance, host } = define({
- setup() {
- return { foo, id }
- },
- render(_ctx: Record<string, any>) {
- return createComponent(
- Child,
- [
- {
- foo: () => _ctx.foo,
- id: () => _ctx.id,
- },
- ],
- null,
- null,
- true,
- )
- },
- }).render()
- const reset = setCurrentInstance(instance)
- expect(host.innerHTML).toBe('<div id="a">1</div>')
- foo.value++
- await nextTick()
- expect(host.innerHTML).toBe('<div id="a">2</div>')
- id.value = 'b'
- await nextTick()
- expect(host.innerHTML).toBe('<div id="b">2</div>')
- reset()
- })
- it('should not fallthrough if explicitly pass inheritAttrs: false', async () => {
- const t0 = template('<div>')
- const { component: Child } = define({
- props: ['foo'],
- inheritAttrs: false,
- render() {
- const instance = getCurrentInstance()!
- const n0 = t0()
- watchEffect(() => setText(n0, instance.props.foo))
- return n0
- },
- })
- const foo = ref(1)
- const id = ref('a')
- const { instance, host } = define({
- setup() {
- return { foo, id }
- },
- render(_ctx: Record<string, any>) {
- return createComponent(
- Child,
- [
- {
- foo: () => _ctx.foo,
- id: () => _ctx.id,
- },
- ],
- null,
- null,
- true,
- )
- },
- }).render()
- const reset = setCurrentInstance(instance)
- expect(host.innerHTML).toBe('<div>1</div>')
- foo.value++
- await nextTick()
- expect(host.innerHTML).toBe('<div>2</div>')
- id.value = 'b'
- await nextTick()
- expect(host.innerHTML).toBe('<div>2</div>')
- reset()
- })
- it('should pass through attrs in nested single root components', async () => {
- const t0 = template('<div>')
- const { component: Grandson } = define({
- props: ['custom-attr'],
- render() {
- const instance = getCurrentInstance()!
- const n0 = t0()
- watchEffect(() => setText(n0, instance.attrs.foo))
- return n0
- },
- })
- const { component: Child } = define({
- render() {
- const n0 = createComponent(
- Grandson,
- [
- {
- 'custom-attr': () => 'custom-attr',
- },
- ],
- null,
- null,
- true,
- )
- return n0
- },
- })
- const foo = ref(1)
- const id = ref('a')
- const { instance, host } = define({
- setup() {
- return { foo, id }
- },
- render(_ctx: Record<string, any>) {
- return createComponent(
- Child,
- [
- {
- foo: () => _ctx.foo,
- id: () => _ctx.id,
- },
- ],
- null,
- null,
- true,
- )
- },
- }).render()
- const reset = setCurrentInstance(instance)
- expect(host.innerHTML).toBe('<div foo="1" id="a">1</div>')
- foo.value++
- await nextTick()
- expect(host.innerHTML).toBe('<div foo="2" id="a">2</div>')
- id.value = 'b'
- await nextTick()
- expect(host.innerHTML).toBe('<div foo="2" id="b">2</div>')
- reset()
- })
- })
|