2019-08-16 21:42:46 +08:00
|
|
|
import { reactive, isReactive, effect, toRaw } 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('WeakSet', () => {
|
|
|
|
it('instanceof', () => {
|
2019-10-07 08:18:47 +08:00
|
|
|
const original = new WeakSet()
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
|
|
|
expect(isReactive(observed)).toBe(true)
|
2019-10-07 08:18:47 +08:00
|
|
|
expect(original instanceof WeakSet).toBe(true)
|
|
|
|
expect(observed instanceof WeakSet).toBe(true)
|
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 observe mutations', () => {
|
|
|
|
let dummy
|
|
|
|
const value = {}
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new WeakSet())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => (dummy = set.has(value)))
|
2020-01-14 06:11:49 +08:00
|
|
|
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
set.add(value)
|
|
|
|
expect(dummy).toBe(true)
|
|
|
|
set.delete(value)
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe mutations with observed value', () => {
|
|
|
|
let dummy
|
|
|
|
const value = reactive({})
|
|
|
|
const set = reactive(new WeakSet())
|
|
|
|
effect(() => (dummy = set.has(value)))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(false)
|
|
|
|
set.add(value)
|
|
|
|
expect(dummy).toBe(true)
|
|
|
|
set.delete(value)
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
})
|
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 set: any = reactive(new WeakSet())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => (dummy = set.customProp))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
set.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 value = {}
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new WeakSet())
|
2018-09-21 04:18:22 +08:00
|
|
|
const setSpy = jest.fn(() => (dummy = set.has(value)))
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(setSpy)
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(false)
|
|
|
|
expect(setSpy).toHaveBeenCalledTimes(1)
|
|
|
|
set.add(value)
|
|
|
|
expect(dummy).toBe(true)
|
|
|
|
expect(setSpy).toHaveBeenCalledTimes(2)
|
|
|
|
set.add(value)
|
|
|
|
expect(dummy).toBe(true)
|
|
|
|
expect(setSpy).toHaveBeenCalledTimes(2)
|
|
|
|
set.delete(value)
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
expect(setSpy).toHaveBeenCalledTimes(3)
|
|
|
|
set.delete(value)
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
expect(setSpy).toHaveBeenCalledTimes(3)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should not observe raw data', () => {
|
|
|
|
const value = {}
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new WeakSet())
|
2019-06-11 23:50:28 +08:00
|
|
|
effect(() => (dummy = toRaw(set).has(value)))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(false)
|
|
|
|
set.add(value)
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not be triggered by raw mutations', () => {
|
|
|
|
const value = {}
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new WeakSet())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => (dummy = set.has(value)))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(false)
|
2019-06-11 23:50:28 +08:00
|
|
|
toRaw(set).add(value)
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(false)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should not pollute original Set with Proxies', () => {
|
|
|
|
const set = new WeakSet()
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(set)
|
|
|
|
const value = reactive({})
|
2018-09-21 04:18:22 +08:00
|
|
|
observed.add(value)
|
|
|
|
expect(observed.has(value)).toBe(true)
|
|
|
|
expect(set.has(value)).toBe(false)
|
|
|
|
})
|
2020-11-28 03:16:00 +08:00
|
|
|
|
|
|
|
it('should return proxy from WeakSet.add call', () => {
|
|
|
|
const set = reactive(new WeakSet())
|
|
|
|
const result = set.add({})
|
|
|
|
expect(result).toBe(set)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
})
|