Skip to content

Commit 657476d

Browse files
authored
fix(reactivity): clear method on readonly collections should return undefined (#7316)
1 parent 6e0b068 commit 657476d

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

packages/reactivity/__tests__/readonly.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ describe('reactivity/readonly', () => {
275275
expect(isReactive(value)).toBe(true)
276276
}
277277
})
278+
279+
test('should return undefined from Map.clear() call', () => {
280+
const wrapped = readonly(new Collection())
281+
expect(wrapped.clear()).toBeUndefined()
282+
expect(
283+
`Clear operation failed: target is readonly.`
284+
).toHaveBeenWarned()
285+
})
278286
}
279287
})
280288
})
@@ -332,6 +340,14 @@ describe('reactivity/readonly', () => {
332340
expect(isReadonly(v2)).toBe(true)
333341
}
334342
})
343+
344+
test('should return undefined from Set.clear() call', () => {
345+
const wrapped = readonly(new Collection())
346+
expect(wrapped.clear()).toBeUndefined()
347+
expect(
348+
`Clear operation failed: target is readonly.`
349+
).toHaveBeenWarned()
350+
})
335351
}
336352
})
337353
})

packages/reactivity/__tests__/shallowReadonly.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ describe('reactivity/shallowReadonly', () => {
113113
).not.toHaveBeenWarned()
114114
})
115115
})
116+
117+
test('should return undefined from Map.clear() call', () => {
118+
const sroMap = shallowReadonly(new Map())
119+
expect(sroMap.clear()).toBeUndefined()
120+
expect(`Clear operation failed: target is readonly.`).toHaveBeenWarned()
121+
})
116122
})
117123

118124
describe('collection/Set', () => {
@@ -197,5 +203,11 @@ describe('reactivity/shallowReadonly', () => {
197203
).not.toHaveBeenWarned()
198204
})
199205
})
206+
207+
test('should return undefined from Set.clear() call', () => {
208+
const sroSet = shallowReadonly(new Set())
209+
expect(sroSet.clear()).toBeUndefined()
210+
expect(`Clear operation failed: target is readonly.`).toHaveBeenWarned()
211+
})
200212
})
201213
})

packages/reactivity/src/collectionHandlers.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ function createReadonlyMethod(type: TriggerOpTypes): Function {
223223
toRaw(this)
224224
)
225225
}
226-
return type === TriggerOpTypes.DELETE ? false : this
226+
return type === TriggerOpTypes.DELETE
227+
? false
228+
: type === TriggerOpTypes.CLEAR
229+
? undefined
230+
: this
227231
}
228232
}
229233

0 commit comments

Comments
 (0)