Skip to content

Commit

Permalink
fix(reactivity): clear method on readonly collections should return u…
Browse files Browse the repository at this point in the history
…ndefined (#7316)
  • Loading branch information
wsypower committed Nov 9, 2023
1 parent 6e0b068 commit 657476d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/reactivity/__tests__/readonly.spec.ts
Expand Up @@ -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()
})
}
})
})
Expand Down Expand Up @@ -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()
})
}
})
})
Expand Down
12 changes: 12 additions & 0 deletions packages/reactivity/__tests__/shallowReadonly.spec.ts
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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()
})
})
})
6 changes: 5 additions & 1 deletion packages/reactivity/src/collectionHandlers.ts
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 657476d

Please sign in to comment.