|
@@ -12,6 +12,7 @@ import {
|
|
|
watchSyncEffect,
|
|
watchSyncEffect,
|
|
|
} from '@vue/runtime-dom'
|
|
} from '@vue/runtime-dom'
|
|
|
import { renderEffect, template } from '../src'
|
|
import { renderEffect, template } from '../src'
|
|
|
|
|
+import { RenderEffect } from '../src/renderEffect'
|
|
|
import { onEffectCleanup } from '@vue/reactivity'
|
|
import { onEffectCleanup } from '@vue/reactivity'
|
|
|
import { makeRender } from './_utils'
|
|
import { makeRender } from './_utils'
|
|
|
|
|
|
|
@@ -34,6 +35,82 @@ const createDemo = (setupFn: () => any, renderFn: (ctx: any) => any) =>
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
describe('renderEffect', () => {
|
|
describe('renderEffect', () => {
|
|
|
|
|
+ test('initializes noLifecycle effect with raw effect function', () => {
|
|
|
|
|
+ let calls = 0
|
|
|
|
|
+ const fn = () => {
|
|
|
|
|
+ calls++
|
|
|
|
|
+ }
|
|
|
|
|
+ const effect = new RenderEffect(fn, true)
|
|
|
|
|
+
|
|
|
|
|
+ expect(effect.fn).toBe(fn)
|
|
|
|
|
+ expect(effect.updateJob).toBe(undefined)
|
|
|
|
|
+
|
|
|
|
|
+ effect.run()
|
|
|
|
|
+ expect(calls).toBe(1)
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ test('creates update lifecycle job lazily', async () => {
|
|
|
|
|
+ const effect = new RenderEffect(() => {})
|
|
|
|
|
+ expect(effect.updateJob).toBe(undefined)
|
|
|
|
|
+
|
|
|
|
|
+ const effects: RenderEffect[] = []
|
|
|
|
|
+ const calls: string[] = []
|
|
|
|
|
+ const { instance } = createDemo(
|
|
|
|
|
+ () => {
|
|
|
|
|
+ const source = ref(0)
|
|
|
|
|
+ const update = () => source.value++
|
|
|
|
|
+ onUpdated(() => calls.push(`updated ${source.value}`))
|
|
|
|
|
+ return { source, update }
|
|
|
|
|
+ },
|
|
|
|
|
+ ctx => {
|
|
|
|
|
+ const effect = new RenderEffect(() => {
|
|
|
|
|
+ calls.push(`render ${ctx.source}`)
|
|
|
|
|
+ })
|
|
|
|
|
+ effects.push(effect)
|
|
|
|
|
+ effect.run()
|
|
|
|
|
+ },
|
|
|
|
|
+ ).render()
|
|
|
|
|
+
|
|
|
|
|
+ expect(effects[0].updateJob).toBe(undefined)
|
|
|
|
|
+ expect(calls).toEqual(['render 0'])
|
|
|
|
|
+
|
|
|
|
|
+ const { update } = instance?.setupState as any
|
|
|
|
|
+ update()
|
|
|
|
|
+ await nextTick()
|
|
|
|
|
+
|
|
|
|
|
+ expect(effects[0].updateJob).toEqual(expect.any(Function))
|
|
|
|
|
+ expect(calls).toEqual(['render 0', 'render 1', 'updated 1'])
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ test('creates update lifecycle job after hooks are registered late', async () => {
|
|
|
|
|
+ const effects: RenderEffect[] = []
|
|
|
|
|
+ const calls: string[] = []
|
|
|
|
|
+ const { instance } = createDemo(
|
|
|
|
|
+ () => {
|
|
|
|
|
+ const source = ref(0)
|
|
|
|
|
+ const update = () => source.value++
|
|
|
|
|
+ const effect = new RenderEffect(() => {
|
|
|
|
|
+ calls.push(`render ${source.value}`)
|
|
|
|
|
+ })
|
|
|
|
|
+ effects.push(effect)
|
|
|
|
|
+ effect.run()
|
|
|
|
|
+ onUpdated(() => calls.push(`updated ${source.value}`))
|
|
|
|
|
+ return { update }
|
|
|
|
|
+ },
|
|
|
|
|
+ () => {},
|
|
|
|
|
+ ).render()
|
|
|
|
|
+
|
|
|
|
|
+ expect(effects[0].updateJob).toBe(undefined)
|
|
|
|
|
+ expect(calls).toEqual(['render 0'])
|
|
|
|
|
+
|
|
|
|
|
+ const { update } = instance?.setupState as any
|
|
|
|
|
+ update()
|
|
|
|
|
+ await nextTick()
|
|
|
|
|
+
|
|
|
|
|
+ expect(effects[0].updateJob).toEqual(expect.any(Function))
|
|
|
|
|
+ expect(calls).toEqual(['render 0', 'render 1', 'updated 1'])
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
test('basic', async () => {
|
|
test('basic', async () => {
|
|
|
let dummy: any
|
|
let dummy: any
|
|
|
const source = ref(0)
|
|
const source = ref(0)
|