test: test ops logging for test renderer
This commit is contained in:
parent
da20a06a78
commit
dfc855cd54
@ -49,7 +49,9 @@ export function initializeComputed(
|
|||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
get(_, key: any) {
|
get(_, key: any) {
|
||||||
return handles[key]()
|
if (handles.hasOwnProperty(key)) {
|
||||||
|
return handles[key]()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// TODO should be readonly
|
// TODO should be readonly
|
||||||
}
|
}
|
||||||
|
@ -5,19 +5,30 @@ import {
|
|||||||
nodeOps,
|
nodeOps,
|
||||||
NodeTypes,
|
NodeTypes,
|
||||||
TestElement,
|
TestElement,
|
||||||
TestText
|
TestText,
|
||||||
|
dumpOps,
|
||||||
|
NodeOpTypes,
|
||||||
|
nextTick,
|
||||||
|
observable,
|
||||||
|
resetOps
|
||||||
} from '../src'
|
} from '../src'
|
||||||
|
|
||||||
describe('test renderer', () => {
|
describe('test renderer', () => {
|
||||||
it('should work', () => {
|
it('should work', () => {
|
||||||
class App extends Component {
|
class App extends Component {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
id: 'test',
|
||||||
|
text: 'hello'
|
||||||
|
}
|
||||||
|
}
|
||||||
render() {
|
render() {
|
||||||
return h(
|
return h(
|
||||||
'div',
|
'div',
|
||||||
{
|
{
|
||||||
id: 'test'
|
id: this.id
|
||||||
},
|
},
|
||||||
'hello'
|
this.text
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,7 +47,85 @@ describe('test renderer', () => {
|
|||||||
expect(text.text).toBe('hello')
|
expect(text.text).toBe('hello')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should record ops', () => {
|
it('should record ops', async () => {
|
||||||
// TODO
|
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'
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,14 +1,6 @@
|
|||||||
import { createRenderer, VNode } from '@vue/core'
|
import { createRenderer, VNode } from '@vue/core'
|
||||||
import { nodeOps, TestElement } from './nodeOps'
|
import { nodeOps, TestElement } from './nodeOps'
|
||||||
|
import { patchData } from './patchData'
|
||||||
function patchData(
|
|
||||||
el: TestElement,
|
|
||||||
key: string,
|
|
||||||
prevValue: any,
|
|
||||||
nextValue: any
|
|
||||||
) {
|
|
||||||
el.props[key] = nextValue
|
|
||||||
}
|
|
||||||
|
|
||||||
const { render: _render } = createRenderer({
|
const { render: _render } = createRenderer({
|
||||||
nodeOps,
|
nodeOps,
|
||||||
|
@ -21,57 +21,44 @@ export interface TestText {
|
|||||||
|
|
||||||
export type TestNode = TestElement | TestText
|
export type TestNode = TestElement | TestText
|
||||||
|
|
||||||
const enum OpTypes {
|
export const enum NodeOpTypes {
|
||||||
CREATE = 'create',
|
CREATE = 'create',
|
||||||
INSERT = 'insert',
|
INSERT = 'insert',
|
||||||
APPEND = 'append',
|
APPEND = 'append',
|
||||||
REMOVE = 'remove',
|
REMOVE = 'remove',
|
||||||
SET_TEXT = 'setText',
|
SET_TEXT = 'setText',
|
||||||
CLEAR = 'clearContent',
|
CLEAR = 'clearContent',
|
||||||
NEXT_SIBLING = 'nextSibling',
|
PATCH = 'patch'
|
||||||
PARENT_NODE = 'parentNode'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Op {
|
export interface NodeOp {
|
||||||
type: OpTypes
|
type: NodeOpTypes
|
||||||
nodeType?: NodeTypes
|
nodeType?: NodeTypes
|
||||||
tag?: string
|
tag?: string
|
||||||
text?: string
|
text?: string
|
||||||
targetNode?: TestNode
|
targetNode?: TestNode
|
||||||
parentNode?: TestElement
|
parentNode?: TestElement
|
||||||
refNode?: TestNode
|
refNode?: TestNode
|
||||||
|
propKey?: string
|
||||||
|
propPrevValue?: any
|
||||||
|
propNextValue?: any
|
||||||
}
|
}
|
||||||
|
|
||||||
let nodeId: number = 0
|
let nodeId: number = 0
|
||||||
let isRecording: boolean = false
|
let recordedNodeOps: NodeOp[] = []
|
||||||
let recordedOps: Op[] = []
|
|
||||||
|
|
||||||
function logOp(op: Op) {
|
export function logNodeOp(op: NodeOp) {
|
||||||
if (isRecording) {
|
recordedNodeOps.push(op)
|
||||||
recordedOps.push(op)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function startRecordingOps() {
|
export function resetOps() {
|
||||||
if (!isRecording) {
|
recordedNodeOps = []
|
||||||
isRecording = true
|
|
||||||
recordedOps = []
|
|
||||||
} else {
|
|
||||||
throw new Error(
|
|
||||||
'`startRecordingOps` called when there is already an active session.'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dumpOps(): Op[] {
|
export function dumpOps(): NodeOp[] {
|
||||||
if (!isRecording) {
|
const ops = recordedNodeOps.slice()
|
||||||
throw new Error(
|
resetOps()
|
||||||
'`dumpOps` called without a recording session. ' +
|
return ops
|
||||||
'Call `startRecordingOps` first to start a session.'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
isRecording = false
|
|
||||||
return recordedOps.slice()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createElement(tag: string): TestElement {
|
function createElement(tag: string): TestElement {
|
||||||
@ -83,8 +70,8 @@ function createElement(tag: string): TestElement {
|
|||||||
props: {},
|
props: {},
|
||||||
parentNode: null
|
parentNode: null
|
||||||
}
|
}
|
||||||
logOp({
|
logNodeOp({
|
||||||
type: OpTypes.CREATE,
|
type: NodeOpTypes.CREATE,
|
||||||
nodeType: NodeTypes.ELEMENT,
|
nodeType: NodeTypes.ELEMENT,
|
||||||
targetNode: node,
|
targetNode: node,
|
||||||
tag
|
tag
|
||||||
@ -99,8 +86,8 @@ function createText(text: string): TestText {
|
|||||||
text,
|
text,
|
||||||
parentNode: null
|
parentNode: null
|
||||||
}
|
}
|
||||||
logOp({
|
logNodeOp({
|
||||||
type: OpTypes.CREATE,
|
type: NodeOpTypes.CREATE,
|
||||||
nodeType: NodeTypes.TEXT,
|
nodeType: NodeTypes.TEXT,
|
||||||
targetNode: node,
|
targetNode: node,
|
||||||
text
|
text
|
||||||
@ -109,8 +96,8 @@ function createText(text: string): TestText {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setText(node: TestText, text: string) {
|
function setText(node: TestText, text: string) {
|
||||||
logOp({
|
logNodeOp({
|
||||||
type: OpTypes.SET_TEXT,
|
type: NodeOpTypes.SET_TEXT,
|
||||||
targetNode: node,
|
targetNode: node,
|
||||||
text
|
text
|
||||||
})
|
})
|
||||||
@ -118,8 +105,8 @@ function setText(node: TestText, text: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function appendChild(parent: TestElement, child: TestNode) {
|
function appendChild(parent: TestElement, child: TestNode) {
|
||||||
logOp({
|
logNodeOp({
|
||||||
type: OpTypes.APPEND,
|
type: NodeOpTypes.APPEND,
|
||||||
targetNode: child,
|
targetNode: child,
|
||||||
parentNode: parent
|
parentNode: parent
|
||||||
})
|
})
|
||||||
@ -140,8 +127,8 @@ function insertBefore(parent: TestElement, child: TestNode, ref: TestNode) {
|
|||||||
console.error('parent: ', parent)
|
console.error('parent: ', parent)
|
||||||
throw new Error('ref is not a child of parent')
|
throw new Error('ref is not a child of parent')
|
||||||
}
|
}
|
||||||
logOp({
|
logNodeOp({
|
||||||
type: OpTypes.INSERT,
|
type: NodeOpTypes.INSERT,
|
||||||
targetNode: child,
|
targetNode: child,
|
||||||
parentNode: parent,
|
parentNode: parent,
|
||||||
refNode: ref
|
refNode: ref
|
||||||
@ -160,8 +147,8 @@ function replaceChild(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function removeChild(parent: TestElement, child: TestNode) {
|
function removeChild(parent: TestElement, child: TestNode) {
|
||||||
logOp({
|
logNodeOp({
|
||||||
type: OpTypes.REMOVE,
|
type: NodeOpTypes.REMOVE,
|
||||||
targetNode: child,
|
targetNode: child,
|
||||||
parentNode: parent
|
parentNode: parent
|
||||||
})
|
})
|
||||||
@ -177,8 +164,8 @@ function removeChild(parent: TestElement, child: TestNode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function clearContent(node: TestNode) {
|
function clearContent(node: TestNode) {
|
||||||
logOp({
|
logNodeOp({
|
||||||
type: OpTypes.CLEAR,
|
type: NodeOpTypes.CLEAR,
|
||||||
targetNode: node
|
targetNode: node
|
||||||
})
|
})
|
||||||
if (node.type === NodeTypes.ELEMENT) {
|
if (node.type === NodeTypes.ELEMENT) {
|
||||||
@ -192,18 +179,10 @@ function clearContent(node: TestNode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function parentNode(node: TestNode): TestElement | null {
|
function parentNode(node: TestNode): TestElement | null {
|
||||||
logOp({
|
|
||||||
type: OpTypes.PARENT_NODE,
|
|
||||||
targetNode: node
|
|
||||||
})
|
|
||||||
return node.parentNode
|
return node.parentNode
|
||||||
}
|
}
|
||||||
|
|
||||||
function nextSibling(node: TestNode): TestNode | null {
|
function nextSibling(node: TestNode): TestNode | null {
|
||||||
logOp({
|
|
||||||
type: OpTypes.NEXT_SIBLING,
|
|
||||||
targetNode: node
|
|
||||||
})
|
|
||||||
const parent = node.parentNode
|
const parent = node.parentNode
|
||||||
if (!parent) {
|
if (!parent) {
|
||||||
return null
|
return null
|
||||||
@ -229,3 +208,5 @@ export const nodeOps = {
|
|||||||
nextSibling,
|
nextSibling,
|
||||||
querySelector
|
querySelector
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function patchData() {}
|
||||||
|
17
packages/renderer-test/src/patchData.ts
Normal file
17
packages/renderer-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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user