fix(reactivity): should trigger collection's write-function correctly on non-reactive keys (#1992)

This commit is contained in:
Pick
2020-09-15 09:31:04 +08:00
committed by GitHub
parent b2dc95378d
commit fcf9b2cf19
3 changed files with 63 additions and 12 deletions

View File

@@ -1,6 +1,13 @@
import { reactive, effect, isReactive, toRaw } from '../../src'
describe('reactivity/collections', () => {
function coverCollectionFn(collection: Set<any>, fnName: string) {
const spy = jest.fn()
let proxy = reactive(collection)
;(collection as any)[fnName] = spy
return [proxy as any, spy]
}
describe('Set', () => {
it('instanceof', () => {
const original = new Set()
@@ -423,5 +430,29 @@ describe('reactivity/collections', () => {
}, thisArg)
expect(count).toBe(1)
})
it('should trigger Set.has only once for non-reactive keys', () => {
const [proxy, spy] = coverCollectionFn(new Set(), 'has')
proxy.has('foo')
expect(spy).toBeCalledTimes(1)
})
it('should trigger Set.add only once for non-reactive keys', () => {
const [proxy, spy] = coverCollectionFn(new Set(), 'add')
proxy.add('foo')
expect(spy).toBeCalledTimes(1)
})
it('should trigger Set.delete only once for non-reactive keys', () => {
const [proxy, spy] = coverCollectionFn(new Set(), 'delete')
proxy.delete('foo')
expect(spy).toBeCalledTimes(1)
})
it('should trigger Set.clear only once for non-reactive keys', () => {
const [proxy, spy] = coverCollectionFn(new Set(), 'clear')
proxy.clear()
expect(spy).toBeCalledTimes(1)
})
})
})