Skip to content

Commit

Permalink
fix(runtime): fixed hydrate component lifecycle $el is null
Browse files Browse the repository at this point in the history
  • Loading branch information
Linkontoask committed Mar 27, 2023
1 parent d0bf19c commit 79b9920
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/runtime/src/renderer/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function updateComponentEffect(
component.subTree = nextTree as VNode
hydrate(component.vnode.el, component.subTree, component, ssrMessage)

component.$el = component.vnode.el
component.mounted = true
// onAfterMount
invokeLifecycle(component, 'afterMounts')
Expand Down
20 changes: 15 additions & 5 deletions packages/runtime/tests/ssr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { renderToString } from '@gyron/dom-server'
import { noop, sleep } from '@gyron/shared'
import {
createSSRInstance,
createVNode,
h,
nextRender,
Expand All @@ -12,12 +11,13 @@ import {
createRef,
onBeforeMount,
createSSRContext,
onAfterMount,
} from '../src'

function ssr(html: string, vnode: VNode) {
const container = document.createElement('div')
container.innerHTML = html
createSSRInstance(vnode).render(container)
createSSRContext({ message: {} }).render(vnode, container)
return {
container,
}
Expand Down Expand Up @@ -236,14 +236,12 @@ describe('SSR', () => {
})

test('async component lifecycle', async () => {
const container = document.createElement('div')
const fn = jest.fn()
const App = FCA(async () => {
onBeforeMount(fn)
return createVNode('span', null, 0)
})
container.innerHTML = '<span>0</span>'
createSSRInstance(h(App)).render(container)
ssr('<span>0</span>', h(App))
await sleep(0)
expect(fn).toHaveBeenCalled()
})
Expand All @@ -269,4 +267,16 @@ describe('SSR', () => {
const html = await renderToString(root)
expect(html).toBe('<span>Hello Gyron</span>')
})

test('ssr with component mounted property $el', async () => {
const fn = jest.fn(({ $el }) => {
expect($el).toBeTruthy()
})
const App = () => {
onAfterMount(fn)
return h('span', 'gyron')
}
ssr('<span>gyron</span>', h(App))
expect(fn).toHaveBeenCalled()
})
})

0 comments on commit 79b9920

Please sign in to comment.