refactor: rename packages
This commit is contained in:
3
packages/runtime-test/.npmignore
Normal file
3
packages/runtime-test/.npmignore
Normal file
@@ -0,0 +1,3 @@
|
||||
__tests__/
|
||||
__mocks__/
|
||||
dist/packages
|
||||
34
packages/runtime-test/README.md
Normal file
34
packages/runtime-test/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# @vue/runtime-test
|
||||
|
||||
``` js
|
||||
import {
|
||||
h,
|
||||
render,
|
||||
Component,
|
||||
nodeOps,
|
||||
startRecordingOps,
|
||||
dumpOps
|
||||
} from '@vue/runtime-test'
|
||||
|
||||
class App extends Component {
|
||||
data () {
|
||||
return {
|
||||
msg: 'Hello World!'
|
||||
}
|
||||
}
|
||||
render () {
|
||||
return h('div', this.msg)
|
||||
}
|
||||
}
|
||||
|
||||
// root is of type `TestElement` as defined in src/nodeOps.ts
|
||||
const root = nodeOps.createElement('div')
|
||||
|
||||
startRecordingOps()
|
||||
|
||||
render(h(App), root)
|
||||
|
||||
const ops = dumpOps()
|
||||
|
||||
console.log(ops)
|
||||
```
|
||||
155
packages/runtime-test/__tests__/testRenderer.spec.ts
Normal file
155
packages/runtime-test/__tests__/testRenderer.spec.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import {
|
||||
h,
|
||||
render,
|
||||
Component,
|
||||
nodeOps,
|
||||
NodeTypes,
|
||||
TestElement,
|
||||
TestText,
|
||||
dumpOps,
|
||||
NodeOpTypes,
|
||||
nextTick,
|
||||
observable,
|
||||
resetOps,
|
||||
serialize
|
||||
} from '../src'
|
||||
|
||||
describe('test renderer', () => {
|
||||
it('should work', () => {
|
||||
class App extends Component {
|
||||
render() {
|
||||
return h(
|
||||
'div',
|
||||
{
|
||||
id: 'test'
|
||||
},
|
||||
'hello'
|
||||
)
|
||||
}
|
||||
}
|
||||
const root = nodeOps.createElement('div')
|
||||
render(h(App), root)
|
||||
|
||||
expect(root.children.length).toBe(1)
|
||||
|
||||
const el = root.children[0] as TestElement
|
||||
expect(el.type).toBe(NodeTypes.ELEMENT)
|
||||
expect(el.props.id).toBe('test')
|
||||
expect(el.children.length).toBe(1)
|
||||
|
||||
const text = el.children[0] as TestText
|
||||
expect(text.type).toBe(NodeTypes.TEXT)
|
||||
expect(text.text).toBe('hello')
|
||||
})
|
||||
|
||||
it('should record ops', async () => {
|
||||
const state = observable({
|
||||
id: 'test',
|
||||
text: 'hello'
|
||||
})
|
||||
|
||||
class App extends Component {
|
||||
render() {
|
||||
return h(
|
||||
'div',
|
||||
{
|
||||
id: state.id
|
||||
},
|
||||
state.text
|
||||
)
|
||||
}
|
||||
}
|
||||
const root = nodeOps.createElement('div')
|
||||
|
||||
resetOps()
|
||||
render(h(App), root)
|
||||
const ops = dumpOps()
|
||||
|
||||
expect(ops.length).toBe(5)
|
||||
|
||||
expect(ops[0]).toEqual({
|
||||
type: NodeOpTypes.CREATE,
|
||||
nodeType: NodeTypes.ELEMENT,
|
||||
tag: 'div',
|
||||
targetNode: root.children[0]
|
||||
})
|
||||
|
||||
expect(ops[1]).toEqual({
|
||||
type: NodeOpTypes.PATCH,
|
||||
targetNode: root.children[0],
|
||||
propKey: 'id',
|
||||
propPrevValue: null,
|
||||
propNextValue: 'test'
|
||||
})
|
||||
|
||||
expect(ops[2]).toEqual({
|
||||
type: NodeOpTypes.CREATE,
|
||||
nodeType: NodeTypes.TEXT,
|
||||
text: 'hello',
|
||||
targetNode: (root.children[0] as TestElement).children[0]
|
||||
})
|
||||
|
||||
expect(ops[3]).toEqual({
|
||||
type: NodeOpTypes.APPEND,
|
||||
targetNode: (root.children[0] as TestElement).children[0],
|
||||
parentNode: root.children[0]
|
||||
})
|
||||
|
||||
expect(ops[4]).toEqual({
|
||||
type: NodeOpTypes.APPEND,
|
||||
targetNode: root.children[0],
|
||||
parentNode: root
|
||||
})
|
||||
|
||||
// test update ops
|
||||
state.id = 'foo'
|
||||
state.text = 'bar'
|
||||
await nextTick()
|
||||
|
||||
const updateOps = dumpOps()
|
||||
expect(updateOps.length).toBe(2)
|
||||
|
||||
expect(updateOps[0]).toEqual({
|
||||
type: NodeOpTypes.PATCH,
|
||||
targetNode: root.children[0],
|
||||
propKey: 'id',
|
||||
propPrevValue: 'test',
|
||||
propNextValue: 'foo'
|
||||
})
|
||||
|
||||
expect(updateOps[1]).toEqual({
|
||||
type: NodeOpTypes.SET_TEXT,
|
||||
targetNode: (root.children[0] as TestElement).children[0],
|
||||
text: 'bar'
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to serialize nodes', () => {
|
||||
class App extends Component {
|
||||
render() {
|
||||
return h(
|
||||
'div',
|
||||
{
|
||||
id: 'test'
|
||||
},
|
||||
[h('span', 'foo'), 'hello']
|
||||
)
|
||||
}
|
||||
}
|
||||
const root = nodeOps.createElement('div')
|
||||
render(h(App), root)
|
||||
expect(serialize(root)).toEqual(
|
||||
`<div><div id="test"><span>foo</span>hello</div></div>`
|
||||
)
|
||||
expect(serialize(root, 2)).toEqual(
|
||||
`<div>
|
||||
<div id="test">
|
||||
<span>
|
||||
foo
|
||||
</span>
|
||||
hello
|
||||
</div>
|
||||
</div>`
|
||||
)
|
||||
})
|
||||
})
|
||||
7
packages/runtime-test/index.js
Normal file
7
packages/runtime-test/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
module.exports = require('./dist/runtime-test.cjs.prod.js')
|
||||
} else {
|
||||
module.exports = require('./dist/runtime-test.cjs.js')
|
||||
}
|
||||
24
packages/runtime-test/package.json
Normal file
24
packages/runtime-test/package.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "@vue/runtime-test",
|
||||
"version": "3.0.0-alpha.1",
|
||||
"description": "@vue/runtime-test",
|
||||
"main": "index.js",
|
||||
"module": "dist/runtime-test.esm-bundler.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/vuejs/vue.git"
|
||||
},
|
||||
"keywords": [
|
||||
"vue"
|
||||
],
|
||||
"author": "Evan You",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/vuejs/vue/issues"
|
||||
},
|
||||
"homepage": "https://github.com/vuejs/vue/tree/dev/packages/runtime-test#readme",
|
||||
"dependencies": {
|
||||
"@vue/runtime-core": "3.0.0-alpha.1"
|
||||
}
|
||||
}
|
||||
38
packages/runtime-test/src/index.ts
Normal file
38
packages/runtime-test/src/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import {
|
||||
h,
|
||||
createRenderer,
|
||||
VNode,
|
||||
Component,
|
||||
createComponentInstance
|
||||
} from '@vue/runtime-core'
|
||||
import { nodeOps, TestElement } from './nodeOps'
|
||||
import { patchData } from './patchData'
|
||||
|
||||
const { render: _render } = createRenderer({
|
||||
nodeOps,
|
||||
patchData
|
||||
})
|
||||
|
||||
type publicRender = (
|
||||
node: VNode | null,
|
||||
container: TestElement
|
||||
) => Component | null
|
||||
export const render = _render as publicRender
|
||||
|
||||
export function createInstance<T extends Component>(
|
||||
Class: new () => T,
|
||||
props?: any
|
||||
): T {
|
||||
return createComponentInstance(h(Class, props)).$proxy as any
|
||||
}
|
||||
|
||||
export function renderIntsance<T extends Component>(
|
||||
Class: new () => T,
|
||||
props?: any
|
||||
): T {
|
||||
return render(h(Class, props), nodeOps.createElement('div')) as any
|
||||
}
|
||||
|
||||
export { serialize } from './serialize'
|
||||
export * from './nodeOps'
|
||||
export * from '@vue/runtime-core'
|
||||
202
packages/runtime-test/src/nodeOps.ts
Normal file
202
packages/runtime-test/src/nodeOps.ts
Normal file
@@ -0,0 +1,202 @@
|
||||
export const enum NodeTypes {
|
||||
TEXT = 'text',
|
||||
ELEMENT = 'element'
|
||||
}
|
||||
|
||||
export interface TestElement {
|
||||
id: number
|
||||
type: NodeTypes.ELEMENT
|
||||
parentNode: TestElement | null
|
||||
tag: string
|
||||
children: TestNode[]
|
||||
props: Record<string, any>
|
||||
}
|
||||
|
||||
export interface TestText {
|
||||
id: number
|
||||
type: NodeTypes.TEXT
|
||||
parentNode: TestElement | null
|
||||
text: string
|
||||
}
|
||||
|
||||
export type TestNode = TestElement | TestText
|
||||
|
||||
export const enum NodeOpTypes {
|
||||
CREATE = 'create',
|
||||
INSERT = 'insert',
|
||||
APPEND = 'append',
|
||||
REMOVE = 'remove',
|
||||
SET_TEXT = 'setText',
|
||||
CLEAR = 'clearContent',
|
||||
PATCH = 'patch'
|
||||
}
|
||||
|
||||
export interface NodeOp {
|
||||
type: NodeOpTypes
|
||||
nodeType?: NodeTypes
|
||||
tag?: string
|
||||
text?: string
|
||||
targetNode?: TestNode
|
||||
parentNode?: TestElement
|
||||
refNode?: TestNode
|
||||
propKey?: string
|
||||
propPrevValue?: any
|
||||
propNextValue?: any
|
||||
}
|
||||
|
||||
let nodeId: number = 0
|
||||
let recordedNodeOps: NodeOp[] = []
|
||||
|
||||
export function logNodeOp(op: NodeOp) {
|
||||
recordedNodeOps.push(op)
|
||||
}
|
||||
|
||||
export function resetOps() {
|
||||
recordedNodeOps = []
|
||||
}
|
||||
|
||||
export function dumpOps(): NodeOp[] {
|
||||
const ops = recordedNodeOps.slice()
|
||||
resetOps()
|
||||
return ops
|
||||
}
|
||||
|
||||
function createElement(tag: string): TestElement {
|
||||
const node: TestElement = {
|
||||
id: nodeId++,
|
||||
type: NodeTypes.ELEMENT,
|
||||
tag,
|
||||
children: [],
|
||||
props: {},
|
||||
parentNode: null
|
||||
}
|
||||
logNodeOp({
|
||||
type: NodeOpTypes.CREATE,
|
||||
nodeType: NodeTypes.ELEMENT,
|
||||
targetNode: node,
|
||||
tag
|
||||
})
|
||||
return node
|
||||
}
|
||||
|
||||
function createText(text: string): TestText {
|
||||
const node: TestText = {
|
||||
id: nodeId++,
|
||||
type: NodeTypes.TEXT,
|
||||
text,
|
||||
parentNode: null
|
||||
}
|
||||
logNodeOp({
|
||||
type: NodeOpTypes.CREATE,
|
||||
nodeType: NodeTypes.TEXT,
|
||||
targetNode: node,
|
||||
text
|
||||
})
|
||||
return node
|
||||
}
|
||||
|
||||
function setText(node: TestText, text: string) {
|
||||
logNodeOp({
|
||||
type: NodeOpTypes.SET_TEXT,
|
||||
targetNode: node,
|
||||
text
|
||||
})
|
||||
node.text = text
|
||||
}
|
||||
|
||||
function appendChild(parent: TestElement, child: TestNode) {
|
||||
logNodeOp({
|
||||
type: NodeOpTypes.APPEND,
|
||||
targetNode: child,
|
||||
parentNode: parent
|
||||
})
|
||||
if (child.parentNode) {
|
||||
removeChild(child.parentNode, child)
|
||||
}
|
||||
parent.children.push(child)
|
||||
child.parentNode = parent
|
||||
}
|
||||
|
||||
function insertBefore(parent: TestElement, child: TestNode, ref: TestNode) {
|
||||
if (child.parentNode) {
|
||||
removeChild(child.parentNode, child)
|
||||
}
|
||||
const refIndex = parent.children.indexOf(ref)
|
||||
if (refIndex === -1) {
|
||||
console.error('ref: ', ref)
|
||||
console.error('parent: ', parent)
|
||||
throw new Error('ref is not a child of parent')
|
||||
}
|
||||
logNodeOp({
|
||||
type: NodeOpTypes.INSERT,
|
||||
targetNode: child,
|
||||
parentNode: parent,
|
||||
refNode: ref
|
||||
})
|
||||
parent.children.splice(refIndex, 0, child)
|
||||
child.parentNode = parent
|
||||
}
|
||||
|
||||
function removeChild(parent: TestElement, child: TestNode) {
|
||||
logNodeOp({
|
||||
type: NodeOpTypes.REMOVE,
|
||||
targetNode: child,
|
||||
parentNode: parent
|
||||
})
|
||||
const i = parent.children.indexOf(child)
|
||||
if (i > -1) {
|
||||
parent.children.splice(i, 1)
|
||||
} else {
|
||||
console.error('target: ', child)
|
||||
console.error('parent: ', parent)
|
||||
throw Error('target is not a childNode of parent')
|
||||
}
|
||||
child.parentNode = null
|
||||
}
|
||||
|
||||
function clearContent(node: TestNode) {
|
||||
logNodeOp({
|
||||
type: NodeOpTypes.CLEAR,
|
||||
targetNode: node
|
||||
})
|
||||
if (node.type === NodeTypes.ELEMENT) {
|
||||
node.children.forEach(c => {
|
||||
c.parentNode = null
|
||||
})
|
||||
node.children = []
|
||||
} else {
|
||||
node.text = ''
|
||||
}
|
||||
}
|
||||
|
||||
function parentNode(node: TestNode): TestElement | null {
|
||||
return node.parentNode
|
||||
}
|
||||
|
||||
function nextSibling(node: TestNode): TestNode | null {
|
||||
const parent = node.parentNode
|
||||
if (!parent) {
|
||||
return null
|
||||
}
|
||||
const i = parent.children.indexOf(node)
|
||||
return parent.children[i + 1] || null
|
||||
}
|
||||
|
||||
function querySelector() {
|
||||
throw new Error('querySelector not supported in test renderer.')
|
||||
}
|
||||
|
||||
export const nodeOps = {
|
||||
createElement,
|
||||
createText,
|
||||
setText,
|
||||
appendChild,
|
||||
insertBefore,
|
||||
removeChild,
|
||||
clearContent,
|
||||
parentNode,
|
||||
nextSibling,
|
||||
querySelector
|
||||
}
|
||||
|
||||
export function patchData() {}
|
||||
17
packages/runtime-test/src/patchData.ts
Normal file
17
packages/runtime-test/src/patchData.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { TestElement, logNodeOp, NodeOpTypes } from './nodeOps'
|
||||
|
||||
export function patchData(
|
||||
el: TestElement,
|
||||
key: string,
|
||||
prevValue: any,
|
||||
nextValue: any
|
||||
) {
|
||||
logNodeOp({
|
||||
type: NodeOpTypes.PATCH,
|
||||
targetNode: el,
|
||||
propKey: key,
|
||||
propPrevValue: prevValue,
|
||||
propNextValue: nextValue
|
||||
})
|
||||
el.props[key] = nextValue
|
||||
}
|
||||
42
packages/runtime-test/src/serialize.ts
Normal file
42
packages/runtime-test/src/serialize.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { TestElement, TestNode, NodeTypes, TestText } from './nodeOps'
|
||||
|
||||
export function serialize(
|
||||
node: TestNode,
|
||||
indent: number = 0,
|
||||
depth: number = 0
|
||||
): string {
|
||||
if (node.type === NodeTypes.ELEMENT) {
|
||||
return serializeElement(node, indent, depth)
|
||||
} else {
|
||||
return serializeText(node, indent, depth)
|
||||
}
|
||||
}
|
||||
|
||||
function serializeElement(
|
||||
node: TestElement,
|
||||
indent: number,
|
||||
depth: number
|
||||
): string {
|
||||
const props = Object.keys(node.props)
|
||||
.map(key => {
|
||||
return `${key}=${JSON.stringify(node.props[key])}`
|
||||
})
|
||||
.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}` +
|
||||
`${padding}</${node.tag}>`
|
||||
)
|
||||
}
|
||||
|
||||
function serializeText(node: TestText, indent: number, depth: number): string {
|
||||
const padding = indent ? ` `.repeat(indent).repeat(depth) : ``
|
||||
return padding + node.text
|
||||
}
|
||||
Reference in New Issue
Block a user