vue3-yuanma/packages/reactivity/__tests__/collections/WeakMap.spec.ts

122 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-08-16 21:42:46 +08:00
import { reactive, effect, toRaw, isReactive } from '../../src'
2018-09-20 23:43:14 +08:00
2019-08-20 21:58:10 +08:00
describe('reactivity/collections', () => {
describe('WeakMap', () => {
test('instanceof', () => {
const original = new WeakMap()
2019-08-16 21:42:46 +08:00
const observed = reactive(original)
expect(isReactive(observed)).toBe(true)
expect(original instanceof WeakMap).toBe(true)
expect(observed instanceof WeakMap).toBe(true)
})
2018-09-20 23:43:14 +08:00
it('should observe mutations', () => {
let dummy
const key = {}
2019-08-16 21:42:46 +08:00
const map = reactive(new WeakMap())
2018-11-14 00:03:35 +08:00
effect(() => {
dummy = map.get(key)
})
expect(dummy).toBe(undefined)
map.set(key, 'value')
expect(dummy).toBe('value')
map.set(key, 'value2')
expect(dummy).toBe('value2')
map.delete(key)
expect(dummy).toBe(undefined)
2018-09-20 23:43:14 +08:00
})
it('should not observe custom property mutations', () => {
let dummy
2019-08-16 21:42:46 +08:00
const map: any = reactive(new WeakMap())
2018-11-14 00:03:35 +08:00
effect(() => (dummy = map.customProp))
2018-09-20 23:43:14 +08:00
expect(dummy).toBe(undefined)
map.customProp = 'Hello World'
expect(dummy).toBe(undefined)
})
2018-09-20 23:43:14 +08:00
it('should not observe non value changing mutations', () => {
let dummy
const key = {}
2019-08-16 21:42:46 +08:00
const map = reactive(new WeakMap())
const mapSpy = jest.fn(() => (dummy = map.get(key)))
2018-11-14 00:03:35 +08:00
effect(mapSpy)
expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(1)
map.set(key, undefined)
expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(2)
map.set(key, 'value')
expect(dummy).toBe('value')
expect(mapSpy).toHaveBeenCalledTimes(3)
map.set(key, 'value')
expect(dummy).toBe('value')
expect(mapSpy).toHaveBeenCalledTimes(3)
map.delete(key)
expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(4)
map.delete(key)
expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(4)
})
2018-09-20 23:43:14 +08:00
it('should not observe raw data', () => {
let dummy
const key = {}
2019-08-16 21:42:46 +08:00
const map = reactive(new WeakMap())
effect(() => (dummy = toRaw(map).get(key)))
2018-09-20 23:43:14 +08:00
expect(dummy).toBe(undefined)
map.set(key, 'Hello')
expect(dummy).toBe(undefined)
map.delete(key)
expect(dummy).toBe(undefined)
})
2018-09-20 23:43:14 +08:00
it('should not pollute original Map with Proxies', () => {
const map = new WeakMap()
2019-08-16 21:42:46 +08:00
const observed = reactive(map)
const key = {}
2019-08-16 21:42:46 +08:00
const value = reactive({})
observed.set(key, value)
expect(map.get(key)).not.toBe(value)
expect(map.get(key)).toBe(toRaw(value))
})
2018-09-20 23:43:14 +08:00
it('should return observable versions of contained values', () => {
2019-08-16 21:42:46 +08:00
const observed = reactive(new WeakMap())
const key = {}
const value = {}
observed.set(key, value)
const wrapped = observed.get(key)
2019-08-16 21:42:46 +08:00
expect(isReactive(wrapped)).toBe(true)
expect(toRaw(wrapped)).toBe(value)
})
it('should observed nested data', () => {
const observed = reactive(new WeakMap())
const key = {}
observed.set(key, { a: 1 })
let dummy
2018-11-14 00:03:35 +08:00
effect(() => {
dummy = observed.get(key).a
})
observed.get(key).a = 2
expect(dummy).toBe(2)
})
it('should not be trigger when the value and the old value both are NaN', () => {
const map = new WeakMap()
const key = {}
map.set(key, NaN)
const mapSpy = jest.fn(() => map.get(key))
effect(mapSpy)
map.set(key, NaN)
expect(mapSpy).toHaveBeenCalledTimes(1)
})
2018-09-20 23:43:14 +08:00
})
})