2018-10-02 01:15:07 +08:00
|
|
|
import {
|
|
|
|
h,
|
|
|
|
render,
|
|
|
|
Component,
|
|
|
|
nodeOps,
|
|
|
|
NodeTypes,
|
|
|
|
TestElement,
|
2018-10-02 04:42:53 +08:00
|
|
|
TestText,
|
|
|
|
dumpOps,
|
|
|
|
NodeOpTypes,
|
|
|
|
nextTick,
|
|
|
|
observable,
|
2018-10-02 05:22:49 +08:00
|
|
|
resetOps,
|
|
|
|
serialize
|
2018-10-02 01:15:07 +08:00
|
|
|
} from '../src'
|
|
|
|
|
|
|
|
describe('test renderer', () => {
|
|
|
|
it('should work', () => {
|
|
|
|
class App extends Component {
|
|
|
|
render() {
|
|
|
|
return h(
|
|
|
|
'div',
|
|
|
|
{
|
2018-10-02 05:22:49 +08:00
|
|
|
id: 'test'
|
2018-10-02 01:15:07 +08:00
|
|
|
},
|
2018-10-02 05:22:49 +08:00
|
|
|
'hello'
|
2018-10-02 01:15:07 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2018-10-02 04:42:53 +08:00
|
|
|
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'
|
|
|
|
})
|
2018-10-02 01:15:07 +08:00
|
|
|
})
|
2018-10-02 05:22:49 +08:00
|
|
|
|
|
|
|
it('should be able to serialize nodes', () => {
|
|
|
|
class App extends Component {
|
|
|
|
render() {
|
|
|
|
return h(
|
|
|
|
'div',
|
|
|
|
{
|
|
|
|
id: 'test'
|
|
|
|
},
|
|
|
|
'hello'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(App), root)
|
|
|
|
|
|
|
|
expect(serialize(root)).toEqual(
|
|
|
|
`<div>
|
|
|
|
<div id="test">
|
|
|
|
hello
|
|
|
|
</div>
|
|
|
|
</div>`
|
|
|
|
)
|
|
|
|
})
|
2018-10-02 01:15:07 +08:00
|
|
|
})
|