feat(runtime-test): triggerEvent
This commit is contained in:
parent
52e6964d6c
commit
665cd8e3d9
@ -11,7 +11,9 @@ import {
|
|||||||
nextTick,
|
nextTick,
|
||||||
observable,
|
observable,
|
||||||
resetOps,
|
resetOps,
|
||||||
serialize
|
serialize,
|
||||||
|
renderIntsance,
|
||||||
|
triggerEvent
|
||||||
} from '../src'
|
} from '../src'
|
||||||
|
|
||||||
describe('test renderer', () => {
|
describe('test renderer', () => {
|
||||||
@ -152,4 +154,27 @@ describe('test renderer', () => {
|
|||||||
</div>`
|
</div>`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should be able to trigger events', async () => {
|
||||||
|
class App extends Component {
|
||||||
|
count = 0
|
||||||
|
inc() {
|
||||||
|
this.count++
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
return h(
|
||||||
|
'div',
|
||||||
|
{
|
||||||
|
onClick: this.inc
|
||||||
|
},
|
||||||
|
this.count
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const app = renderIntsance(App)
|
||||||
|
triggerEvent(app.$el, 'click')
|
||||||
|
expect(app.count).toBe(1)
|
||||||
|
await nextTick()
|
||||||
|
expect(serialize(app.$el)).toBe(`<div>1</div>`)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -34,5 +34,6 @@ export function renderIntsance<T extends Component>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export { serialize } from './serialize'
|
export { serialize } from './serialize'
|
||||||
|
export { triggerEvent } from './triggerEvent'
|
||||||
export * from './nodeOps'
|
export * from './nodeOps'
|
||||||
export * from '@vue/runtime-core'
|
export * from '@vue/runtime-core'
|
||||||
|
@ -10,6 +10,7 @@ export interface TestElement {
|
|||||||
tag: string
|
tag: string
|
||||||
children: TestNode[]
|
children: TestNode[]
|
||||||
props: Record<string, any>
|
props: Record<string, any>
|
||||||
|
eventListeners: Record<string, Function | Function[]> | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TestText {
|
export interface TestText {
|
||||||
@ -68,7 +69,8 @@ function createElement(tag: string): TestElement {
|
|||||||
tag,
|
tag,
|
||||||
children: [],
|
children: [],
|
||||||
props: {},
|
props: {},
|
||||||
parentNode: null
|
parentNode: null,
|
||||||
|
eventListeners: null
|
||||||
}
|
}
|
||||||
logNodeOp({
|
logNodeOp({
|
||||||
type: NodeOpTypes.CREATE,
|
type: NodeOpTypes.CREATE,
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { TestElement, logNodeOp, NodeOpTypes } from './nodeOps'
|
import { TestElement, logNodeOp, NodeOpTypes } from './nodeOps'
|
||||||
|
import { isOn } from '@vue/shared'
|
||||||
|
|
||||||
export function patchData(
|
export function patchData(
|
||||||
el: TestElement,
|
el: TestElement,
|
||||||
@ -14,4 +15,8 @@ export function patchData(
|
|||||||
propNextValue: nextValue
|
propNextValue: nextValue
|
||||||
})
|
})
|
||||||
el.props[key] = 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 { TestElement, TestNode, NodeTypes, TestText } from './nodeOps'
|
||||||
|
import { isOn } from '@vue/shared'
|
||||||
|
|
||||||
export function serialize(
|
export function serialize(
|
||||||
node: TestNode,
|
node: TestNode,
|
||||||
@ -19,7 +20,7 @@ function serializeElement(
|
|||||||
): string {
|
): string {
|
||||||
const props = Object.keys(node.props)
|
const props = Object.keys(node.props)
|
||||||
.map(key => {
|
.map(key => {
|
||||||
return `${key}=${JSON.stringify(node.props[key])}`
|
return isOn(key) ? `` : `${key}=${JSON.stringify(node.props[key])}`
|
||||||
})
|
})
|
||||||
.join(' ')
|
.join(' ')
|
||||||
const newLine = indent ? `\n` : ``
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user