diff --git a/packages/reactivity/__tests__/collections/Map.spec.ts b/packages/reactivity/__tests__/collections/Map.spec.ts index b190da32fce..4cfb9e6bbcd 100644 --- a/packages/reactivity/__tests__/collections/Map.spec.ts +++ b/packages/reactivity/__tests__/collections/Map.spec.ts @@ -26,6 +26,22 @@ describe('reactivity/collections', () => { expect(dummy).toBe(undefined) }) + it('should observe mutations with observed value as key', () => { + let dummy + const key = reactive({}) + const value = reactive({}) + const map = reactive(new Map()) + effect(() => { + dummy = map.get(key) + }) + + expect(dummy).toBe(undefined) + map.set(key, value) + expect(dummy).toBe(value) + map.delete(key) + expect(dummy).toBe(undefined) + }) + it('should observe size mutations', () => { let dummy const map = reactive(new Map()) diff --git a/packages/reactivity/__tests__/collections/Set.spec.ts b/packages/reactivity/__tests__/collections/Set.spec.ts index f01090d2f54..9e3efa166f1 100644 --- a/packages/reactivity/__tests__/collections/Set.spec.ts +++ b/packages/reactivity/__tests__/collections/Set.spec.ts @@ -22,6 +22,19 @@ describe('reactivity/collections', () => { expect(dummy).toBe(false) }) + it('should observe mutations with observed value', () => { + let dummy + const value = reactive({}) + const set = reactive(new Set()) + effect(() => (dummy = set.has(value))) + + expect(dummy).toBe(false) + set.add(value) + expect(dummy).toBe(true) + set.delete(value) + expect(dummy).toBe(false) + }) + it('should observe for of iteration', () => { let dummy const set = reactive(new Set() as Set) diff --git a/packages/reactivity/__tests__/collections/WeakMap.spec.ts b/packages/reactivity/__tests__/collections/WeakMap.spec.ts index 6c82b82dd86..e3dde72e618 100644 --- a/packages/reactivity/__tests__/collections/WeakMap.spec.ts +++ b/packages/reactivity/__tests__/collections/WeakMap.spec.ts @@ -27,6 +27,22 @@ describe('reactivity/collections', () => { expect(dummy).toBe(undefined) }) + it('should observe mutations with observed value as key', () => { + let dummy + const key = reactive({}) + const value = reactive({}) + const map = reactive(new WeakMap()) + effect(() => { + dummy = map.get(key) + }) + + expect(dummy).toBe(undefined) + map.set(key, value) + expect(dummy).toBe(value) + map.delete(key) + expect(dummy).toBe(undefined) + }) + it('should not observe custom property mutations', () => { let dummy const map: any = reactive(new WeakMap()) diff --git a/packages/reactivity/__tests__/collections/WeakSet.spec.ts b/packages/reactivity/__tests__/collections/WeakSet.spec.ts index 2427de125e5..182298cf4b0 100644 --- a/packages/reactivity/__tests__/collections/WeakSet.spec.ts +++ b/packages/reactivity/__tests__/collections/WeakSet.spec.ts @@ -23,6 +23,19 @@ describe('reactivity/collections', () => { expect(dummy).toBe(false) }) + it('should observe mutations with observed value', () => { + let dummy + const value = reactive({}) + const set = reactive(new WeakSet()) + effect(() => (dummy = set.has(value))) + + expect(dummy).toBe(false) + set.add(value) + expect(dummy).toBe(true) + set.delete(value) + expect(dummy).toBe(false) + }) + it('should not observe custom property mutations', () => { let dummy const set: any = reactive(new WeakSet()) diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index b5289844089..cb90cf0e3c2 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -63,6 +63,7 @@ function add(this: SetTypes, value: unknown) { function set(this: MapTypes, key: unknown, value: unknown) { value = toRaw(value) + key = toRaw(key) const target = toRaw(this) const proto = getProto(target) const hadKey = proto.has.call(target, key) @@ -87,6 +88,7 @@ function set(this: MapTypes, key: unknown, value: unknown) { } function deleteEntry(this: CollectionTypes, key: unknown) { + key = toRaw(key) const target = toRaw(this) const proto = getProto(target) const hadKey = proto.has.call(target, key)