feat(runtime-test): triggerEvent
This commit is contained in:
@@ -34,5 +34,6 @@ export function renderIntsance<T extends Component>(
|
||||
}
|
||||
|
||||
export { serialize } from './serialize'
|
||||
export { triggerEvent } from './triggerEvent'
|
||||
export * from './nodeOps'
|
||||
export * from '@vue/runtime-core'
|
||||
|
||||
@@ -10,6 +10,7 @@ export interface TestElement {
|
||||
tag: string
|
||||
children: TestNode[]
|
||||
props: Record<string, any>
|
||||
eventListeners: Record<string, Function | Function[]> | null
|
||||
}
|
||||
|
||||
export interface TestText {
|
||||
@@ -68,7 +69,8 @@ function createElement(tag: string): TestElement {
|
||||
tag,
|
||||
children: [],
|
||||
props: {},
|
||||
parentNode: null
|
||||
parentNode: null,
|
||||
eventListeners: null
|
||||
}
|
||||
logNodeOp({
|
||||
type: NodeOpTypes.CREATE,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { TestElement, logNodeOp, NodeOpTypes } from './nodeOps'
|
||||
import { isOn } from '@vue/shared'
|
||||
|
||||
export function patchData(
|
||||
el: TestElement,
|
||||
@@ -14,4 +15,8 @@ export function patchData(
|
||||
propNextValue: nextValue
|
||||
})
|
||||
el.props[key] = nextValue
|
||||
if (isOn(key)) {
|
||||
const event = key.slice(2).toLowerCase()
|
||||
;(el.eventListeners || (el.eventListeners = {}))[event] = nextValue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { TestElement, TestNode, NodeTypes, TestText } from './nodeOps'
|
||||
import { isOn } from '@vue/shared'
|
||||
|
||||
export function serialize(
|
||||
node: TestNode,
|
||||
@@ -19,7 +20,7 @@ function serializeElement(
|
||||
): string {
|
||||
const props = Object.keys(node.props)
|
||||
.map(key => {
|
||||
return `${key}=${JSON.stringify(node.props[key])}`
|
||||
return isOn(key) ? `` : `${key}=${JSON.stringify(node.props[key])}`
|
||||
})
|
||||
.join(' ')
|
||||
const newLine = indent ? `\n` : ``
|
||||
|
||||
21
packages/runtime-test/src/triggerEvent.ts
Normal file
21
packages/runtime-test/src/triggerEvent.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { TestElement } from './nodeOps'
|
||||
|
||||
export function triggerEvent(
|
||||
el: TestElement,
|
||||
event: string,
|
||||
payload: any[] = []
|
||||
) {
|
||||
const { eventListeners } = el
|
||||
if (eventListeners) {
|
||||
const listener = eventListeners[event]
|
||||
if (listener) {
|
||||
if (Array.isArray(listener)) {
|
||||
for (let i = 0; i < listener.length; i++) {
|
||||
listener[i](...payload)
|
||||
}
|
||||
} else {
|
||||
listener(...payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user