Skip to content

Commit

Permalink
test: add tests for createElement with binding (#921)
Browse files Browse the repository at this point in the history
Co-authored-by: neiyichao03 <nieyichao03@kuaishou.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people authored Apr 25, 2022
1 parent 564a5a4 commit e0128e3
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions test/v3/runtime-core/h.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { h, createApp } from '../../../src'
import { mockWarn } from '../../helpers'
import { h, createApp, ref, getCurrentInstance } from '../../../src'
import { mockWarn, sleep } from '../../helpers'

describe('renderer: h', () => {
mockWarn(true)
Expand All @@ -10,6 +10,45 @@ describe('renderer: h', () => {
).toHaveBeenWarned()
})

it('should not warn with called outside of render function', async () => {
const spy = jest.fn()
const Comp = {
setup() {
const instance = getCurrentInstance()
const createElement = h.bind(instance)
const renderVnode = () => createElement('p', {})
setTimeout(renderVnode, 10)
},
}
const root = document.createElement('div')
createApp(Comp).mount(root)
await sleep(50)

expect(spy).toHaveBeenCalledTimes(0)
})

it(`Should support h's responsive rendering`, async () => {
const Comp = {
setup() {
const showNode1 = ref(true)
setTimeout(() => {
showNode1.value = false
}, 10)
return () =>
showNode1.value
? h('div', void 0, [h('br'), 'hello world', h('br')])
: h('div', void 0, [h('div'), 'nextTick render', h('div')])
},
}
const root = document.createElement('div')
const vm = createApp(Comp).mount(root)
expect(vm.$el.outerHTML).toBe(`<div><br>hello world<br></div>`)
await sleep(50)
expect(vm.$el.outerHTML).toBe(
`<div><div></div>nextTick render<div></div></div>`
)
})

it('should work with called outside of render function', () => {
const msg = 'hello world'
const vnode = h('hello-world', {
Expand Down

0 comments on commit e0128e3

Please sign in to comment.