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(mapSpy).toHaveBeenCalledTimes(1)
|
||||
map.set('key', 'value')
|
||||
expect(dummy).toBe('value')
|
||||
map.set('key', undefined)
|
||||
expect(dummy).toBe(undefined)
|
||||
expect(mapSpy).toHaveBeenCalledTimes(2)
|
||||
map.set('key', 'value')
|
||||
expect(dummy).toBe('value')
|
||||
expect(mapSpy).toHaveBeenCalledTimes(2)
|
||||
map.delete('key')
|
||||
expect(dummy).toBe(undefined)
|
||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
||||
map.set('key', 'value')
|
||||
expect(dummy).toBe('value')
|
||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
||||
map.delete('key')
|
||||
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()
|
||||
expect(dummy).toBe(undefined)
|
||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
||||
expect(mapSpy).toHaveBeenCalledTimes(4)
|
||||
})
|
||||
|
||||
it('should not observe raw data', () => {
|
||||
|
@ -46,18 +46,21 @@ describe('reactivity/collections', () => {
|
||||
|
||||
expect(dummy).toBe(undefined)
|
||||
expect(mapSpy).toHaveBeenCalledTimes(1)
|
||||
map.set(key, 'value')
|
||||
expect(dummy).toBe('value')
|
||||
map.set(key, undefined)
|
||||
expect(dummy).toBe(undefined)
|
||||
expect(mapSpy).toHaveBeenCalledTimes(2)
|
||||
map.set(key, 'value')
|
||||
expect(dummy).toBe('value')
|
||||
expect(mapSpy).toHaveBeenCalledTimes(2)
|
||||
map.delete(key)
|
||||
expect(dummy).toBe(undefined)
|
||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
||||
map.set(key, 'value')
|
||||
expect(dummy).toBe('value')
|
||||
expect(mapSpy).toHaveBeenCalledTimes(3)
|
||||
map.delete(key)
|
||||
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', () => {
|
||||
|
@ -68,23 +68,21 @@ function set(this: MapTypes, key: unknown, value: unknown) {
|
||||
const hadKey = proto.has.call(target, key)
|
||||
const oldValue = proto.get.call(target, key)
|
||||
const result = proto.set.call(target, key, value)
|
||||
if (value !== oldValue) {
|
||||
/* istanbul ignore else */
|
||||
if (__DEV__) {
|
||||
const extraInfo = { oldValue, newValue: value }
|
||||
if (!hadKey) {
|
||||
trigger(target, OperationTypes.ADD, key, extraInfo)
|
||||
} else {
|
||||
} else if (value !== oldValue) {
|
||||
trigger(target, OperationTypes.SET, key, extraInfo)
|
||||
}
|
||||
} else {
|
||||
if (!hadKey) {
|
||||
trigger(target, OperationTypes.ADD, key)
|
||||
} else {
|
||||
} else if (value !== oldValue) {
|
||||
trigger(target, OperationTypes.SET, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user