|
| 1 | +import { |
| 2 | + createComponent, |
| 3 | + getCurrentInstance, |
| 4 | + nextTick, |
| 5 | + ref, |
| 6 | + setText, |
| 7 | + template, |
| 8 | + watchEffect, |
| 9 | +} from '../src' |
| 10 | +import { setCurrentInstance } from '../src/component' |
| 11 | +import { makeRender } from './_utils' |
| 12 | + |
| 13 | +const define = makeRender<any>() |
| 14 | + |
| 15 | +describe('attribute fallthrough', () => { |
| 16 | + it('should allow attrs to fallthrough', async () => { |
| 17 | + const t0 = template('<div>') |
| 18 | + const { component: Child } = define({ |
| 19 | + props: ['foo'], |
| 20 | + render() { |
| 21 | + const instance = getCurrentInstance()! |
| 22 | + const n0 = t0() |
| 23 | + watchEffect(() => setText(n0, instance.props.foo)) |
| 24 | + return n0 |
| 25 | + }, |
| 26 | + }) |
| 27 | + |
| 28 | + const foo = ref(1) |
| 29 | + const id = ref('a') |
| 30 | + const { instance, host } = define({ |
| 31 | + setup() { |
| 32 | + return { foo, id } |
| 33 | + }, |
| 34 | + render(_ctx: Record<string, any>) { |
| 35 | + return createComponent( |
| 36 | + Child, |
| 37 | + [ |
| 38 | + { |
| 39 | + foo: () => _ctx.foo, |
| 40 | + id: () => _ctx.id, |
| 41 | + }, |
| 42 | + ], |
| 43 | + true, |
| 44 | + ) |
| 45 | + }, |
| 46 | + }).render() |
| 47 | + const reset = setCurrentInstance(instance) |
| 48 | + expect(host.innerHTML).toBe('<div id="a">1</div>') |
| 49 | + |
| 50 | + foo.value++ |
| 51 | + await nextTick() |
| 52 | + expect(host.innerHTML).toBe('<div id="a">2</div>') |
| 53 | + |
| 54 | + id.value = 'b' |
| 55 | + await nextTick() |
| 56 | + expect(host.innerHTML).toBe('<div id="b">2</div>') |
| 57 | + reset() |
| 58 | + }) |
| 59 | + |
| 60 | + it('should not fallthrough if explicitly pass inheritAttrs: false', async () => { |
| 61 | + const t0 = template('<div>') |
| 62 | + const { component: Child } = define({ |
| 63 | + props: ['foo'], |
| 64 | + inheritAttrs: false, |
| 65 | + render() { |
| 66 | + const instance = getCurrentInstance()! |
| 67 | + const n0 = t0() |
| 68 | + watchEffect(() => setText(n0, instance.props.foo)) |
| 69 | + return n0 |
| 70 | + }, |
| 71 | + }) |
| 72 | + |
| 73 | + const foo = ref(1) |
| 74 | + const id = ref('a') |
| 75 | + const { instance, host } = define({ |
| 76 | + setup() { |
| 77 | + return { foo, id } |
| 78 | + }, |
| 79 | + render(_ctx: Record<string, any>) { |
| 80 | + return createComponent( |
| 81 | + Child, |
| 82 | + [ |
| 83 | + { |
| 84 | + foo: () => _ctx.foo, |
| 85 | + id: () => _ctx.id, |
| 86 | + }, |
| 87 | + ], |
| 88 | + true, |
| 89 | + ) |
| 90 | + }, |
| 91 | + }).render() |
| 92 | + const reset = setCurrentInstance(instance) |
| 93 | + expect(host.innerHTML).toBe('<div>1</div>') |
| 94 | + |
| 95 | + foo.value++ |
| 96 | + await nextTick() |
| 97 | + expect(host.innerHTML).toBe('<div>2</div>') |
| 98 | + |
| 99 | + id.value = 'b' |
| 100 | + await nextTick() |
| 101 | + expect(host.innerHTML).toBe('<div>2</div>') |
| 102 | + reset() |
| 103 | + }) |
| 104 | + |
| 105 | + it('should pass through attrs in nested single root components', async () => { |
| 106 | + const t0 = template('<div>') |
| 107 | + const { component: Grandson } = define({ |
| 108 | + props: ['custom-attr'], |
| 109 | + render() { |
| 110 | + const instance = getCurrentInstance()! |
| 111 | + const n0 = t0() |
| 112 | + watchEffect(() => setText(n0, instance.attrs.foo)) |
| 113 | + return n0 |
| 114 | + }, |
| 115 | + }) |
| 116 | + |
| 117 | + const { component: Child } = define({ |
| 118 | + render() { |
| 119 | + const n0 = createComponent( |
| 120 | + Grandson, |
| 121 | + [ |
| 122 | + { |
| 123 | + 'custom-attr': () => 'custom-attr', |
| 124 | + }, |
| 125 | + ], |
| 126 | + true, |
| 127 | + ) |
| 128 | + return n0 |
| 129 | + }, |
| 130 | + }) |
| 131 | + |
| 132 | + const foo = ref(1) |
| 133 | + const id = ref('a') |
| 134 | + const { instance, host } = define({ |
| 135 | + setup() { |
| 136 | + return { foo, id } |
| 137 | + }, |
| 138 | + render(_ctx: Record<string, any>) { |
| 139 | + return createComponent( |
| 140 | + Child, |
| 141 | + [ |
| 142 | + { |
| 143 | + foo: () => _ctx.foo, |
| 144 | + id: () => _ctx.id, |
| 145 | + }, |
| 146 | + ], |
| 147 | + true, |
| 148 | + ) |
| 149 | + }, |
| 150 | + }).render() |
| 151 | + const reset = setCurrentInstance(instance) |
| 152 | + expect(host.innerHTML).toBe('<div foo="1" id="a">1</div>') |
| 153 | + |
| 154 | + foo.value++ |
| 155 | + await nextTick() |
| 156 | + expect(host.innerHTML).toBe('<div foo="2" id="a">2</div>') |
| 157 | + |
| 158 | + id.value = 'b' |
| 159 | + await nextTick() |
| 160 | + expect(host.innerHTML).toBe('<div foo="2" id="b">2</div>') |
| 161 | + reset() |
| 162 | + }) |
| 163 | +}) |
0 commit comments