]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
chore: update tests edison/fix/13665 13672/head
authordaiwei <daiwei521@126.com>
Mon, 21 Jul 2025 07:19:03 +0000 (15:19 +0800)
committerdaiwei <daiwei521@126.com>
Mon, 21 Jul 2025 08:08:55 +0000 (16:08 +0800)
packages/runtime-core/src/index.ts
packages/runtime-core/src/rendererTemplateRef.ts
packages/runtime-vapor/__tests__/dom/templateRef.spec.ts
packages/runtime-vapor/src/apiTemplateRef.ts

index 00fef2582f85bb01002d38575f969cd088264750..9d97bb18593ca93a388011aedabb0de5c5b4ac72 100644 (file)
@@ -565,4 +565,4 @@ export { createInternalObject } from './internalObject'
 /**
  * @internal
  */
-export { validateTemplateRef } from './rendererTemplateRef'
+export { createCanSetSetupRefChecker } from './rendererTemplateRef'
index eef60a6e4cbf50642c000749e7cb1c278d960cf5..84f0bda1b95bead1f62e7647d7d0e80bd344bb8e 100644 (file)
@@ -77,7 +77,7 @@ export function setRef(
   const oldRef = oldRawRef && (oldRawRef as VNodeNormalizedRefAtom).r
   const refs = owner.refs === EMPTY_OBJ ? (owner.refs = {}) : owner.refs
   const setupState = owner.setupState
-  const canSetSetupRef = validateTemplateRef(setupState)
+  const canSetSetupRef = createCanSetSetupRefChecker(setupState)
 
   // dynamic ref changed. unset old ref
   if (oldRef != null && oldRef !== ref) {
@@ -148,7 +148,7 @@ export function setRef(
   }
 }
 
-export function validateTemplateRef(
+export function createCanSetSetupRefChecker(
   setupState: Data,
 ): (key: string) => boolean {
   const rawSetupState = toRaw(setupState)
index f1ce23ac156b5ceddd4238063c14245c8057892e..4e8a1a9253c2a41d07d673c55eb25b5d5d2d93bc 100644 (file)
@@ -19,6 +19,7 @@ import {
   nextTick,
   reactive,
   ref,
+  shallowRef,
   useTemplateRef,
   watchEffect,
 } from '@vue/runtime-dom'
@@ -208,8 +209,8 @@ describe('api: template ref', () => {
     const { render } = define({
       setup() {
         return {
-          foo: fooEl,
-          bar: barEl,
+          foo: shallowRef(fooEl),
+          bar: shallowRef(barEl),
         }
       },
       render() {
@@ -251,6 +252,7 @@ describe('api: template ref', () => {
     })
     const { host } = render()
     expect(state.refKey).toBe(host.children[0])
+    expect('Template ref "refKey" used on a non-ref value').toHaveBeenWarned()
   })
 
   test('multiple root refs', () => {
@@ -713,6 +715,45 @@ describe('api: template ref', () => {
     expect(html()).toBe('<div>changed</div><!--dynamic-component-->')
   })
 
+  test('should not attempt to set when variable name is same as key', () => {
+    let tRef: ShallowRef
+    const key = 'refKey'
+    define({
+      setup() {
+        tRef = useTemplateRef('_')
+        return {
+          [key]: tRef,
+        }
+      },
+      render() {
+        const n0 = template('<div></div>')() as Element
+        createTemplateRefSetter()(n0, key)
+        return n0
+      },
+    }).render()
+    expect('target is readonly').not.toHaveBeenWarned()
+    expect(tRef!.value).toBe(null)
+  })
+
+  test('should work when used as direct ref value (compiled in prod mode)', () => {
+    __DEV__ = false
+    try {
+      let foo: ShallowRef
+      const { host } = define({
+        setup() {
+          foo = useTemplateRef('foo')
+          const n0 = template('<div></div>')() as Element
+          createTemplateRefSetter()(n0, foo)
+          return n0
+        },
+      }).render()
+      expect('target is readonly').not.toHaveBeenWarned()
+      expect(foo!.value).toBe(host.children[0])
+    } finally {
+      __DEV__ = true
+    }
+  })
+
   // TODO: can not reproduce in Vapor
   // // #2078
   // test('handling multiple merged refs', async () => {
index 1b88d16c82d349b1e362d57f52b69fcd8d484c07..a14f373e7def428d3dc890c5ff61ddcfeb930077 100644 (file)
@@ -9,8 +9,8 @@ import {
   ErrorCodes,
   type SchedulerJob,
   callWithErrorHandling,
+  createCanSetSetupRefChecker,
   queuePostFlushCb,
-  validateTemplateRef,
   warn,
 } from '@vue/runtime-dom'
 import {
@@ -56,7 +56,7 @@ export function setRef(
   const refs =
     instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs
 
-  const canSetSetupRef = validateTemplateRef(setupState)
+  const canSetSetupRef = createCanSetSetupRefChecker(setupState)
   // dynamic ref changed. unset old ref
   if (oldRef != null && oldRef !== ref) {
     if (isString(oldRef)) {