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', () => {
|
2018-09-21 04:18:22 +08:00
|
|
|
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)
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(original instanceof WeakMap).toBe(true)
|
|
|
|
expect(observed instanceof WeakMap).toBe(true)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +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(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2018-09-21 04:18:22 +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
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
map.customProp = 'Hello World'
|
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +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())
|
2018-09-21 04:18:22 +08:00
|
|
|
const mapSpy = jest.fn(() => (dummy = map.get(key)))
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(mapSpy)
|
2018-09-21 04:18:22 +08:00
|
|
|
|
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(1)
|
2019-10-23 23:00:55 +08:00
|
|
|
map.set(key, undefined)
|
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(2)
|
2018-09-21 04:18:22 +08:00
|
|
|
map.set(key, 'value')
|
|
|
|
expect(dummy).toBe('value')
|
2019-10-23 23:00:55 +08:00
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(3)
|
2018-09-21 04:18:22 +08:00
|
|
|
map.set(key, 'value')
|
|
|
|
expect(dummy).toBe('value')
|
2019-10-23 23:00:55 +08:00
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(3)
|
2018-09-21 04:18:22 +08:00
|
|
|
map.delete(key)
|
|
|
|
expect(dummy).toBe(undefined)
|
2019-10-23 23:00:55 +08:00
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
2018-09-21 04:18:22 +08:00
|
|
|
map.delete(key)
|
|
|
|
expect(dummy).toBe(undefined)
|
2019-10-23 23:00:55 +08:00
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +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())
|
2019-06-11 23:50:28 +08:00
|
|
|
effect(() => (dummy = toRaw(map).get(key)))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +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
|
|
|
|
2018-09-21 04:18:22 +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)
|
2018-09-21 04:18:22 +08:00
|
|
|
const key = {}
|
2019-08-16 21:42:46 +08:00
|
|
|
const value = reactive({})
|
2018-09-21 04:18:22 +08:00
|
|
|
observed.set(key, value)
|
|
|
|
expect(map.get(key)).not.toBe(value)
|
2019-06-11 23:50:28 +08:00
|
|
|
expect(map.get(key)).toBe(toRaw(value))
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should return observable versions of contained values', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(new WeakMap())
|
2018-09-21 04:18:22 +08:00
|
|
|
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)
|
2019-06-11 23:50:28 +08:00
|
|
|
expect(toRaw(wrapped)).toBe(value)
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should observed nested data', () => {
|
2019-10-07 08:18:47 +08:00
|
|
|
const observed = reactive(new WeakMap())
|
2018-09-21 04:18:22 +08:00
|
|
|
const key = {}
|
|
|
|
observed.set(key, { a: 1 })
|
|
|
|
let dummy
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy = observed.get(key).a
|
|
|
|
})
|
|
|
|
observed.get(key).a = 2
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
})
|