test: fix reactivity tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { reactive, effect, toRaw, isReactive } from '../../src'
|
||||
|
||||
describe('observer/collections', () => {
|
||||
describe('reactivity/collections', () => {
|
||||
describe('Map', () => {
|
||||
test('instanceof', () => {
|
||||
const original = new Map()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { reactive, effect, isReactive, toRaw } from '../../src'
|
||||
|
||||
describe('observer/collections', () => {
|
||||
describe('reactivity/collections', () => {
|
||||
describe('Set', () => {
|
||||
it('instanceof', () => {
|
||||
const original = new Set()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { reactive, effect, toRaw, isReactive } from '../../src'
|
||||
|
||||
describe('observer/collections', () => {
|
||||
describe('reactivity/collections', () => {
|
||||
describe('WeakMap', () => {
|
||||
test('instanceof', () => {
|
||||
const original = new WeakMap()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { reactive, isReactive, effect, toRaw } from '../../src'
|
||||
|
||||
describe('observer/collections', () => {
|
||||
describe('reactivity/collections', () => {
|
||||
describe('WeakSet', () => {
|
||||
it('instanceof', () => {
|
||||
const original = new Set()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { computed, reactive, effect, stop } from '../src'
|
||||
|
||||
describe('observer/computed', () => {
|
||||
describe('reactivity/computed', () => {
|
||||
it('should return updated value', () => {
|
||||
const value: any = reactive({})
|
||||
const cValue = computed(() => value.foo)
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
} from '../src/index'
|
||||
import { ITERATE_KEY } from '../src/effect'
|
||||
|
||||
describe('observer/effect', () => {
|
||||
describe('reactivity/effect', () => {
|
||||
it('should run the passed function once (wrapped by a effect)', () => {
|
||||
const fnSpy = jest.fn(() => {})
|
||||
effect(fnSpy)
|
||||
@@ -269,7 +269,7 @@ describe('observer/effect', () => {
|
||||
|
||||
it('should not observe raw mutations', () => {
|
||||
let dummy
|
||||
const obj: any = reactive()
|
||||
const obj: any = reactive({})
|
||||
effect(() => (dummy = toRaw(obj).prop))
|
||||
|
||||
expect(dummy).toBe(undefined)
|
||||
@@ -279,7 +279,7 @@ describe('observer/effect', () => {
|
||||
|
||||
it('should not be triggered by raw mutations', () => {
|
||||
let dummy
|
||||
const obj: any = reactive()
|
||||
const obj: any = reactive({})
|
||||
effect(() => (dummy = obj.prop))
|
||||
|
||||
expect(dummy).toBe(undefined)
|
||||
@@ -437,7 +437,7 @@ describe('observer/effect', () => {
|
||||
|
||||
it('should not run multiple times for a single mutation', () => {
|
||||
let dummy
|
||||
const obj: any = reactive()
|
||||
const obj: any = reactive({})
|
||||
const fnSpy = jest.fn(() => {
|
||||
for (const key in obj) {
|
||||
dummy = obj[key]
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
effect
|
||||
} from '../src'
|
||||
|
||||
describe('observer/immutable', () => {
|
||||
describe('reactivity/immutable', () => {
|
||||
let warn: any
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -341,16 +341,16 @@ describe('observer/immutable', () => {
|
||||
})
|
||||
})
|
||||
|
||||
test('calling observable on an immutable should return immutable', () => {
|
||||
const a = immutable()
|
||||
test('calling reactive on an immutable should return immutable', () => {
|
||||
const a = immutable({})
|
||||
const b = reactive(a)
|
||||
expect(isImmutable(b)).toBe(true)
|
||||
// should point to same original
|
||||
expect(toRaw(a)).toBe(toRaw(b))
|
||||
})
|
||||
|
||||
test('calling immutable on an observable should return immutable', () => {
|
||||
const a = reactive()
|
||||
test('calling immutable on a reactive object should return immutable', () => {
|
||||
const a = reactive({})
|
||||
const b = immutable(a)
|
||||
expect(isImmutable(b)).toBe(true)
|
||||
// should point to same original
|
||||
@@ -1,6 +1,6 @@
|
||||
import { reactive, isReactive, toRaw, markNonReactive } from '../src/reactive'
|
||||
|
||||
describe('observer/observable', () => {
|
||||
describe('reactivity/reactive', () => {
|
||||
test('Object', () => {
|
||||
const original = { foo: 1 }
|
||||
const observed = reactive(original)
|
||||
@@ -30,7 +30,7 @@ describe('observer/observable', () => {
|
||||
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 observed = reactive(original)
|
||||
const clone = observed.slice()
|
||||
@@ -39,7 +39,7 @@ describe('observer/observable', () => {
|
||||
expect(clone[0]).toBe(observed[0])
|
||||
})
|
||||
|
||||
test('nested observables', () => {
|
||||
test('nested reactives', () => {
|
||||
const original = {
|
||||
nested: {
|
||||
foo: 1
|
||||
@@ -70,9 +70,9 @@ describe('observer/observable', () => {
|
||||
const observed = reactive(original)
|
||||
// set
|
||||
const value = { baz: 3 }
|
||||
const observableValue = reactive(value)
|
||||
const reactiveValue = reactive(value)
|
||||
observed[0] = value
|
||||
expect(observed[0]).toBe(observableValue)
|
||||
expect(observed[0]).toBe(reactiveValue)
|
||||
expect(original[0]).toBe(value)
|
||||
// delete
|
||||
delete observed[0]
|
||||
@@ -80,11 +80,11 @@ describe('observer/observable', () => {
|
||||
expect(original[0]).toBeUndefined()
|
||||
// mutating methods
|
||||
observed.push(value)
|
||||
expect(observed[2]).toBe(observableValue)
|
||||
expect(observed[2]).toBe(reactiveValue)
|
||||
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 raw = {}
|
||||
observed.foo = raw
|
||||
@@ -123,14 +123,15 @@ describe('observer/observable', () => {
|
||||
expect(toRaw(original)).toBe(original)
|
||||
})
|
||||
|
||||
test('unobservable values', () => {
|
||||
test('non-observable values', () => {
|
||||
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 getMsg = (value: any) =>
|
||||
`value cannot be made reactive: ${String(value)}`
|
||||
const assertValue = (value: any) => {
|
||||
reactive(value)
|
||||
expect(lastMsg).toMatch(getMsg(value))
|
||||
@@ -144,10 +145,8 @@ describe('observer/observable', () => {
|
||||
assertValue(false)
|
||||
// null
|
||||
assertValue(null)
|
||||
// undefined should work because it returns empty object observable
|
||||
lastMsg = ''
|
||||
reactive(undefined)
|
||||
expect(lastMsg).toBe('')
|
||||
// undefined
|
||||
assertValue(undefined)
|
||||
// symbol
|
||||
const s = Symbol()
|
||||
assertValue(s)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ref, effect, reactive } from '../src/index'
|
||||
|
||||
describe('observer/value', () => {
|
||||
describe('reactivity/value', () => {
|
||||
it('should hold a value', () => {
|
||||
const a = ref(1)
|
||||
expect(a.value).toBe(1)
|
||||
|
||||
Reference in New Issue
Block a user