|
@@ -1,6 +1,6 @@
|
|
|
import { reactive, isReactive, toRaw, markNonReactive } from '../src/reactive'
|
|
import { reactive, isReactive, toRaw, markNonReactive } from '../src/reactive'
|
|
|
|
|
|
|
|
-describe('observer/observable', () => {
|
|
|
|
|
|
|
+describe('reactivity/reactive', () => {
|
|
|
test('Object', () => {
|
|
test('Object', () => {
|
|
|
const original = { foo: 1 }
|
|
const original = { foo: 1 }
|
|
|
const observed = reactive(original)
|
|
const observed = reactive(original)
|
|
@@ -30,7 +30,7 @@ describe('observer/observable', () => {
|
|
|
expect(Object.keys(observed)).toEqual(['0'])
|
|
expect(Object.keys(observed)).toEqual(['0'])
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- test('cloned observable Array should point to observed values', () => {
|
|
|
|
|
|
|
+ test('cloned reactive Array should point to observed values', () => {
|
|
|
const original = [{ foo: 1 }]
|
|
const original = [{ foo: 1 }]
|
|
|
const observed = reactive(original)
|
|
const observed = reactive(original)
|
|
|
const clone = observed.slice()
|
|
const clone = observed.slice()
|
|
@@ -39,7 +39,7 @@ describe('observer/observable', () => {
|
|
|
expect(clone[0]).toBe(observed[0])
|
|
expect(clone[0]).toBe(observed[0])
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- test('nested observables', () => {
|
|
|
|
|
|
|
+ test('nested reactives', () => {
|
|
|
const original = {
|
|
const original = {
|
|
|
nested: {
|
|
nested: {
|
|
|
foo: 1
|
|
foo: 1
|
|
@@ -70,9 +70,9 @@ describe('observer/observable', () => {
|
|
|
const observed = reactive(original)
|
|
const observed = reactive(original)
|
|
|
// set
|
|
// set
|
|
|
const value = { baz: 3 }
|
|
const value = { baz: 3 }
|
|
|
- const observableValue = reactive(value)
|
|
|
|
|
|
|
+ const reactiveValue = reactive(value)
|
|
|
observed[0] = value
|
|
observed[0] = value
|
|
|
- expect(observed[0]).toBe(observableValue)
|
|
|
|
|
|
|
+ expect(observed[0]).toBe(reactiveValue)
|
|
|
expect(original[0]).toBe(value)
|
|
expect(original[0]).toBe(value)
|
|
|
// delete
|
|
// delete
|
|
|
delete observed[0]
|
|
delete observed[0]
|
|
@@ -80,11 +80,11 @@ describe('observer/observable', () => {
|
|
|
expect(original[0]).toBeUndefined()
|
|
expect(original[0]).toBeUndefined()
|
|
|
// mutating methods
|
|
// mutating methods
|
|
|
observed.push(value)
|
|
observed.push(value)
|
|
|
- expect(observed[2]).toBe(observableValue)
|
|
|
|
|
|
|
+ expect(observed[2]).toBe(reactiveValue)
|
|
|
expect(original[2]).toBe(value)
|
|
expect(original[2]).toBe(value)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- test('setting a property with an unobserved value should wrap with observable', () => {
|
|
|
|
|
|
|
+ test('setting a property with an unobserved value should wrap with reactive', () => {
|
|
|
const observed: any = reactive({})
|
|
const observed: any = reactive({})
|
|
|
const raw = {}
|
|
const raw = {}
|
|
|
observed.foo = raw
|
|
observed.foo = raw
|
|
@@ -123,14 +123,15 @@ describe('observer/observable', () => {
|
|
|
expect(toRaw(original)).toBe(original)
|
|
expect(toRaw(original)).toBe(original)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- test('unobservable values', () => {
|
|
|
|
|
|
|
+ test('non-observable values', () => {
|
|
|
const warn = jest.spyOn(console, 'warn')
|
|
const warn = jest.spyOn(console, 'warn')
|
|
|
let lastMsg: string
|
|
let lastMsg: string
|
|
|
warn.mockImplementation(msg => {
|
|
warn.mockImplementation(msg => {
|
|
|
lastMsg = msg
|
|
lastMsg = msg
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- const getMsg = (value: any) => `value is not observable: ${String(value)}`
|
|
|
|
|
|
|
+ const getMsg = (value: any) =>
|
|
|
|
|
+ `value cannot be made reactive: ${String(value)}`
|
|
|
const assertValue = (value: any) => {
|
|
const assertValue = (value: any) => {
|
|
|
reactive(value)
|
|
reactive(value)
|
|
|
expect(lastMsg).toMatch(getMsg(value))
|
|
expect(lastMsg).toMatch(getMsg(value))
|
|
@@ -144,10 +145,8 @@ describe('observer/observable', () => {
|
|
|
assertValue(false)
|
|
assertValue(false)
|
|
|
// null
|
|
// null
|
|
|
assertValue(null)
|
|
assertValue(null)
|
|
|
- // undefined should work because it returns empty object observable
|
|
|
|
|
- lastMsg = ''
|
|
|
|
|
- reactive(undefined)
|
|
|
|
|
- expect(lastMsg).toBe('')
|
|
|
|
|
|
|
+ // undefined
|
|
|
|
|
+ assertValue(undefined)
|
|
|
// symbol
|
|
// symbol
|
|
|
const s = Symbol()
|
|
const s = Symbol()
|
|
|
assertValue(s)
|
|
assertValue(s)
|