|
1 | 1 | import {
|
2 | 2 | type Ref,
|
3 | 3 | type VueElement,
|
| 4 | + createApp, |
4 | 5 | defineAsyncComponent,
|
5 | 6 | defineComponent,
|
6 | 7 | defineCustomElement,
|
@@ -60,6 +61,54 @@ describe('defineCustomElement', () => {
|
60 | 61 | expect(e.shadowRoot!.innerHTML).toBe('')
|
61 | 62 | })
|
62 | 63 |
|
| 64 | + // #10610 |
| 65 | + test('When elements move, avoid prematurely disconnecting MutationObserver', async () => { |
| 66 | + const CustomInput = defineCustomElement({ |
| 67 | + props: ['value'], |
| 68 | + emits: ['update'], |
| 69 | + setup(props, { emit }) { |
| 70 | + return () => |
| 71 | + h('input', { |
| 72 | + type: 'number', |
| 73 | + value: props.value, |
| 74 | + onInput: (e: InputEvent) => { |
| 75 | + const num = (e.target! as HTMLInputElement).valueAsNumber |
| 76 | + emit('update', Number.isNaN(num) ? null : num) |
| 77 | + }, |
| 78 | + }) |
| 79 | + }, |
| 80 | + }) |
| 81 | + customElements.define('my-el-input', CustomInput) |
| 82 | + const num = ref('12') |
| 83 | + const containerComp = defineComponent({ |
| 84 | + setup() { |
| 85 | + return () => { |
| 86 | + return h('div', [ |
| 87 | + h('my-el-input', { |
| 88 | + value: num.value, |
| 89 | + onUpdate: ($event: CustomEvent) => { |
| 90 | + num.value = $event.detail[0] |
| 91 | + }, |
| 92 | + }), |
| 93 | + h('div', { id: 'move' }), |
| 94 | + ]) |
| 95 | + } |
| 96 | + }, |
| 97 | + }) |
| 98 | + const app = createApp(containerComp) |
| 99 | + app.mount(container) |
| 100 | + const myInputEl = container.querySelector('my-el-input')! |
| 101 | + const inputEl = myInputEl.shadowRoot!.querySelector('input')! |
| 102 | + await nextTick() |
| 103 | + expect(inputEl.value).toBe('12') |
| 104 | + const moveEl = container.querySelector('#move')! |
| 105 | + moveEl.append(myInputEl) |
| 106 | + await nextTick() |
| 107 | + myInputEl.removeAttribute('value') |
| 108 | + await nextTick() |
| 109 | + expect(inputEl.value).toBe('') |
| 110 | + }) |
| 111 | + |
63 | 112 | test('should not unmount on move', async () => {
|
64 | 113 | container.innerHTML = `<div><my-element></my-element></div>`
|
65 | 114 | const e = container.childNodes[0].childNodes[0] as VueElement
|
|
0 commit comments