diff --git a/packages/reactivity/__tests__/reactive.spec.ts b/packages/reactivity/__tests__/reactive.spec.ts index ab798ff2c8b..a7c46451d1e 100644 --- a/packages/reactivity/__tests__/reactive.spec.ts +++ b/packages/reactivity/__tests__/reactive.spec.ts @@ -1,6 +1,7 @@ import { ref, isRef } from '../src/ref' import { reactive, isReactive, toRaw, markRaw } from '../src/reactive' import { computed } from '../src/computed' +import { effect } from '../src/effect' describe('reactivity/reactive', () => { test('Object', () => { @@ -44,22 +45,59 @@ describe('reactivity/reactive', () => { expect(isReactive(observed.array[0])).toBe(true) }) - test('process subtypes of collections properly', () => { - class CustomMap extends Map { - count = 0 + test('observing subtypes of IterableCollections(Map, Set)', () => { + // subtypes of Map + class CustomMap extends Map {} + const cmap = reactive(new CustomMap()) - set(key: any, value: any): this { - super.set(key, value) - this.count++ - return this - } - } + expect(cmap instanceof Map).toBe(true) + expect(isReactive(cmap)).toBe(true) + + cmap.set('key', {}) + expect(isReactive(cmap.get('key'))).toBe(true) + + // subtypes of Set + class CustomSet extends Set {} + const cset = reactive(new CustomSet()) + + expect(cset instanceof Set).toBe(true) + expect(isReactive(cset)).toBe(true) + + let dummy + effect(() => (dummy = cset.has('value'))) + expect(dummy).toBe(false) + cset.add('value') + expect(dummy).toBe(true) + cset.delete('value') + expect(dummy).toBe(false) + }) + + test('observing subtypes of WeakCollections(WeakMap, WeakSet)', () => { + // subtypes of WeakMap + class CustomMap extends WeakMap {} + const cmap = reactive(new CustomMap()) + + expect(cmap instanceof WeakMap).toBe(true) + expect(isReactive(cmap)).toBe(true) + + const key = {} + cmap.set(key, {}) + expect(isReactive(cmap.get(key))).toBe(true) + + // subtypes of WeakSet + class CustomSet extends WeakSet {} + const cset = reactive(new CustomSet()) + + expect(cset instanceof WeakSet).toBe(true) + expect(isReactive(cset)).toBe(true) - const testMap = new CustomMap() - const observed = reactive(testMap) - expect(observed.count).toBe(0) - observed.set('test', 'value') - expect(observed.count).toBe(1) + let dummy + effect(() => (dummy = cset.has(key))) + expect(dummy).toBe(false) + cset.add(key) + expect(dummy).toBe(true) + cset.delete(key) + expect(dummy).toBe(false) }) test('observed value should proxy mutations to original (Object)', () => {