2018-09-21 04:18:22 +08:00
|
|
|
import {
|
2019-08-16 21:42:46 +08:00
|
|
|
reactive,
|
2019-08-23 21:38:32 +08:00
|
|
|
readonly,
|
2019-06-11 23:50:28 +08:00
|
|
|
toRaw,
|
2019-08-16 21:42:46 +08:00
|
|
|
isReactive,
|
2019-08-23 21:38:32 +08:00
|
|
|
isReadonly,
|
2020-04-16 04:45:20 +08:00
|
|
|
markRaw,
|
2019-08-22 00:03:35 +08:00
|
|
|
effect,
|
2019-11-07 01:51:06 +08:00
|
|
|
ref,
|
2020-04-16 04:45:20 +08:00
|
|
|
shallowReadonly,
|
|
|
|
isProxy
|
2018-09-21 04:18:22 +08:00
|
|
|
} from '../src'
|
2020-01-22 22:29:35 +08:00
|
|
|
import { mockWarn } from '@vue/shared'
|
2018-09-21 04:18:22 +08:00
|
|
|
|
2019-10-06 23:26:33 +08:00
|
|
|
/**
|
|
|
|
* @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html
|
|
|
|
*/
|
|
|
|
type Writable<T> = { -readonly [P in keyof T]: T[P] }
|
|
|
|
|
2019-08-23 21:38:32 +08:00
|
|
|
describe('reactivity/readonly', () => {
|
2019-08-27 04:08:23 +08:00
|
|
|
mockWarn()
|
2018-09-21 04:18:22 +08:00
|
|
|
|
|
|
|
describe('Object', () => {
|
2019-08-23 21:38:32 +08:00
|
|
|
it('should make nested values readonly', () => {
|
2018-09-21 04:18:22 +08:00
|
|
|
const original = { foo: 1, bar: { baz: 2 } }
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped = readonly(original)
|
|
|
|
expect(wrapped).not.toBe(original)
|
|
|
|
expect(isProxy(wrapped)).toBe(true)
|
|
|
|
expect(isReactive(wrapped)).toBe(false)
|
|
|
|
expect(isReadonly(wrapped)).toBe(true)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(original)).toBe(false)
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(original)).toBe(false)
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(isReactive(wrapped.bar)).toBe(false)
|
|
|
|
expect(isReadonly(wrapped.bar)).toBe(true)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(original.bar)).toBe(false)
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(original.bar)).toBe(false)
|
2018-09-21 04:18:22 +08:00
|
|
|
// get
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(wrapped.foo).toBe(1)
|
2018-09-21 04:18:22 +08:00
|
|
|
// has
|
2020-04-16 04:45:20 +08:00
|
|
|
expect('foo' in wrapped).toBe(true)
|
2018-09-21 04:18:22 +08:00
|
|
|
// ownKeys
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(Object.keys(wrapped)).toEqual(['foo', 'bar'])
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not allow mutation', () => {
|
2019-10-06 23:26:33 +08:00
|
|
|
const qux = Symbol('qux')
|
|
|
|
const original = {
|
|
|
|
foo: 1,
|
|
|
|
bar: {
|
|
|
|
baz: 2
|
|
|
|
},
|
|
|
|
[qux]: 3
|
|
|
|
}
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped: Writable<typeof original> = readonly(original)
|
2019-10-06 23:26:33 +08:00
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped.foo = 2
|
|
|
|
expect(wrapped.foo).toBe(1)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Set operation on key "foo" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarnedLast()
|
2019-10-06 23:26:33 +08:00
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped.bar.baz = 3
|
|
|
|
expect(wrapped.bar.baz).toBe(2)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Set operation on key "baz" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarnedLast()
|
2019-10-06 23:26:33 +08:00
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped[qux] = 4
|
|
|
|
expect(wrapped[qux]).toBe(3)
|
2019-10-06 23:26:33 +08:00
|
|
|
expect(
|
|
|
|
`Set operation on key "Symbol(qux)" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarnedLast()
|
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
delete wrapped.foo
|
|
|
|
expect(wrapped.foo).toBe(1)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Delete operation on key "foo" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarnedLast()
|
2019-10-06 23:26:33 +08:00
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
delete wrapped.bar.baz
|
|
|
|
expect(wrapped.bar.baz).toBe(2)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Delete operation on key "baz" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarnedLast()
|
2019-10-06 23:26:33 +08:00
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
delete wrapped[qux]
|
|
|
|
expect(wrapped[qux]).toBe(3)
|
2019-10-06 23:26:33 +08:00
|
|
|
expect(
|
|
|
|
`Delete operation on key "Symbol(qux)" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarnedLast()
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2020-04-15 11:49:46 +08:00
|
|
|
it('should not trigger effects', () => {
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped: any = readonly({ a: 1 })
|
2018-09-21 04:18:22 +08:00
|
|
|
let dummy
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2020-04-16 04:45:20 +08:00
|
|
|
dummy = wrapped.a
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped.a = 2
|
|
|
|
expect(wrapped.a).toBe(1)
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(1)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(`target is readonly`).toHaveBeenWarned()
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Array', () => {
|
2019-08-23 21:38:32 +08:00
|
|
|
it('should make nested values readonly', () => {
|
2019-10-05 22:39:40 +08:00
|
|
|
const original = [{ foo: 1 }]
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped = readonly(original)
|
|
|
|
expect(wrapped).not.toBe(original)
|
|
|
|
expect(isProxy(wrapped)).toBe(true)
|
|
|
|
expect(isReactive(wrapped)).toBe(false)
|
|
|
|
expect(isReadonly(wrapped)).toBe(true)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(original)).toBe(false)
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(original)).toBe(false)
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(isReactive(wrapped[0])).toBe(false)
|
|
|
|
expect(isReadonly(wrapped[0])).toBe(true)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(original[0])).toBe(false)
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(original[0])).toBe(false)
|
2018-09-21 04:18:22 +08:00
|
|
|
// get
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(wrapped[0].foo).toBe(1)
|
2018-09-21 04:18:22 +08:00
|
|
|
// has
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(0 in wrapped).toBe(true)
|
2018-09-21 04:18:22 +08:00
|
|
|
// ownKeys
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(Object.keys(wrapped)).toEqual(['0'])
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not allow mutation', () => {
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped: any = readonly([{ foo: 1 }])
|
|
|
|
wrapped[0] = 1
|
|
|
|
expect(wrapped[0]).not.toBe(1)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Set operation on key "0" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarned()
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped[0].foo = 2
|
|
|
|
expect(wrapped[0].foo).toBe(1)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Set operation on key "foo" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarned()
|
2018-09-21 04:18:22 +08:00
|
|
|
|
|
|
|
// should block length mutation
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped.length = 0
|
|
|
|
expect(wrapped.length).toBe(1)
|
|
|
|
expect(wrapped[0].foo).toBe(1)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Set operation on key "length" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarned()
|
2018-09-21 04:18:22 +08:00
|
|
|
|
|
|
|
// mutation methods invoke set/length internally and thus are blocked as well
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped.push(2)
|
|
|
|
expect(wrapped.length).toBe(1)
|
2018-09-21 04:18:22 +08:00
|
|
|
// push triggers two warnings on [1] and .length
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(`target is readonly.`).toHaveBeenWarnedTimes(5)
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2020-04-15 11:49:46 +08:00
|
|
|
it('should not trigger effects', () => {
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped: any = readonly([{ a: 1 }])
|
2018-09-21 04:18:22 +08:00
|
|
|
let dummy
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2020-04-16 04:45:20 +08:00
|
|
|
dummy = wrapped[0].a
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped[0].a = 2
|
|
|
|
expect(wrapped[0].a).toBe(1)
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(1)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(`target is readonly`).toHaveBeenWarnedTimes(1)
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped[0] = { a: 2 }
|
|
|
|
expect(wrapped[0].a).toBe(1)
|
2018-09-21 04:18:22 +08:00
|
|
|
expect(dummy).toBe(1)
|
2019-09-04 06:11:04 +08:00
|
|
|
expect(`target is readonly`).toHaveBeenWarnedTimes(2)
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
})
|
2018-09-21 21:52:46 +08:00
|
|
|
|
|
|
|
const maps = [Map, WeakMap]
|
|
|
|
maps.forEach((Collection: any) => {
|
2018-09-21 04:18:22 +08:00
|
|
|
describe(Collection.name, () => {
|
2019-08-23 21:38:32 +08:00
|
|
|
test('should make nested values readonly', () => {
|
2018-09-21 04:18:22 +08:00
|
|
|
const key1 = {}
|
|
|
|
const key2 = {}
|
|
|
|
const original = new Collection([[key1, {}], [key2, {}]])
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped = readonly(original)
|
|
|
|
expect(wrapped).not.toBe(original)
|
|
|
|
expect(isProxy(wrapped)).toBe(true)
|
|
|
|
expect(isReactive(wrapped)).toBe(false)
|
|
|
|
expect(isReadonly(wrapped)).toBe(true)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(original)).toBe(false)
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(original)).toBe(false)
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(isReactive(wrapped.get(key1))).toBe(false)
|
|
|
|
expect(isReadonly(wrapped.get(key1))).toBe(true)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(original.get(key1))).toBe(false)
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(original.get(key1))).toBe(false)
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2018-11-14 00:03:35 +08:00
|
|
|
test('should not allow mutation & not trigger effect', () => {
|
2019-08-23 21:38:32 +08:00
|
|
|
const map = readonly(new Collection())
|
2018-09-21 04:18:22 +08:00
|
|
|
const key = {}
|
|
|
|
let dummy
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy = map.get(key)
|
|
|
|
})
|
|
|
|
expect(dummy).toBeUndefined()
|
|
|
|
map.set(key, 1)
|
|
|
|
expect(dummy).toBeUndefined()
|
|
|
|
expect(map.has(key)).toBe(false)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Set operation on key "${key}" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarned()
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2018-09-22 04:54:12 +08:00
|
|
|
if (Collection === Map) {
|
2019-10-05 22:48:54 +08:00
|
|
|
test('should retrieve readonly values on iteration', () => {
|
2018-09-22 04:54:12 +08:00
|
|
|
const key1 = {}
|
|
|
|
const key2 = {}
|
|
|
|
const original = new Collection([[key1, {}], [key2, {}]])
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped: any = readonly(original)
|
2020-06-12 04:55:56 +08:00
|
|
|
expect(wrapped.size).toBe(2)
|
2020-04-16 04:45:20 +08:00
|
|
|
for (const [key, value] of wrapped) {
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(key)).toBe(true)
|
|
|
|
expect(isReadonly(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
}
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped.forEach((value: any) => {
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
})
|
2020-04-16 04:45:20 +08:00
|
|
|
for (const value of wrapped.values()) {
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
})
|
2018-09-21 21:52:46 +08:00
|
|
|
|
|
|
|
const sets = [Set, WeakSet]
|
|
|
|
sets.forEach((Collection: any) => {
|
2018-09-21 04:18:22 +08:00
|
|
|
describe(Collection.name, () => {
|
2019-08-23 21:38:32 +08:00
|
|
|
test('should make nested values readonly', () => {
|
2018-09-21 04:18:22 +08:00
|
|
|
const key1 = {}
|
|
|
|
const key2 = {}
|
|
|
|
const original = new Collection([key1, key2])
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped = readonly(original)
|
|
|
|
expect(wrapped).not.toBe(original)
|
|
|
|
expect(isProxy(wrapped)).toBe(true)
|
|
|
|
expect(isReactive(wrapped)).toBe(false)
|
|
|
|
expect(isReadonly(wrapped)).toBe(true)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(original)).toBe(false)
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(original)).toBe(false)
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(wrapped.has(reactive(key1))).toBe(true)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(original.has(reactive(key1))).toBe(false)
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2018-11-14 00:03:35 +08:00
|
|
|
test('should not allow mutation & not trigger effect', () => {
|
2019-08-23 21:38:32 +08:00
|
|
|
const set = readonly(new Collection())
|
2018-09-21 04:18:22 +08:00
|
|
|
const key = {}
|
|
|
|
let dummy
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2018-09-21 04:18:22 +08:00
|
|
|
dummy = set.has(key)
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
set.add(key)
|
|
|
|
expect(dummy).toBe(false)
|
|
|
|
expect(set.has(key)).toBe(false)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Add operation on key "${key}" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarned()
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2018-09-22 04:54:12 +08:00
|
|
|
if (Collection === Set) {
|
2019-10-05 22:48:54 +08:00
|
|
|
test('should retrieve readonly values on iteration', () => {
|
2018-09-22 04:54:12 +08:00
|
|
|
const original = new Collection([{}, {}])
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped: any = readonly(original)
|
2020-06-12 04:55:56 +08:00
|
|
|
expect(wrapped.size).toBe(2)
|
2020-04-16 04:45:20 +08:00
|
|
|
for (const value of wrapped) {
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
}
|
2020-04-16 04:45:20 +08:00
|
|
|
wrapped.forEach((value: any) => {
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
})
|
2020-04-16 04:45:20 +08:00
|
|
|
for (const value of wrapped.values()) {
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(value)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
}
|
2020-04-16 04:45:20 +08:00
|
|
|
for (const [v1, v2] of wrapped.entries()) {
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(v1)).toBe(true)
|
|
|
|
expect(isReadonly(v2)).toBe(true)
|
2018-09-22 04:54:12 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-08-23 21:38:32 +08:00
|
|
|
test('calling reactive on an readonly should return readonly', () => {
|
|
|
|
const a = readonly({})
|
2019-08-16 21:42:46 +08:00
|
|
|
const b = reactive(a)
|
2019-08-23 21:38:32 +08:00
|
|
|
expect(isReadonly(b)).toBe(true)
|
2018-09-21 04:18:22 +08:00
|
|
|
// should point to same original
|
2019-06-11 23:50:28 +08:00
|
|
|
expect(toRaw(a)).toBe(toRaw(b))
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2019-08-23 21:38:32 +08:00
|
|
|
test('calling readonly on a reactive object should return readonly', () => {
|
2019-08-20 21:58:10 +08:00
|
|
|
const a = reactive({})
|
2019-08-23 21:38:32 +08:00
|
|
|
const b = readonly(a)
|
|
|
|
expect(isReadonly(b)).toBe(true)
|
2018-09-21 04:18:22 +08:00
|
|
|
// should point to same original
|
2019-06-11 23:50:28 +08:00
|
|
|
expect(toRaw(a)).toBe(toRaw(b))
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2020-04-15 11:49:46 +08:00
|
|
|
test('readonly should track and trigger if wrapping reactive original', () => {
|
|
|
|
const a = reactive({ n: 1 })
|
|
|
|
const b = readonly(a)
|
2020-04-16 04:45:20 +08:00
|
|
|
// should return true since it's wrapping a reactive source
|
|
|
|
expect(isReactive(b)).toBe(true)
|
|
|
|
|
2020-04-15 11:49:46 +08:00
|
|
|
let dummy
|
|
|
|
effect(() => {
|
|
|
|
dummy = b.n
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
a.n++
|
|
|
|
expect(b.n).toBe(2)
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
})
|
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
test('wrapping already wrapped value should return same Proxy', () => {
|
2018-09-21 04:18:22 +08:00
|
|
|
const original = { foo: 1 }
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped = readonly(original)
|
|
|
|
const wrapped2 = readonly(wrapped)
|
|
|
|
expect(wrapped2).toBe(wrapped)
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
test('wrapping the same value multiple times should return same Proxy', () => {
|
2018-09-21 04:18:22 +08:00
|
|
|
const original = { foo: 1 }
|
2020-04-16 04:45:20 +08:00
|
|
|
const wrapped = readonly(original)
|
|
|
|
const wrapped2 = readonly(original)
|
|
|
|
expect(wrapped2).toBe(wrapped)
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
test('markRaw', () => {
|
2019-08-23 21:38:32 +08:00
|
|
|
const obj = readonly({
|
2018-09-21 04:18:22 +08:00
|
|
|
foo: { a: 1 },
|
2020-04-16 04:45:20 +08:00
|
|
|
bar: markRaw({ b: 2 })
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
2020-04-16 04:45:20 +08:00
|
|
|
expect(isReadonly(obj.foo)).toBe(true)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(obj.bar)).toBe(false)
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|
|
|
|
|
2019-08-23 21:38:32 +08:00
|
|
|
test('should make ref readonly', () => {
|
|
|
|
const n: any = readonly(ref(1))
|
2019-08-22 00:03:35 +08:00
|
|
|
n.value = 2
|
|
|
|
expect(n.value).toBe(1)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`Set operation on key "value" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarned()
|
2019-08-22 00:03:35 +08:00
|
|
|
})
|
2019-11-07 01:51:06 +08:00
|
|
|
|
2019-12-03 03:11:12 +08:00
|
|
|
describe('shallowReadonly', () => {
|
|
|
|
test('should not make non-reactive properties reactive', () => {
|
|
|
|
const props = shallowReadonly({ n: { foo: 1 } })
|
|
|
|
expect(isReactive(props.n)).toBe(false)
|
2019-11-07 01:51:06 +08:00
|
|
|
})
|
|
|
|
|
2019-12-03 03:11:12 +08:00
|
|
|
test('should make root level properties readonly', () => {
|
|
|
|
const props = shallowReadonly({ n: 1 })
|
2019-11-07 01:51:06 +08:00
|
|
|
// @ts-ignore
|
|
|
|
props.n = 2
|
2019-12-03 03:11:12 +08:00
|
|
|
expect(props.n).toBe(1)
|
2019-11-07 01:51:06 +08:00
|
|
|
expect(
|
|
|
|
`Set operation on key "n" failed: target is readonly.`
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
2019-12-03 03:11:12 +08:00
|
|
|
|
|
|
|
// to retain 2.x behavior.
|
|
|
|
test('should NOT make nested properties readonly', () => {
|
|
|
|
const props = shallowReadonly({ n: { foo: 1 } })
|
|
|
|
// @ts-ignore
|
|
|
|
props.n.foo = 2
|
|
|
|
expect(props.n.foo).toBe(2)
|
|
|
|
expect(
|
|
|
|
`Set operation on key "foo" failed: target is readonly.`
|
|
|
|
).not.toHaveBeenWarned()
|
|
|
|
})
|
2019-11-07 01:51:06 +08:00
|
|
|
})
|
2018-09-21 04:18:22 +08:00
|
|
|
})
|