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', () => {
|
2020-09-15 09:31:04 +08:00
|
|
|
function coverCollectionFn(collection: Map<any, any>, fnName: string) {
|
|
|
|
const spy = jest.fn()
|
|
|
|
let proxy = reactive(collection)
|
|
|
|
;(collection as any)[fnName] = spy
|
|
|
|
return [proxy as any, spy]
|
|
|
|
}
|
|
|
|
|
2018-09-20 23:43:14 +08:00
|
|
|
describe('Map', () => {
|
|
|
|
test('instanceof', () => {
|
|
|
|
const original = new Map()
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
|
|
|
expect(isReactive(observed)).toBe(true)
|
2018-09-20 23:43:14 +08:00
|
|
|
expect(original instanceof Map).toBe(true)
|
|
|
|
expect(observed instanceof Map).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe mutations', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-20 23:43:14 +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)
|
|
|
|
})
|
|
|
|
|
2020-01-14 06:11:49 +08:00
|
|
|
it('should observe mutations with observed value as key', () => {
|
|
|
|
let dummy
|
|
|
|
const key = reactive({})
|
|
|
|
const value = reactive({})
|
|
|
|
const map = reactive(new Map())
|
|
|
|
effect(() => {
|
|
|
|
dummy = map.get(key)
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
map.set(key, value)
|
|
|
|
expect(dummy).toBe(value)
|
|
|
|
map.delete(key)
|
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
})
|
|
|
|
|
2018-09-20 23:43:14 +08:00
|
|
|
it('should observe size mutations', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => (dummy = map.size))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
map.set('key1', 'value')
|
|
|
|
map.set('key2', 'value2')
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
map.delete('key1')
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
map.clear()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe for of iteration', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-20 23:43:14 +08:00
|
|
|
dummy = 0
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
for (let [key, num] of map) {
|
|
|
|
key
|
|
|
|
dummy += num
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
map.set('key1', 3)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
map.set('key2', 2)
|
|
|
|
expect(dummy).toBe(5)
|
2020-02-18 13:09:24 +08:00
|
|
|
// iteration should track mutation of existing entries (#709)
|
|
|
|
map.set('key1', 4)
|
|
|
|
expect(dummy).toBe(6)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.delete('key1')
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
map.clear()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe forEach iteration', () => {
|
|
|
|
let dummy: any
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-20 23:43:14 +08:00
|
|
|
dummy = 0
|
|
|
|
map.forEach((num: any) => (dummy += num))
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
map.set('key1', 3)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
map.set('key2', 2)
|
|
|
|
expect(dummy).toBe(5)
|
2020-02-18 13:09:24 +08:00
|
|
|
// iteration should track mutation of existing entries (#709)
|
|
|
|
map.set('key1', 4)
|
|
|
|
expect(dummy).toBe(6)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.delete('key1')
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
map.clear()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe keys iteration', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-20 23:43:14 +08:00
|
|
|
dummy = 0
|
|
|
|
for (let key of map.keys()) {
|
|
|
|
dummy += key
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
map.set(3, 3)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
map.set(2, 2)
|
|
|
|
expect(dummy).toBe(5)
|
|
|
|
map.delete(3)
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
map.clear()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe values iteration', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-20 23:43:14 +08:00
|
|
|
dummy = 0
|
|
|
|
for (let num of map.values()) {
|
|
|
|
dummy += num
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
map.set('key1', 3)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
map.set('key2', 2)
|
|
|
|
expect(dummy).toBe(5)
|
2020-02-18 13:09:24 +08:00
|
|
|
// iteration should track mutation of existing entries (#709)
|
|
|
|
map.set('key1', 4)
|
|
|
|
expect(dummy).toBe(6)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.delete('key1')
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
map.clear()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should observe entries iteration', () => {
|
|
|
|
let dummy
|
2019-10-08 23:00:20 +08:00
|
|
|
let dummy2
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2019-10-08 23:00:20 +08:00
|
|
|
dummy = ''
|
|
|
|
dummy2 = 0
|
2018-09-20 23:43:14 +08:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
for (let [key, num] of map.entries()) {
|
2019-10-08 23:00:20 +08:00
|
|
|
dummy += key
|
|
|
|
dummy2 += num
|
2018-09-20 23:43:14 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-10-08 23:00:20 +08:00
|
|
|
expect(dummy).toBe('')
|
|
|
|
expect(dummy2).toBe(0)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.set('key1', 3)
|
2019-10-08 23:00:20 +08:00
|
|
|
expect(dummy).toBe('key1')
|
|
|
|
expect(dummy2).toBe(3)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.set('key2', 2)
|
2019-10-08 23:00:20 +08:00
|
|
|
expect(dummy).toBe('key1key2')
|
|
|
|
expect(dummy2).toBe(5)
|
2020-02-18 13:09:24 +08:00
|
|
|
// iteration should track mutation of existing entries (#709)
|
|
|
|
map.set('key1', 4)
|
|
|
|
expect(dummy).toBe('key1key2')
|
|
|
|
expect(dummy2).toBe(6)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.delete('key1')
|
2019-10-08 23:00:20 +08:00
|
|
|
expect(dummy).toBe('key2')
|
|
|
|
expect(dummy2).toBe(2)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.clear()
|
2019-10-08 23:00:20 +08:00
|
|
|
expect(dummy).toBe('')
|
|
|
|
expect(dummy2).toBe(0)
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should be triggered by clearing', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => (dummy = map.get('key')))
|
2018-09-20 23:43:14 +08:00
|
|
|
|
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
map.set('key', 3)
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
map.clear()
|
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not observe custom property mutations', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const map: any = reactive(new Map())
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not observe non value changing mutations', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2018-09-20 23:43:14 +08:00
|
|
|
const mapSpy = jest.fn(() => (dummy = map.get('key')))
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(mapSpy)
|
2018-09-20 23:43:14 +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-20 23:43:14 +08:00
|
|
|
map.set('key', 'value')
|
|
|
|
expect(dummy).toBe('value')
|
2019-10-23 23:00:55 +08:00
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(3)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.set('key', 'value')
|
|
|
|
expect(dummy).toBe('value')
|
2019-10-23 23:00:55 +08:00
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(3)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.delete('key')
|
|
|
|
expect(dummy).toBe(undefined)
|
2019-10-23 23:00:55 +08:00
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.delete('key')
|
|
|
|
expect(dummy).toBe(undefined)
|
2019-10-23 23:00:55 +08:00
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
2018-09-20 23:43:14 +08:00
|
|
|
map.clear()
|
|
|
|
expect(dummy).toBe(undefined)
|
2019-10-23 23:00:55 +08:00
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not observe raw data', () => {
|
|
|
|
let dummy
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map())
|
2019-06-11 23:50:28 +08:00
|
|
|
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-21 04:18:22 +08:00
|
|
|
|
|
|
|
it('should not pollute original Map with Proxies', () => {
|
|
|
|
const map = new Map()
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(map)
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
it('should return observable versions of contained values', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(new Map())
|
2018-09-21 04:18:22 +08:00
|
|
|
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-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(new Map())
|
2018-09-21 04:18:22 +08:00
|
|
|
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-22 01:34:00 +08:00
|
|
|
|
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 map = reactive(new Map([[1, { 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
|
|
|
|
map.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)
|
2019-10-05 22:39:40 +08:00
|
|
|
map.get(1)!.foo++
|
2018-09-22 04:54:12 +08:00
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
2018-09-22 01:34:00 +08:00
|
|
|
|
2018-09-22 04:54:12 +08:00
|
|
|
it('should observe nested values in iterations (values)', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map([[1, { 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 map.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)
|
2019-10-05 22:39:40 +08:00
|
|
|
map.get(1)!.foo++
|
2018-09-22 04:54:12 +08:00
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
2018-09-22 01:34:00 +08:00
|
|
|
|
2018-09-22 04:54:12 +08:00
|
|
|
it('should observe nested values in iterations (entries)', () => {
|
|
|
|
const key = {}
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map([[key, { 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 map.entries()) {
|
|
|
|
key
|
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)
|
2019-10-05 22:39:40 +08:00
|
|
|
map.get(key)!.foo++
|
2018-09-22 04:54:12 +08:00
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
2018-09-22 01:34:00 +08:00
|
|
|
|
2018-09-22 04:54:12 +08:00
|
|
|
it('should observe nested values in iterations (for...of)', () => {
|
|
|
|
const key = {}
|
2019-08-16 21:42:46 +08:00
|
|
|
const map = reactive(new Map([[key, { 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 map) {
|
|
|
|
key
|
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)
|
2019-10-05 22:39:40 +08:00
|
|
|
map.get(key)!.foo++
|
2018-09-22 04:54:12 +08:00
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
2019-10-23 23:53:43 +08:00
|
|
|
|
|
|
|
it('should not be trigger when the value and the old value both are NaN', () => {
|
|
|
|
const map = reactive(new Map([['foo', NaN]]))
|
|
|
|
const mapSpy = jest.fn(() => map.get('foo'))
|
|
|
|
effect(mapSpy)
|
|
|
|
map.set('foo', NaN)
|
|
|
|
expect(mapSpy).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
2020-03-07 00:10:02 +08:00
|
|
|
|
|
|
|
it('should work with reactive keys in raw map', () => {
|
|
|
|
const raw = new Map()
|
|
|
|
const key = reactive({})
|
|
|
|
raw.set(key, 1)
|
|
|
|
const map = reactive(raw)
|
|
|
|
|
|
|
|
expect(map.has(key)).toBe(true)
|
|
|
|
expect(map.get(key)).toBe(1)
|
|
|
|
|
|
|
|
expect(map.delete(key)).toBe(true)
|
|
|
|
expect(map.has(key)).toBe(false)
|
|
|
|
expect(map.get(key)).toBeUndefined()
|
|
|
|
})
|
2020-03-25 00:43:06 +08:00
|
|
|
|
2020-04-05 00:57:15 +08:00
|
|
|
it('should track set of reactive keys in raw map', () => {
|
|
|
|
const raw = new Map()
|
|
|
|
const key = reactive({})
|
|
|
|
raw.set(key, 1)
|
|
|
|
const map = reactive(raw)
|
|
|
|
|
|
|
|
let dummy
|
|
|
|
effect(() => {
|
|
|
|
dummy = map.get(key)
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
|
|
|
|
map.set(key, 2)
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should track deletion of reactive keys in raw map', () => {
|
|
|
|
const raw = new Map()
|
|
|
|
const key = reactive({})
|
|
|
|
raw.set(key, 1)
|
|
|
|
const map = reactive(raw)
|
|
|
|
|
|
|
|
let dummy
|
|
|
|
effect(() => {
|
|
|
|
dummy = map.has(key)
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(true)
|
|
|
|
|
|
|
|
map.delete(key)
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn when both raw and reactive versions of the same object is used as key', () => {
|
|
|
|
const raw = new Map()
|
|
|
|
const rawKey = {}
|
|
|
|
const key = reactive(rawKey)
|
|
|
|
raw.set(rawKey, 1)
|
|
|
|
raw.set(key, 1)
|
|
|
|
const map = reactive(raw)
|
|
|
|
map.set(key, 2)
|
|
|
|
expect(
|
|
|
|
`Reactive Map contains both the raw and reactive`
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
2020-03-25 00:43:06 +08:00
|
|
|
// #877
|
|
|
|
it('should not trigger key iteration when setting existing keys', () => {
|
|
|
|
const map = reactive(new Map())
|
|
|
|
const spy = jest.fn()
|
|
|
|
|
|
|
|
effect(() => {
|
|
|
|
const keys = []
|
|
|
|
for (const key of map.keys()) {
|
|
|
|
keys.push(key)
|
|
|
|
}
|
|
|
|
spy(keys)
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(spy).toHaveBeenCalledTimes(1)
|
|
|
|
expect(spy.mock.calls[0][0]).toMatchObject([])
|
|
|
|
|
|
|
|
map.set('a', 0)
|
|
|
|
expect(spy).toHaveBeenCalledTimes(2)
|
|
|
|
expect(spy.mock.calls[1][0]).toMatchObject(['a'])
|
|
|
|
|
|
|
|
map.set('b', 0)
|
|
|
|
expect(spy).toHaveBeenCalledTimes(3)
|
|
|
|
expect(spy.mock.calls[2][0]).toMatchObject(['a', 'b'])
|
|
|
|
|
|
|
|
// keys didn't change, should not trigger
|
|
|
|
map.set('b', 1)
|
|
|
|
expect(spy).toHaveBeenCalledTimes(3)
|
|
|
|
})
|
2020-08-26 23:41:18 +08:00
|
|
|
|
2020-09-15 09:31:04 +08:00
|
|
|
it('should trigger Map.has only once for non-reactive keys', () => {
|
|
|
|
const [proxy, spy] = coverCollectionFn(new Map(), 'has')
|
2020-08-26 23:41:18 +08:00
|
|
|
proxy.has('k')
|
2020-09-15 09:31:04 +08:00
|
|
|
expect(spy).toBeCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should trigger Map.set only once for non-reactive keys', () => {
|
|
|
|
const [proxy, spy] = coverCollectionFn(new Map(), 'set')
|
|
|
|
proxy.set('k', 'v')
|
|
|
|
expect(spy).toBeCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should trigger Map.delete only once for non-reactive keys', () => {
|
|
|
|
const [proxy, spy] = coverCollectionFn(new Map(), 'delete')
|
|
|
|
proxy.delete('foo')
|
|
|
|
expect(spy).toBeCalledTimes(1)
|
|
|
|
})
|
2020-08-26 23:41:18 +08:00
|
|
|
|
2020-09-15 09:31:04 +08:00
|
|
|
it('should trigger Map.clear only once for non-reactive keys', () => {
|
|
|
|
const [proxy, spy] = coverCollectionFn(new Map(), 'clear')
|
|
|
|
proxy.clear()
|
2020-08-26 23:41:18 +08:00
|
|
|
expect(spy).toBeCalledTimes(1)
|
|
|
|
})
|
2018-09-20 23:43:14 +08:00
|
|
|
})
|
|
|
|
})
|