2018-10-02 01:15:07 +08:00
|
|
|
import {
|
2019-08-24 03:27:17 +08:00
|
|
|
h,
|
2018-10-02 01:15:07 +08:00
|
|
|
render,
|
|
|
|
nodeOps,
|
|
|
|
NodeTypes,
|
|
|
|
TestElement,
|
2019-08-22 05:05:14 +08:00
|
|
|
TestText,
|
|
|
|
ref,
|
|
|
|
reactive,
|
|
|
|
dumpOps,
|
|
|
|
resetOps,
|
|
|
|
NodeOpTypes,
|
|
|
|
nextTick,
|
|
|
|
serialize,
|
2019-10-11 09:53:15 +08:00
|
|
|
triggerEvent,
|
|
|
|
mockWarn
|
2018-10-02 01:15:07 +08:00
|
|
|
} from '../src'
|
|
|
|
|
|
|
|
describe('test renderer', () => {
|
2019-10-11 09:53:15 +08:00
|
|
|
mockWarn()
|
|
|
|
|
2019-08-22 05:05:14 +08:00
|
|
|
it('should work', () => {
|
2018-10-02 01:15:07 +08:00
|
|
|
const root = nodeOps.createElement('div')
|
2019-06-20 21:28:37 +08:00
|
|
|
render(
|
|
|
|
h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
id: 'test'
|
|
|
|
},
|
|
|
|
'hello'
|
|
|
|
),
|
|
|
|
root
|
|
|
|
)
|
2018-10-02 01:15:07 +08:00
|
|
|
|
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2019-08-22 05:05:14 +08:00
|
|
|
it('should record ops', async () => {
|
|
|
|
const state = reactive({
|
|
|
|
id: 'test',
|
|
|
|
text: 'hello'
|
|
|
|
})
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
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(4)
|
|
|
|
|
|
|
|
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.SET_ELEMENT_TEXT,
|
|
|
|
text: 'hello',
|
|
|
|
targetNode: root.children[0]
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(ops[3]).toEqual({
|
|
|
|
type: NodeOpTypes.INSERT,
|
|
|
|
targetNode: root.children[0],
|
|
|
|
parentNode: root,
|
|
|
|
refNode: null
|
|
|
|
})
|
|
|
|
|
|
|
|
// 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_ELEMENT_TEXT,
|
|
|
|
targetNode: root.children[0],
|
|
|
|
text: 'bar'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to serialize nodes', () => {
|
|
|
|
const App = {
|
|
|
|
render() {
|
|
|
|
return h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
id: 'test'
|
|
|
|
},
|
2019-08-24 03:27:17 +08:00
|
|
|
[h('span', 'foo'), 'hello']
|
2019-08-22 05:05:14 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(App), root)
|
|
|
|
expect(serialize(root)).toEqual(
|
|
|
|
`<div><div id="test"><span>foo</span>hello</div></div>`
|
|
|
|
)
|
|
|
|
// indented output
|
|
|
|
expect(serialize(root, 2)).toEqual(
|
|
|
|
`<div>
|
|
|
|
<div id="test">
|
|
|
|
<span>
|
|
|
|
foo
|
|
|
|
</span>
|
|
|
|
hello
|
|
|
|
</div>
|
|
|
|
</div>`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to trigger events', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
|
|
|
|
const App = () => {
|
|
|
|
return h(
|
|
|
|
'span',
|
|
|
|
{
|
|
|
|
onClick: () => {
|
|
|
|
count.value++
|
|
|
|
}
|
|
|
|
},
|
|
|
|
count.value
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(App), root)
|
|
|
|
triggerEvent(root.children[0] as TestElement, 'click')
|
|
|
|
expect(count.value).toBe(1)
|
|
|
|
await nextTick()
|
|
|
|
expect(serialize(root)).toBe(`<div><span>1</span></div>`)
|
|
|
|
})
|
2019-10-11 09:53:15 +08:00
|
|
|
|
|
|
|
it('should be able to trigger events with muliple listeners', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
const count2 = ref(1)
|
|
|
|
|
|
|
|
const App = () => {
|
|
|
|
return h(
|
|
|
|
'span',
|
|
|
|
{
|
|
|
|
onClick: [
|
|
|
|
() => {
|
|
|
|
count.value++
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
count2.value++
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
`${count.value}, ${count2.value}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(App), root)
|
|
|
|
triggerEvent(root.children[0] as TestElement, 'click')
|
|
|
|
expect(count.value).toBe(1)
|
|
|
|
expect(count2.value).toBe(2)
|
|
|
|
await nextTick()
|
|
|
|
expect(serialize(root)).toBe(`<div><span>1, 2</span></div>`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should mock warn', () => {
|
|
|
|
console.warn('warn!!!')
|
|
|
|
expect('warn!!!').toHaveBeenWarned()
|
|
|
|
expect('warn!!!').toHaveBeenWarnedTimes(1)
|
|
|
|
|
|
|
|
console.warn('warn!!!')
|
|
|
|
expect('warn!!!').toHaveBeenWarnedTimes(2)
|
|
|
|
|
|
|
|
console.warn('warning')
|
|
|
|
expect('warn!!!').toHaveBeenWarnedTimes(2)
|
|
|
|
expect('warning').toHaveBeenWarnedLast()
|
|
|
|
})
|
2018-10-02 01:15:07 +08:00
|
|
|
})
|