fix(reactivity): clear method on readonly collections should return undefined (#7316)

This commit is contained in:
2023-11-09 17:32:21 +08:00 committed by GitHub
parent 6e0b068e92
commit 657476dcdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View File

@ -275,6 +275,14 @@ describe('reactivity/readonly', () => {
expect(isReactive(value)).toBe(true)
}
})
test('should return undefined from Map.clear() call', () => {
const wrapped = readonly(new Collection())
expect(wrapped.clear()).toBeUndefined()
expect(
`Clear operation failed: target is readonly.`
).toHaveBeenWarned()
})
}
})
})
@ -332,6 +340,14 @@ describe('reactivity/readonly', () => {
expect(isReadonly(v2)).toBe(true)
}
})
test('should return undefined from Set.clear() call', () => {
const wrapped = readonly(new Collection())
expect(wrapped.clear()).toBeUndefined()
expect(
`Clear operation failed: target is readonly.`
).toHaveBeenWarned()
})
}
})
})

View File

@ -113,6 +113,12 @@ describe('reactivity/shallowReadonly', () => {
).not.toHaveBeenWarned()
})
})
test('should return undefined from Map.clear() call', () => {
const sroMap = shallowReadonly(new Map())
expect(sroMap.clear()).toBeUndefined()
expect(`Clear operation failed: target is readonly.`).toHaveBeenWarned()
})
})
describe('collection/Set', () => {
@ -197,5 +203,11 @@ describe('reactivity/shallowReadonly', () => {
).not.toHaveBeenWarned()
})
})
test('should return undefined from Set.clear() call', () => {
const sroSet = shallowReadonly(new Set())
expect(sroSet.clear()).toBeUndefined()
expect(`Clear operation failed: target is readonly.`).toHaveBeenWarned()
})
})
})

View File

@ -223,7 +223,11 @@ function createReadonlyMethod(type: TriggerOpTypes): Function {
toRaw(this)
)
}
return type === TriggerOpTypes.DELETE ? false : this
return type === TriggerOpTypes.DELETE
? false
: type === TriggerOpTypes.CLEAR
? undefined
: this
}
}