2019-10-10 23:34:42 +08:00
|
|
|
import { ref, isRef } from '../src/ref'
|
2020-06-10 05:20:30 +08:00
|
|
|
import { reactive, isReactive, toRaw, markRaw } from '../src/reactive'
|
2020-01-22 22:29:35 +08:00
|
|
|
import { mockWarn } from '@vue/shared'
|
2019-10-14 23:21:09 +08:00
|
|
|
import { computed } from '../src/computed'
|
2018-09-20 05:45:19 +08:00
|
|
|
|
2019-08-20 21:58:10 +08:00
|
|
|
describe('reactivity/reactive', () => {
|
2019-08-27 04:08:23 +08:00
|
|
|
mockWarn()
|
|
|
|
|
2018-09-20 05:45:19 +08:00
|
|
|
test('Object', () => {
|
|
|
|
const original = { foo: 1 }
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
2018-09-20 05:45:19 +08:00
|
|
|
expect(observed).not.toBe(original)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(observed)).toBe(true)
|
|
|
|
expect(isReactive(original)).toBe(false)
|
2018-09-20 05:45:19 +08:00
|
|
|
// get
|
|
|
|
expect(observed.foo).toBe(1)
|
|
|
|
// has
|
|
|
|
expect('foo' in observed).toBe(true)
|
|
|
|
// ownKeys
|
|
|
|
expect(Object.keys(observed)).toEqual(['foo'])
|
|
|
|
})
|
2020-05-03 22:49:23 +08:00
|
|
|
|
|
|
|
test('proto', () => {
|
|
|
|
const obj = {}
|
|
|
|
const reactiveObj = reactive(obj)
|
|
|
|
expect(isReactive(reactiveObj)).toBe(true)
|
|
|
|
// read prop of reactiveObject will cause reactiveObj[prop] to be reactive
|
|
|
|
// @ts-ignore
|
|
|
|
const prototype = reactiveObj['__proto__']
|
|
|
|
const otherObj = { data: ['a'] }
|
|
|
|
expect(isReactive(otherObj)).toBe(false)
|
|
|
|
const reactiveOther = reactive(otherObj)
|
|
|
|
expect(isReactive(reactiveOther)).toBe(true)
|
|
|
|
expect(reactiveOther.data[0]).toBe('a')
|
|
|
|
})
|
2018-09-20 05:45:19 +08:00
|
|
|
|
2019-08-20 21:58:10 +08:00
|
|
|
test('nested reactives', () => {
|
2018-09-20 05:45:19 +08:00
|
|
|
const original = {
|
|
|
|
nested: {
|
|
|
|
foo: 1
|
|
|
|
},
|
|
|
|
array: [{ bar: 2 }]
|
|
|
|
}
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
|
|
|
expect(isReactive(observed.nested)).toBe(true)
|
|
|
|
expect(isReactive(observed.array)).toBe(true)
|
|
|
|
expect(isReactive(observed.array[0])).toBe(true)
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('observed value should proxy mutations to original (Object)', () => {
|
|
|
|
const original: any = { foo: 1 }
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
2018-09-20 05:45:19 +08:00
|
|
|
// set
|
|
|
|
observed.bar = 1
|
|
|
|
expect(observed.bar).toBe(1)
|
|
|
|
expect(original.bar).toBe(1)
|
|
|
|
// delete
|
|
|
|
delete observed.foo
|
|
|
|
expect('foo' in observed).toBe(false)
|
|
|
|
expect('foo' in original).toBe(false)
|
|
|
|
})
|
|
|
|
|
2019-08-20 21:58:10 +08:00
|
|
|
test('setting a property with an unobserved value should wrap with reactive', () => {
|
2019-10-05 22:39:40 +08:00
|
|
|
const observed = reactive<{ foo?: object }>({})
|
2018-09-20 05:45:19 +08:00
|
|
|
const raw = {}
|
|
|
|
observed.foo = raw
|
|
|
|
expect(observed.foo).not.toBe(raw)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(observed.foo)).toBe(true)
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('observing already observed value should return same Proxy', () => {
|
|
|
|
const original = { foo: 1 }
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
|
|
|
const observed2 = reactive(observed)
|
2018-09-20 05:45:19 +08:00
|
|
|
expect(observed2).toBe(observed)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('observing the same value multiple times should return same Proxy', () => {
|
|
|
|
const original = { foo: 1 }
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
|
|
|
const observed2 = reactive(original)
|
2018-09-20 05:45:19 +08:00
|
|
|
expect(observed2).toBe(observed)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should not pollute original object with Proxies', () => {
|
|
|
|
const original: any = { foo: 1 }
|
|
|
|
const original2 = { bar: 2 }
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
|
|
|
const observed2 = reactive(original2)
|
2018-09-20 05:45:19 +08:00
|
|
|
observed.bar = observed2
|
|
|
|
expect(observed.bar).toBe(observed2)
|
|
|
|
expect(original.bar).toBe(original2)
|
|
|
|
})
|
|
|
|
|
2020-06-12 03:12:57 +08:00
|
|
|
test('toRaw', () => {
|
2018-09-20 05:45:19 +08:00
|
|
|
const original = { foo: 1 }
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
2019-06-11 23:50:28 +08:00
|
|
|
expect(toRaw(observed)).toBe(original)
|
|
|
|
expect(toRaw(original)).toBe(original)
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|
|
|
|
|
2020-06-12 03:12:57 +08:00
|
|
|
test('toRaw on object using reactive as prototype', () => {
|
|
|
|
const original = reactive({})
|
|
|
|
const obj = Object.create(original)
|
|
|
|
const raw = toRaw(obj)
|
|
|
|
expect(raw).toBe(obj)
|
|
|
|
expect(raw).not.toBe(toRaw(original))
|
|
|
|
})
|
|
|
|
|
2019-10-10 23:34:42 +08:00
|
|
|
test('should not unwrap Ref<T>', () => {
|
|
|
|
const observedNumberRef = reactive(ref(1))
|
|
|
|
const observedObjectRef = reactive(ref({ foo: 1 }))
|
|
|
|
|
|
|
|
expect(isRef(observedNumberRef)).toBe(true)
|
|
|
|
expect(isRef(observedObjectRef)).toBe(true)
|
|
|
|
})
|
|
|
|
|
2019-10-14 23:21:09 +08:00
|
|
|
test('should unwrap computed refs', () => {
|
|
|
|
// readonly
|
|
|
|
const a = computed(() => 1)
|
|
|
|
// writable
|
|
|
|
const b = computed({
|
|
|
|
get: () => 1,
|
|
|
|
set: () => {}
|
|
|
|
})
|
|
|
|
const obj = reactive({ a, b })
|
|
|
|
// check type
|
|
|
|
obj.a + 1
|
|
|
|
obj.b + 1
|
|
|
|
expect(typeof obj.a).toBe(`number`)
|
|
|
|
expect(typeof obj.b).toBe(`number`)
|
|
|
|
})
|
|
|
|
|
2019-08-20 21:58:10 +08:00
|
|
|
test('non-observable values', () => {
|
2018-10-02 06:40:44 +08:00
|
|
|
const assertValue = (value: any) => {
|
2019-08-16 21:42:46 +08:00
|
|
|
reactive(value)
|
2019-08-27 04:08:23 +08:00
|
|
|
expect(
|
|
|
|
`value cannot be made reactive: ${String(value)}`
|
|
|
|
).toHaveBeenWarnedLast()
|
2018-10-02 06:40:44 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 05:45:19 +08:00
|
|
|
// number
|
2018-10-02 06:40:44 +08:00
|
|
|
assertValue(1)
|
2018-09-20 05:45:19 +08:00
|
|
|
// string
|
2018-10-02 06:40:44 +08:00
|
|
|
assertValue('foo')
|
2018-09-20 05:45:19 +08:00
|
|
|
// boolean
|
2018-10-02 06:40:44 +08:00
|
|
|
assertValue(false)
|
2018-09-20 05:45:19 +08:00
|
|
|
// null
|
2018-10-02 06:40:44 +08:00
|
|
|
assertValue(null)
|
2019-08-20 21:58:10 +08:00
|
|
|
// undefined
|
|
|
|
assertValue(undefined)
|
2018-09-20 05:45:19 +08:00
|
|
|
// symbol
|
|
|
|
const s = Symbol()
|
2018-10-02 06:40:44 +08:00
|
|
|
assertValue(s)
|
|
|
|
|
2018-09-20 05:45:19 +08:00
|
|
|
// built-ins should work and return same value
|
|
|
|
const p = Promise.resolve()
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(reactive(p)).toBe(p)
|
2018-09-20 09:50:19 +08:00
|
|
|
const r = new RegExp('')
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(reactive(r)).toBe(r)
|
2018-09-20 09:50:19 +08:00
|
|
|
const d = new Date()
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(reactive(d)).toBe(d)
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|
|
|
|
|
2020-04-16 04:45:20 +08:00
|
|
|
test('markRaw', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const obj = reactive({
|
2018-09-20 05:45:19 +08:00
|
|
|
foo: { a: 1 },
|
2020-04-16 04:45:20 +08:00
|
|
|
bar: markRaw({ b: 2 })
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(obj.foo)).toBe(true)
|
|
|
|
expect(isReactive(obj.bar)).toBe(false)
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|
2020-02-04 23:15:27 +08:00
|
|
|
|
2020-03-23 23:28:20 +08:00
|
|
|
test('should not observe frozen objects', () => {
|
|
|
|
const obj = reactive({
|
|
|
|
foo: Object.freeze({ a: 1 })
|
|
|
|
})
|
|
|
|
expect(isReactive(obj.foo)).toBe(false)
|
|
|
|
})
|
2018-09-20 05:45:19 +08:00
|
|
|
})
|