]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): clear method on readonly collections should return undefined (#7316)
author <2553241022@qq.com>
Thu, 9 Nov 2023 09:32:21 +0000 (17:32 +0800)
committerGitHub <noreply@github.com>
Thu, 9 Nov 2023 09:32:21 +0000 (17:32 +0800)
packages/reactivity/__tests__/readonly.spec.ts
packages/reactivity/__tests__/shallowReadonly.spec.ts
packages/reactivity/src/collectionHandlers.ts

index d0c91f0fb9f84705d0446881c518f6ab139c8319..448419d31766d81e8441f422c2ac207038150d87 100644 (file)
@@ -275,6 +275,14 @@ describe('reactivity/readonly', () => {
             expect(isReactive(value)).toBe(true)
           }
         })
+
+        test('should return undefined from Map.clear() call', () => {
+          const wrapped = readonly(new Collection())
+          expect(wrapped.clear()).toBeUndefined()
+          expect(
+            `Clear operation failed: target is readonly.`
+          ).toHaveBeenWarned()
+        })
       }
     })
   })
@@ -332,6 +340,14 @@ describe('reactivity/readonly', () => {
             expect(isReadonly(v2)).toBe(true)
           }
         })
+
+        test('should return undefined from Set.clear() call', () => {
+          const wrapped = readonly(new Collection())
+          expect(wrapped.clear()).toBeUndefined()
+          expect(
+            `Clear operation failed: target is readonly.`
+          ).toHaveBeenWarned()
+        })
       }
     })
   })
index 79d4376cc015de26740a4de0cb26064d946533b3..b6736f4a59586e9b22321c25ff8abab266eb712c 100644 (file)
@@ -113,6 +113,12 @@ describe('reactivity/shallowReadonly', () => {
         ).not.toHaveBeenWarned()
       })
     })
+
+    test('should return undefined from Map.clear() call', () => {
+      const sroMap = shallowReadonly(new Map())
+      expect(sroMap.clear()).toBeUndefined()
+      expect(`Clear operation failed: target is readonly.`).toHaveBeenWarned()
+    })
   })
 
   describe('collection/Set', () => {
@@ -197,5 +203,11 @@ describe('reactivity/shallowReadonly', () => {
         ).not.toHaveBeenWarned()
       })
     })
+
+    test('should return undefined from Set.clear() call', () => {
+      const sroSet = shallowReadonly(new Set())
+      expect(sroSet.clear()).toBeUndefined()
+      expect(`Clear operation failed: target is readonly.`).toHaveBeenWarned()
+    })
   })
 })
index 1d07af3be8c7e3d00447ee82abb1ef6f0667da80..fe7d13d18412198e04c02add7404d5863479aac1 100644 (file)
@@ -223,7 +223,11 @@ function createReadonlyMethod(type: TriggerOpTypes): Function {
         toRaw(this)
       )
     }
-    return type === TriggerOpTypes.DELETE ? false : this
+    return type === TriggerOpTypes.DELETE
+      ? false
+      : type === TriggerOpTypes.CLEAR
+      ? undefined
+      : this
   }
 }