fix(reactivity): Map.set should trigger when adding new entry with undefined value (#364)
This commit is contained in:
parent
09c248ae87
commit
246cad7459
@ -185,21 +185,24 @@ describe('reactivity/collections', () => {
|
|||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(1)
|
expect(mapSpy).toHaveBeenCalledTimes(1)
|
||||||
map.set('key', 'value')
|
map.set('key', undefined)
|
||||||
expect(dummy).toBe('value')
|
expect(dummy).toBe(undefined)
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(2)
|
expect(mapSpy).toHaveBeenCalledTimes(2)
|
||||||
map.set('key', 'value')
|
map.set('key', 'value')
|
||||||
expect(dummy).toBe('value')
|
expect(dummy).toBe('value')
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(2)
|
expect(mapSpy).toHaveBeenCalledTimes(3)
|
||||||
map.delete('key')
|
map.set('key', 'value')
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe('value')
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
expect(mapSpy).toHaveBeenCalledTimes(3)
|
||||||
map.delete('key')
|
map.delete('key')
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
||||||
|
map.delete('key')
|
||||||
|
expect(dummy).toBe(undefined)
|
||||||
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
||||||
map.clear()
|
map.clear()
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not observe raw data', () => {
|
it('should not observe raw data', () => {
|
||||||
|
@ -46,18 +46,21 @@ describe('reactivity/collections', () => {
|
|||||||
|
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(1)
|
expect(mapSpy).toHaveBeenCalledTimes(1)
|
||||||
map.set(key, 'value')
|
map.set(key, undefined)
|
||||||
expect(dummy).toBe('value')
|
expect(dummy).toBe(undefined)
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(2)
|
expect(mapSpy).toHaveBeenCalledTimes(2)
|
||||||
map.set(key, 'value')
|
map.set(key, 'value')
|
||||||
expect(dummy).toBe('value')
|
expect(dummy).toBe('value')
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(2)
|
expect(mapSpy).toHaveBeenCalledTimes(3)
|
||||||
map.delete(key)
|
map.set(key, 'value')
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe('value')
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
expect(mapSpy).toHaveBeenCalledTimes(3)
|
||||||
map.delete(key)
|
map.delete(key)
|
||||||
expect(dummy).toBe(undefined)
|
expect(dummy).toBe(undefined)
|
||||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
||||||
|
map.delete(key)
|
||||||
|
expect(dummy).toBe(undefined)
|
||||||
|
expect(mapSpy).toHaveBeenCalledTimes(4)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not observe raw data', () => {
|
it('should not observe raw data', () => {
|
||||||
|
@ -68,21 +68,19 @@ function set(this: MapTypes, key: unknown, value: unknown) {
|
|||||||
const hadKey = proto.has.call(target, key)
|
const hadKey = proto.has.call(target, key)
|
||||||
const oldValue = proto.get.call(target, key)
|
const oldValue = proto.get.call(target, key)
|
||||||
const result = proto.set.call(target, key, value)
|
const result = proto.set.call(target, key, value)
|
||||||
if (value !== oldValue) {
|
/* istanbul ignore else */
|
||||||
/* istanbul ignore else */
|
if (__DEV__) {
|
||||||
if (__DEV__) {
|
const extraInfo = { oldValue, newValue: value }
|
||||||
const extraInfo = { oldValue, newValue: value }
|
if (!hadKey) {
|
||||||
if (!hadKey) {
|
trigger(target, OperationTypes.ADD, key, extraInfo)
|
||||||
trigger(target, OperationTypes.ADD, key, extraInfo)
|
} else if (value !== oldValue) {
|
||||||
} else {
|
trigger(target, OperationTypes.SET, key, extraInfo)
|
||||||
trigger(target, OperationTypes.SET, key, extraInfo)
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
if (!hadKey) {
|
||||||
if (!hadKey) {
|
trigger(target, OperationTypes.ADD, key)
|
||||||
trigger(target, OperationTypes.ADD, key)
|
} else if (value !== oldValue) {
|
||||||
} else {
|
trigger(target, OperationTypes.SET, key)
|
||||||
trigger(target, OperationTypes.SET, key)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
Loading…
Reference in New Issue
Block a user