test(reactivity): improve built-in Collection subclass test cases (#1885)
This commit is contained in:
parent
eb2ae44d94
commit
02dcc68c24
@ -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)
|
||||
|
||||
const testMap = new CustomMap()
|
||||
const observed = reactive(testMap)
|
||||
expect(observed.count).toBe(0)
|
||||
observed.set('test', 'value')
|
||||
expect(observed.count).toBe(1)
|
||||
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)
|
||||
|
||||
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)', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user