Skip to content

Commit d3c456a

Browse files
committed
fix: improve isReadonly behaviour, close #811, close #812
1 parent b625420 commit d3c456a

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

src/reactivity/readonly.ts

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export function readonly<T extends object>(
4646
): DeepReadonly<UnwrapNestedRefs<T>> {
4747
if (__DEV__ && !isObject(target)) {
4848
warn(`value cannot be made reactive: ${String(target)}`)
49+
} else {
50+
readonlySet.set(target, true)
4951
}
5052
return target as any
5153
}

src/reactivity/ref.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export function createRef<T>(options: RefOption<T>, readonly = false) {
6565
// related issues: #79
6666
const sealed = Object.seal(r)
6767

68-
readonlySet.set(sealed, true)
68+
if (readonly) readonlySet.set(sealed, true)
69+
6970
return sealed
7071
}
7172

test/v3/reactivity/readonly.spec.ts

+46-26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
watch,
88
nextTick,
99
readonly,
10+
isReadonly,
11+
toRaw,
12+
computed,
1013
} from '../../../src'
1114

1215
const Vue = require('vue/dist/vue.common.js')
@@ -292,21 +295,21 @@ describe('reactivity/readonly', () => {
292295
// })
293296
// })
294297

295-
// test('calling reactive on an readonly should return readonly', () => {
296-
// const a = readonly({})
297-
// const b = reactive(a)
298-
// expect(isReadonly(b)).toBe(true)
299-
// // should point to same original
300-
// expect(toRaw(a)).toBe(toRaw(b))
301-
// })
298+
test('calling reactive on an readonly should return readonly', () => {
299+
const a = readonly({})
300+
const b = reactive(a)
301+
expect(isReadonly(b)).toBe(true)
302+
// should point to same original
303+
expect(toRaw(a)).toBe(toRaw(b))
304+
})
302305

303-
// test('calling readonly on a reactive object should return readonly', () => {
304-
// const a = reactive({})
305-
// const b = readonly(a)
306-
// expect(isReadonly(b)).toBe(true)
307-
// // should point to same original
308-
// expect(toRaw(a)).toBe(toRaw(b))
309-
// })
306+
test('calling readonly on a reactive object should return readonly', () => {
307+
const a = reactive({})
308+
const b = readonly(a)
309+
expect(isReadonly(b)).toBe(true)
310+
// should point to same original
311+
expect(toRaw(a)).toBe(toRaw(b))
312+
})
310313

311314
// test('readonly should track and trigger if wrapping reactive original', () => {
312315
// const a = reactive({ n: 1 })
@@ -324,19 +327,19 @@ describe('reactivity/readonly', () => {
324327
// expect(dummy).toBe(2)
325328
// })
326329

327-
// test('wrapping already wrapped value should return same Proxy', () => {
328-
// const original = { foo: 1 }
329-
// const wrapped = readonly(original)
330-
// const wrapped2 = readonly(wrapped)
331-
// expect(wrapped2).toBe(wrapped)
332-
// })
330+
test('wrapping already wrapped value should return same Proxy', () => {
331+
const original = { foo: 1 }
332+
const wrapped = readonly(original)
333+
const wrapped2 = readonly(wrapped)
334+
expect(wrapped2).toBe(wrapped)
335+
})
333336

334-
// test('wrapping the same value multiple times should return same Proxy', () => {
335-
// const original = { foo: 1 }
336-
// const wrapped = readonly(original)
337-
// const wrapped2 = readonly(original)
338-
// expect(wrapped2).toBe(wrapped)
339-
// })
337+
test('wrapping the same value multiple times should return same Proxy', () => {
338+
const original = { foo: 1 }
339+
const wrapped = readonly(original)
340+
const wrapped2 = readonly(original)
341+
expect(wrapped2).toBe(wrapped)
342+
})
340343

341344
// test('markRaw', () => {
342345
// const obj = readonly({
@@ -474,5 +477,22 @@ describe('reactivity/readonly', () => {
474477
).toHaveBeenWarned()
475478
expect(vm.$el.textContent).toBe(`1`)
476479
})
480+
481+
it('should mark computed as readonly', () => {
482+
expect(isReadonly(computed(() => {}))).toBe(true)
483+
expect(
484+
isReadonly(
485+
computed({
486+
get: () => {},
487+
set: () => {},
488+
})
489+
)
490+
).toBe(false)
491+
})
492+
493+
// #811
494+
it('should not mark ref as readonly', () => {
495+
expect(isReadonly(ref([]))).toBe(false)
496+
})
477497
})
478498
})

0 commit comments

Comments
 (0)