2019-08-16 21:42:46 +08:00
|
|
|
import { reactive, isReactive, toRaw, markNonReactive } from '../src/reactive'
|
2018-09-20 05:45:19 +08:00
|
|
|
|
|
|
|
describe('observer/observable', () => {
|
|
|
|
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'])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Array', () => {
|
|
|
|
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
|
|
|
expect(observed).not.toBe(original)
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(observed)).toBe(true)
|
|
|
|
expect(isReactive(original)).toBe(false)
|
|
|
|
expect(isReactive(observed[0])).toBe(true)
|
2018-09-20 05:45:19 +08:00
|
|
|
// get
|
|
|
|
expect(observed[0].foo).toBe(1)
|
|
|
|
// has
|
|
|
|
expect(0 in observed).toBe(true)
|
|
|
|
// ownKeys
|
|
|
|
expect(Object.keys(observed)).toEqual(['0'])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('cloned observable Array should point to observed values', () => {
|
|
|
|
const original = [{ foo: 1 }]
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
2018-09-20 05:45:19 +08:00
|
|
|
const clone = observed.slice()
|
2019-08-16 21:42:46 +08:00
|
|
|
expect(isReactive(clone[0])).toBe(true)
|
2018-09-20 05:45:19 +08:00
|
|
|
expect(clone[0]).not.toBe(original[0])
|
|
|
|
expect(clone[0]).toBe(observed[0])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('nested observables', () => {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('observed value should proxy mutations to original (Array)', () => {
|
|
|
|
const original: any[] = [{ foo: 1 }, { bar: 2 }]
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed = reactive(original)
|
2018-09-20 05:45:19 +08:00
|
|
|
// set
|
|
|
|
const value = { baz: 3 }
|
2019-08-16 21:42:46 +08:00
|
|
|
const observableValue = reactive(value)
|
2018-09-20 05:45:19 +08:00
|
|
|
observed[0] = value
|
|
|
|
expect(observed[0]).toBe(observableValue)
|
|
|
|
expect(original[0]).toBe(value)
|
|
|
|
// delete
|
|
|
|
delete observed[0]
|
|
|
|
expect(observed[0]).toBeUndefined()
|
|
|
|
expect(original[0]).toBeUndefined()
|
|
|
|
// mutating methods
|
|
|
|
observed.push(value)
|
|
|
|
expect(observed[2]).toBe(observableValue)
|
|
|
|
expect(original[2]).toBe(value)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('setting a property with an unobserved value should wrap with observable', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const observed: any = reactive({})
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('unwrap', () => {
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
test('unobservable values', () => {
|
2018-10-02 06:40:44 +08:00
|
|
|
const warn = jest.spyOn(console, 'warn')
|
|
|
|
let lastMsg: string
|
|
|
|
warn.mockImplementation(msg => {
|
|
|
|
lastMsg = msg
|
|
|
|
})
|
|
|
|
|
|
|
|
const getMsg = (value: any) => `value is not observable: ${String(value)}`
|
|
|
|
const assertValue = (value: any) => {
|
2019-08-16 21:42:46 +08:00
|
|
|
reactive(value)
|
2018-10-02 06:40:44 +08:00
|
|
|
expect(lastMsg).toMatch(getMsg(value))
|
|
|
|
}
|
|
|
|
|
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)
|
2018-09-20 05:45:19 +08:00
|
|
|
// undefined should work because it returns empty object observable
|
2018-10-02 06:40:44 +08:00
|
|
|
lastMsg = ''
|
2019-08-16 21:42:46 +08:00
|
|
|
reactive(undefined)
|
2018-10-02 06:40:44 +08:00
|
|
|
expect(lastMsg).toBe('')
|
2018-09-20 05:45:19 +08:00
|
|
|
// symbol
|
|
|
|
const s = Symbol()
|
2018-10-02 06:40:44 +08:00
|
|
|
assertValue(s)
|
|
|
|
|
|
|
|
warn.mockRestore()
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
test('markNonReactive', () => {
|
2019-08-16 21:42:46 +08:00
|
|
|
const obj = reactive({
|
2018-09-20 05:45:19 +08:00
|
|
|
foo: { a: 1 },
|
|
|
|
bar: markNonReactive({ b: 2 })
|
|
|
|
})
|
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
|
|
|
})
|
|
|
|
})
|