Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/reactivity/__tests__/readonly.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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