From 90daa5c0ad5ebcfc2d94d93a69173b469b2ac997 Mon Sep 17 00:00:00 2001 From: daiwei Date: Thu, 28 Nov 2024 15:36:55 +0800 Subject: [PATCH] test: add tests --- .../__tests__/directives/vModel.spec.ts | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/packages/runtime-dom/__tests__/directives/vModel.spec.ts b/packages/runtime-dom/__tests__/directives/vModel.spec.ts index 534e5dfe9..755594d87 100644 --- a/packages/runtime-dom/__tests__/directives/vModel.spec.ts +++ b/packages/runtime-dom/__tests__/directives/vModel.spec.ts @@ -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('
') + + show.value = true + await nextTick() + expect(root.innerHTML).toBe( + '
should be ok
', + ) + 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('
') + + show.value = true + await nextTick() + expect(root.innerHTML).toBe( + '
should be true
', + ) + const input = root.querySelector('input')! + expect(input.checked).toEqual(true) + }) })