]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test: add tests edison/fix/12460 12482/head
authordaiwei <daiwei521@126.com>
Thu, 28 Nov 2024 07:36:55 +0000 (15:36 +0800)
committerdaiwei <daiwei521@126.com>
Thu, 28 Nov 2024 07:36:55 +0000 (15:36 +0800)
packages/runtime-dom/__tests__/directives/vModel.spec.ts

index 534e5dfe98e4d0b47eec15883d8ab3981a78d0ea..755594d87957ae1a624b3833e029396f2ecc6346 100644 (file)
@@ -5,7 +5,9 @@ import {
   nextTick,
   ref,
   render,
+  vModelCheckbox,
   vModelDynamic,
+  vModelText,
   withDirectives,
 } from '@vue/runtime-dom'
 
@@ -1445,4 +1447,111 @@ describe('vModel', () => {
 
     expect(inputNum1.value).toBe('1')
   })
+
+  // #12460
+  it('prevent input value update in mounted hook if value was updated', async () => {
+    const Comp = defineComponent({
+      data() {
+        return {
+          val: 'bug',
+        }
+      },
+      methods: {
+        onUpdate() {
+          this.val = 'ok'
+        },
+      },
+      render() {
+        return h('div', null, [
+          withDirectives(
+            h('input', {
+              'onUpdate:modelValue': (v: any) => (this.val = v),
+              type: 'search',
+            }),
+            [[vModelText, this.val]],
+          ),
+          'should be ' + this.val,
+          this.$slots.default!({ onUpdate: this.onUpdate }),
+        ])
+      },
+    })
+
+    const show = ref(false)
+    const Page = defineComponent({
+      render() {
+        return show.value
+          ? h(Comp, null, {
+              default: (attrs: any) => {
+                attrs.onUpdate()
+                return h('div')
+              },
+            })
+          : h('div')
+      },
+    })
+
+    render(h(Page), root)
+    expect(root.innerHTML).toBe('<div></div>')
+
+    show.value = true
+    await nextTick()
+    expect(root.innerHTML).toBe(
+      '<div><input type="search">should be ok<div></div></div>',
+    )
+    const input = root.querySelector('input')!
+    expect(input.value).toEqual('ok')
+  })
+
+  it('prevent checkbox value update in mounted hook if value was updated', async () => {
+    const Comp = defineComponent({
+      data() {
+        return {
+          val: false,
+        }
+      },
+      methods: {
+        onUpdate() {
+          this.val = true
+        },
+      },
+      render() {
+        return h('div', null, [
+          withDirectives(
+            h('input', {
+              'onUpdate:modelValue': (v: any) => (this.val = v),
+              type: 'checkbox',
+            }),
+            [[vModelCheckbox, this.val]],
+          ),
+          'should be ' + this.val,
+          this.$slots.default!({ onUpdate: this.onUpdate }),
+        ])
+      },
+    })
+
+    const show = ref(false)
+    const Page = defineComponent({
+      render() {
+        return show.value
+          ? h(Comp, null, {
+              default: (attrs: any) => {
+                attrs.onUpdate()
+                return h('div')
+              },
+            })
+          : h('div')
+      },
+    })
+
+    render(h(Page), root)
+    expect(root.innerHTML).toBe('<div></div>')
+
+    show.value = true
+    await nextTick()
+    expect(root.innerHTML).toBe(
+      '<div><input type="checkbox">should be true<div></div></div>',
+    )
+    const input = root.querySelector('input')!
+    expect(input.checked).toEqual(true)
+  })
 })