test: test for setup()

This commit is contained in:
Evan You
2019-08-26 18:08:56 -04:00
parent 0ede301cf2
commit 5a754aac81
6 changed files with 203 additions and 24 deletions

View File

@@ -1,14 +1,22 @@
import { createRenderer, VNode } from '@vue/runtime-core'
import { nodeOps, TestElement } from './nodeOps'
import { patchProp } from './patchProp'
import { serializeInner } from './serialize'
export const render = createRenderer({
patchProp,
...nodeOps
}) as (node: VNode | null, container: TestElement) => VNode
export { serialize } from './serialize'
export { triggerEvent } from './triggerEvent'
// convenience for one-off render validations
export function renderToString(vnode: VNode) {
const root = nodeOps.createElement('div')
render(vnode, root)
return serializeInner(root)
}
export * from './triggerEvent'
export * from './serialize'
export * from './nodeOps'
export * from './jestUtils'
export * from '@vue/runtime-core'

View File

@@ -19,6 +19,19 @@ export function serialize(
}
}
export function serializeInner(
node: TestElement,
indent: number = 0,
depth: number = 0
) {
const newLine = indent ? `\n` : ``
return node.children.length
? newLine +
node.children.map(c => serialize(c, indent, depth + 1)).join(newLine) +
newLine
: ``
}
function serializeElement(
node: TestElement,
indent: number,
@@ -26,19 +39,15 @@ function serializeElement(
): string {
const props = Object.keys(node.props)
.map(key => {
return isOn(key) ? `` : `${key}=${JSON.stringify(node.props[key])}`
const value = node.props[key]
return isOn(key) || value == null ? `` : `${key}=${JSON.stringify(value)}`
})
.filter(_ => _)
.join(' ')
const newLine = indent ? `\n` : ``
const children = node.children.length
? newLine +
node.children.map(c => serialize(c, indent, depth + 1)).join(newLine) +
newLine
: ``
const padding = indent ? ` `.repeat(indent).repeat(depth) : ``
return (
`${padding}<${node.tag}${props ? ` ${props}` : ``}>` +
`${children}` +
`${serializeInner(node, indent, depth)}` +
`${padding}</${node.tag}>`
)
}