Skip to content

Commit

Permalink
test(reactivity): improve built-in Collection subclass test cases (#1885
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Pick authored Aug 19, 2020
1 parent eb2ae44 commit 02dcc68
Showing 1 changed file with 52 additions and 14 deletions.
66 changes: 52 additions & 14 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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)', () => {
Expand Down

0 comments on commit 02dcc68

Please sign in to comment.