2018-11-14 00:03:35 +08:00
|
|
|
import { computed, observable, effect, stop } from '../src'
|
2018-09-21 06:03:59 +08:00
|
|
|
|
|
|
|
describe('observer/computed', () => {
|
|
|
|
it('should return updated value', () => {
|
|
|
|
const value: any = observable({})
|
|
|
|
const cValue = computed(() => value.foo)
|
2019-05-29 18:47:29 +08:00
|
|
|
expect(cValue.value).toBe(undefined)
|
2018-09-21 06:03:59 +08:00
|
|
|
value.foo = 1
|
2019-05-29 18:47:29 +08:00
|
|
|
expect(cValue.value).toBe(1)
|
2018-09-21 06:03:59 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should compute lazily', () => {
|
|
|
|
const value: any = observable({})
|
|
|
|
const getter = jest.fn(() => value.foo)
|
|
|
|
const cValue = computed(getter)
|
|
|
|
|
|
|
|
// lazy
|
|
|
|
expect(getter).not.toHaveBeenCalled()
|
|
|
|
|
2019-05-29 18:47:29 +08:00
|
|
|
expect(cValue.value).toBe(undefined)
|
2018-09-21 06:03:59 +08:00
|
|
|
expect(getter).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
// should not compute again
|
2019-05-29 18:47:29 +08:00
|
|
|
cValue.value
|
2018-09-21 06:03:59 +08:00
|
|
|
expect(getter).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
// should not compute until needed
|
|
|
|
value.foo = 1
|
|
|
|
expect(getter).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
// now it should compute
|
2019-05-29 18:47:29 +08:00
|
|
|
expect(cValue.value).toBe(1)
|
2018-09-21 06:03:59 +08:00
|
|
|
expect(getter).toHaveBeenCalledTimes(2)
|
|
|
|
|
|
|
|
// should not compute again
|
2019-05-29 18:47:29 +08:00
|
|
|
cValue.value
|
2018-09-21 06:03:59 +08:00
|
|
|
expect(getter).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should accept context', () => {
|
|
|
|
const value: any = observable({})
|
|
|
|
let callCtx, callArg
|
|
|
|
const getter = function(arg: any) {
|
|
|
|
callCtx = this
|
|
|
|
callArg = arg
|
|
|
|
return value.foo
|
|
|
|
}
|
|
|
|
const ctx = {}
|
|
|
|
const cValue = computed(getter, ctx)
|
2019-05-29 18:47:29 +08:00
|
|
|
cValue.value
|
2018-09-21 06:03:59 +08:00
|
|
|
expect(callCtx).toBe(ctx)
|
|
|
|
expect(callArg).toBe(ctx)
|
|
|
|
})
|
|
|
|
|
2018-11-14 00:03:35 +08:00
|
|
|
it('should trigger effect', () => {
|
2018-09-21 06:03:59 +08:00
|
|
|
const value: any = observable({})
|
|
|
|
const cValue = computed(() => value.foo)
|
|
|
|
let dummy
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2019-05-29 18:47:29 +08:00
|
|
|
dummy = cValue.value
|
2018-09-21 06:03:59 +08:00
|
|
|
})
|
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
value.foo = 1
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should work when chained', () => {
|
|
|
|
const value: any = observable({ foo: 0 })
|
|
|
|
const c1 = computed(() => value.foo)
|
2019-05-29 18:47:29 +08:00
|
|
|
const c2 = computed(() => c1.value + 1)
|
|
|
|
expect(c2.value).toBe(1)
|
|
|
|
expect(c1.value).toBe(0)
|
2018-09-21 06:03:59 +08:00
|
|
|
value.foo++
|
2019-05-29 18:47:29 +08:00
|
|
|
expect(c2.value).toBe(2)
|
|
|
|
expect(c1.value).toBe(1)
|
2018-09-21 06:03:59 +08:00
|
|
|
})
|
|
|
|
|
2018-11-14 00:03:35 +08:00
|
|
|
it('should trigger effect when chained', () => {
|
2018-09-21 06:03:59 +08:00
|
|
|
const value: any = observable({ foo: 0 })
|
|
|
|
const getter1 = jest.fn(() => value.foo)
|
|
|
|
const getter2 = jest.fn(() => {
|
2019-05-29 18:47:29 +08:00
|
|
|
return c1.value + 1
|
2018-09-21 06:03:59 +08:00
|
|
|
})
|
|
|
|
const c1 = computed(getter1)
|
|
|
|
const c2 = computed(getter2)
|
|
|
|
|
|
|
|
let dummy
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2019-05-29 18:47:29 +08:00
|
|
|
dummy = c2.value
|
2018-09-21 06:03:59 +08:00
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
expect(getter1).toHaveBeenCalledTimes(1)
|
|
|
|
expect(getter2).toHaveBeenCalledTimes(1)
|
|
|
|
value.foo++
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
// should not result in duplicate calls
|
|
|
|
expect(getter1).toHaveBeenCalledTimes(2)
|
|
|
|
expect(getter2).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
2018-11-14 00:03:35 +08:00
|
|
|
it('should trigger effect when chained (mixed invocations)', () => {
|
2018-09-21 06:03:59 +08:00
|
|
|
const value: any = observable({ foo: 0 })
|
|
|
|
const getter1 = jest.fn(() => value.foo)
|
|
|
|
const getter2 = jest.fn(() => {
|
2019-05-29 18:47:29 +08:00
|
|
|
return c1.value + 1
|
2018-09-21 06:03:59 +08:00
|
|
|
})
|
|
|
|
const c1 = computed(getter1)
|
|
|
|
const c2 = computed(getter2)
|
|
|
|
|
|
|
|
let dummy
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2019-05-29 18:47:29 +08:00
|
|
|
dummy = c1.value + c2.value
|
2018-09-21 06:03:59 +08:00
|
|
|
})
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
|
|
|
|
expect(getter1).toHaveBeenCalledTimes(1)
|
|
|
|
expect(getter2).toHaveBeenCalledTimes(1)
|
|
|
|
value.foo++
|
|
|
|
expect(dummy).toBe(3)
|
|
|
|
// should not result in duplicate calls
|
|
|
|
expect(getter1).toHaveBeenCalledTimes(2)
|
|
|
|
expect(getter2).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should no longer update when stopped', () => {
|
|
|
|
const value: any = observable({})
|
|
|
|
const cValue = computed(() => value.foo)
|
|
|
|
let dummy
|
2018-11-14 00:03:35 +08:00
|
|
|
effect(() => {
|
2019-05-29 18:47:29 +08:00
|
|
|
dummy = cValue.value
|
2018-09-21 06:03:59 +08:00
|
|
|
})
|
|
|
|
expect(dummy).toBe(undefined)
|
|
|
|
value.foo = 1
|
|
|
|
expect(dummy).toBe(1)
|
2018-11-14 00:03:35 +08:00
|
|
|
stop(cValue.effect)
|
2018-09-21 06:03:59 +08:00
|
|
|
value.foo = 2
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
})
|
|
|
|
})
|