2019-08-16 21:42:46 +08:00
|
|
|
import { reactive, effect, isReactive, 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('Set', () => {
|
|
|
|
it('instanceof', () => {
|
|
|
|
const original = new Set()
|
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 Set).toBe(true)
|
|
|
|
expect(observed instanceof Set).toBe(true)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should observe mutations', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
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)
|
|
|
|
set.add('value')
|
|
|
|
expect(dummy).toBe(true)
|
|
|
|
set.delete('value')
|
|
|
|
expect(dummy).toBe(false)
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
|
2020-01-14 06:11:49 +08:00
|
|
|
it('should observe mutations with observed value', () => {
|
|
|
|
let dummy
|
|
|
|
const value = reactive({})
|
|
|
|
const set = reactive(new Set())
|
|
|
|
effect(() => (dummy = set.has(value)))
|
|
|
|
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
set.add(value)
|
|
|
|
expect(dummy).toBe(true)
|
|
|
|
set.delete(value)
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
})
|
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should observe for of iteration', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set() as Set<number>)
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy = 0
|
|
|
|
for (let num of set) {
|
|
|
|
dummy += num
|
|
|
|
}
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(0)
|
|
|
|
set.add(2)
|
|
|
|
set.add(1)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
set.delete(2)
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.clear()
|
|
|
|
expect(dummy).toBe(0)
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should observe forEach iteration', () => {
|
|
|
|
let dummy: any
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy = 0
|
|
|
|
set.forEach(num => (dummy += num))
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(0)
|
|
|
|
set.add(2)
|
|
|
|
set.add(1)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
set.delete(2)
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.clear()
|
|
|
|
expect(dummy).toBe(0)
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should observe values iteration', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set() as Set<number>)
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy = 0
|
|
|
|
for (let num of set.values()) {
|
|
|
|
dummy += num
|
|
|
|
}
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(0)
|
|
|
|
set.add(2)
|
|
|
|
set.add(1)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
set.delete(2)
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.clear()
|
|
|
|
expect(dummy).toBe(0)
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should observe keys iteration', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set() as Set<number>)
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy = 0
|
|
|
|
for (let num of set.keys()) {
|
|
|
|
dummy += num
|
|
|
|
}
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(0)
|
|
|
|
set.add(2)
|
|
|
|
set.add(1)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
set.delete(2)
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.clear()
|
|
|
|
expect(dummy).toBe(0)
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should observe entries iteration', () => {
|
|
|
|
let dummy
|
2019-10-05 22:39:40 +08:00
|
|
|
const set = reactive(new Set<number>())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy = 0
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
for (let [key, num] of set.entries()) {
|
|
|
|
key
|
|
|
|
dummy += num
|
|
|
|
}
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(0)
|
|
|
|
set.add(2)
|
|
|
|
set.add(1)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
set.delete(2)
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.clear()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should be triggered by clearing', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => (dummy = set.has('key')))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(false)
|
|
|
|
set.add('key')
|
|
|
|
expect(dummy).toBe(true)
|
|
|
|
set.clear()
|
|
|
|
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 Set())
|
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 observe size mutations', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => (dummy = set.size))
|
2018-09-21 04:18:22 +08:00
|
|
|
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
set.add('value')
|
|
|
|
set.add('value2')
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
set.delete('value')
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.clear()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
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
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
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-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)
|
|
|
|
set.clear()
|
|
|
|
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', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
2019-06-11 23:50:28 +08:00
|
|
|
effect(() => (dummy = toRaw(set).has('value')))
|
2018-09-21 04:18:22 +08:00
|
|
|
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
set.add('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 raw iterations', () => {
|
|
|
|
let dummy = 0
|
2019-10-05 22:39:40 +08:00
|
|
|
const set = reactive(new Set<number>())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy = 0
|
2019-06-11 23:50:28 +08:00
|
|
|
for (let [num] of toRaw(set).entries()) {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy += num
|
|
|
|
}
|
2019-06-11 23:50:28 +08:00
|
|
|
for (let num of toRaw(set).keys()) {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy += num
|
|
|
|
}
|
2019-06-11 23:50:28 +08:00
|
|
|
for (let num of toRaw(set).values()) {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy += num
|
|
|
|
}
|
2019-06-11 23:50:28 +08:00
|
|
|
toRaw(set).forEach(num => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy += num
|
|
|
|
})
|
2019-06-11 23:50:28 +08:00
|
|
|
for (let num of toRaw(set)) {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy += num
|
|
|
|
}
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
2018-09-21 04:18:22 +08:00
|
|
|
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
set.add(2)
|
|
|
|
set.add(3)
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
set.delete(2)
|
|
|
|
expect(dummy).toBe(0)
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should not be triggered by raw mutations', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => (dummy = set.has('value')))
|
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)
|
|
|
|
dummy = true
|
2019-06-11 23:50:28 +08:00
|
|
|
toRaw(set).delete('value')
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(true)
|
2019-06-11 23:50:28 +08:00
|
|
|
toRaw(set).clear()
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(true)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should not observe raw size mutations', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
2019-06-11 23:50:28 +08:00
|
|
|
effect(() => (dummy = toRaw(set).size))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(0)
|
|
|
|
set.add('value')
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should not be triggered by raw size mutations', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => (dummy = set.size))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(0)
|
2019-06-11 23:50:28 +08:00
|
|
|
toRaw(set).add('value')
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
it('should support objects as key', () => {
|
|
|
|
let dummy
|
|
|
|
const key = {}
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set())
|
2018-09-21 04:18:22 +08:00
|
|
|
const setSpy = jest.fn(() => (dummy = set.has(key)))
|
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)
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
set.add({})
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
expect(setSpy).toHaveBeenCalledTimes(1)
|
2018-09-20 23:43:14 +08:00
|
|
|
|
2018-09-21 04:18:22 +08:00
|
|
|
set.add(key)
|
|
|
|
expect(dummy).toBe(true)
|
|
|
|
expect(setSpy).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
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 Set()
|
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)
|
|
|
|
})
|
2018-09-22 04:54:12 +08:00
|
|
|
|
|
|
|
it('should observe nested values in iterations (forEach)', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set([{ foo: 1 }]))
|
2018-09-22 04:54:12 +08:00
|
|
|
let dummy: any
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-22 04:54:12 +08:00
|
|
|
dummy = 0
|
|
|
|
set.forEach(value => {
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
dummy += value.foo
|
|
|
|
})
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.forEach(value => {
|
|
|
|
value.foo++
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe nested values in iterations (values)', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set([{ foo: 1 }]))
|
2018-09-22 04:54:12 +08:00
|
|
|
let dummy: any
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-22 04:54:12 +08:00
|
|
|
dummy = 0
|
|
|
|
for (const value of set.values()) {
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
dummy += value.foo
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.forEach(value => {
|
|
|
|
value.foo++
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe nested values in iterations (entries)', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set([{ foo: 1 }]))
|
2018-09-22 04:54:12 +08:00
|
|
|
let dummy: any
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-22 04:54:12 +08:00
|
|
|
dummy = 0
|
|
|
|
for (const [key, value] of set.entries()) {
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(key)).toBe(true)
|
|
|
|
expect(isReactive(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
dummy += value.foo
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.forEach(value => {
|
|
|
|
value.foo++
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe nested values in iterations (for...of)', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const set = reactive(new Set([{ foo: 1 }]))
|
2018-09-22 04:54:12 +08:00
|
|
|
let dummy: any
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-22 04:54:12 +08:00
|
|
|
dummy = 0
|
|
|
|
for (const value of set) {
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
dummy += value.foo
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
set.forEach(value => {
|
|
|
|
value.foo++
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
2020-03-07 00:10:02 +08:00
|
|
|
|
|
|
|
it('should work with reactive entries in raw set', () => {
|
|
|
|
const raw = new Set()
|
|
|
|
const entry = reactive({})
|
|
|
|
raw.add(entry)
|
|
|
|
const set = reactive(raw)
|
|
|
|
|
|
|
|
expect(set.has(entry)).toBe(true)
|
|
|
|
|
|
|
|
expect(set.delete(entry)).toBe(true)
|
|
|
|
expect(set.has(entry)).toBe(false)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
})
|