2019-10-11 23:48:20 +08:00
|
|
|
import {
|
2020-02-14 13:13:54 +08:00
|
|
|
createBlock,
|
|
|
|
createVNode,
|
|
|
|
openBlock,
|
2019-10-11 23:48:20 +08:00
|
|
|
Comment,
|
|
|
|
Fragment,
|
|
|
|
Text,
|
2020-02-14 13:13:54 +08:00
|
|
|
cloneVNode,
|
|
|
|
mergeProps,
|
2020-03-24 04:54:28 +08:00
|
|
|
normalizeVNode,
|
|
|
|
transformVNodeArgs
|
2020-02-14 13:13:54 +08:00
|
|
|
} from '../src/vnode'
|
2019-10-11 22:13:04 +08:00
|
|
|
import { Data } from '../src/component'
|
2020-07-28 10:58:37 +08:00
|
|
|
import { ShapeFlags, PatchFlags } from '@vue/shared'
|
2021-12-10 23:49:01 +08:00
|
|
|
import { h, reactive, isReactive, setBlockTracking, ref } from '../src'
|
2020-03-24 04:54:28 +08:00
|
|
|
import { createApp, nodeOps, serializeInner } from '@vue/runtime-test'
|
2021-03-06 00:10:06 +08:00
|
|
|
import { setCurrentRenderingInstance } from '../src/componentRenderContext'
|
2019-10-09 22:28:43 +08:00
|
|
|
|
2019-08-22 10:00:48 +08:00
|
|
|
describe('vnode', () => {
|
2019-10-09 22:28:43 +08:00
|
|
|
test('create with just tag', () => {
|
|
|
|
const vnode = createVNode('p')
|
|
|
|
expect(vnode.type).toBe('p')
|
|
|
|
expect(vnode.props).toBe(null)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('create with tag and props', () => {
|
|
|
|
const vnode = createVNode('p', {})
|
|
|
|
expect(vnode.type).toBe('p')
|
|
|
|
expect(vnode.props).toMatchObject({})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('create with tag, props and children', () => {
|
|
|
|
const vnode = createVNode('p', {}, ['foo'])
|
|
|
|
expect(vnode.type).toBe('p')
|
|
|
|
expect(vnode.props).toMatchObject({})
|
|
|
|
expect(vnode.children).toMatchObject(['foo'])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('create with 0 as props', () => {
|
|
|
|
const vnode = createVNode('p', null)
|
|
|
|
expect(vnode.type).toBe('p')
|
|
|
|
expect(vnode.props).toBe(null)
|
|
|
|
})
|
|
|
|
|
2020-10-14 03:27:21 +08:00
|
|
|
test('show warn when create with invalid type', () => {
|
|
|
|
const vnode = createVNode('')
|
|
|
|
expect('Invalid vnode type when creating vnode').toHaveBeenWarned()
|
|
|
|
expect(vnode.type).toBe(Comment)
|
|
|
|
})
|
|
|
|
|
2020-07-02 07:48:01 +08:00
|
|
|
test('create from an existing vnode', () => {
|
|
|
|
const vnode1 = createVNode('p', { id: 'foo' })
|
|
|
|
const vnode2 = createVNode(vnode1, { class: 'bar' }, 'baz')
|
|
|
|
expect(vnode2).toMatchObject({
|
|
|
|
type: 'p',
|
|
|
|
props: {
|
|
|
|
id: 'foo',
|
|
|
|
class: 'bar'
|
|
|
|
},
|
|
|
|
children: 'baz',
|
|
|
|
shapeFlag: ShapeFlags.ELEMENT | ShapeFlags.TEXT_CHILDREN
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-03-17 01:06:03 +08:00
|
|
|
test('vnode keys', () => {
|
|
|
|
for (const key of ['', 'a', 0, 1, NaN]) {
|
|
|
|
expect(createVNode('div', { key }).key).toBe(key)
|
2020-03-10 04:03:57 +08:00
|
|
|
}
|
2020-03-17 01:06:03 +08:00
|
|
|
expect(createVNode('div').key).toBe(null)
|
|
|
|
expect(createVNode('div', { key: undefined }).key).toBe(null)
|
2020-07-08 23:47:01 +08:00
|
|
|
expect(`VNode created with invalid key (NaN)`).toHaveBeenWarned()
|
2020-03-10 04:03:57 +08:00
|
|
|
})
|
|
|
|
|
2020-03-12 23:46:32 +08:00
|
|
|
test('create with class component', () => {
|
|
|
|
class Component {
|
|
|
|
$props: any
|
|
|
|
static __vccOpts = { template: '<div />' }
|
|
|
|
}
|
|
|
|
const vnode = createVNode(Component)
|
|
|
|
expect(vnode.type).toEqual(Component.__vccOpts)
|
|
|
|
})
|
|
|
|
|
2019-10-09 22:28:43 +08:00
|
|
|
describe('class normalization', () => {
|
|
|
|
test('string', () => {
|
|
|
|
const vnode = createVNode('p', { class: 'foo baz' })
|
|
|
|
expect(vnode.props).toMatchObject({ class: 'foo baz' })
|
|
|
|
})
|
|
|
|
|
|
|
|
test('array<string>', () => {
|
|
|
|
const vnode = createVNode('p', { class: ['foo', 'baz'] })
|
|
|
|
expect(vnode.props).toMatchObject({ class: 'foo baz' })
|
|
|
|
})
|
|
|
|
|
|
|
|
test('array<object>', () => {
|
|
|
|
const vnode = createVNode('p', {
|
|
|
|
class: [{ foo: 'foo' }, { baz: 'baz' }]
|
|
|
|
})
|
|
|
|
expect(vnode.props).toMatchObject({ class: 'foo baz' })
|
|
|
|
})
|
|
|
|
|
|
|
|
test('object', () => {
|
|
|
|
const vnode = createVNode('p', { class: { foo: 'foo', baz: 'baz' } })
|
|
|
|
expect(vnode.props).toMatchObject({ class: 'foo baz' })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('style normalization', () => {
|
|
|
|
test('array', () => {
|
|
|
|
const vnode = createVNode('p', {
|
|
|
|
style: [{ foo: 'foo' }, { baz: 'baz' }]
|
|
|
|
})
|
|
|
|
expect(vnode.props).toMatchObject({ style: { foo: 'foo', baz: 'baz' } })
|
|
|
|
})
|
|
|
|
|
|
|
|
test('object', () => {
|
|
|
|
const vnode = createVNode('p', { style: { foo: 'foo', baz: 'baz' } })
|
|
|
|
expect(vnode.props).toMatchObject({ style: { foo: 'foo', baz: 'baz' } })
|
|
|
|
})
|
|
|
|
})
|
2019-08-22 10:00:48 +08:00
|
|
|
|
2019-10-09 22:28:43 +08:00
|
|
|
describe('children normalization', () => {
|
|
|
|
const nop = jest.fn
|
2019-08-22 10:00:48 +08:00
|
|
|
|
2019-10-09 22:28:43 +08:00
|
|
|
test('null', () => {
|
|
|
|
const vnode = createVNode('p', null, null)
|
|
|
|
expect(vnode.children).toBe(null)
|
|
|
|
expect(vnode.shapeFlag).toBe(ShapeFlags.ELEMENT)
|
|
|
|
})
|
2019-08-22 10:00:48 +08:00
|
|
|
|
2019-10-09 22:28:43 +08:00
|
|
|
test('array', () => {
|
|
|
|
const vnode = createVNode('p', null, ['foo'])
|
|
|
|
expect(vnode.children).toMatchObject(['foo'])
|
|
|
|
expect(vnode.shapeFlag).toBe(
|
2020-03-24 02:47:04 +08:00
|
|
|
ShapeFlags.ELEMENT | ShapeFlags.ARRAY_CHILDREN
|
2019-10-09 22:28:43 +08:00
|
|
|
)
|
|
|
|
})
|
2019-08-22 10:00:48 +08:00
|
|
|
|
2019-10-09 22:28:43 +08:00
|
|
|
test('object', () => {
|
2020-09-02 00:38:47 +08:00
|
|
|
const vnode = createVNode({}, null, { foo: 'foo' })
|
2019-10-09 22:28:43 +08:00
|
|
|
expect(vnode.children).toMatchObject({ foo: 'foo' })
|
|
|
|
expect(vnode.shapeFlag).toBe(
|
2020-09-02 00:38:47 +08:00
|
|
|
ShapeFlags.STATEFUL_COMPONENT | ShapeFlags.SLOTS_CHILDREN
|
2019-10-09 22:28:43 +08:00
|
|
|
)
|
|
|
|
})
|
2019-08-22 10:00:48 +08:00
|
|
|
|
2019-10-09 22:28:43 +08:00
|
|
|
test('function', () => {
|
|
|
|
const vnode = createVNode('p', null, nop)
|
|
|
|
expect(vnode.children).toMatchObject({ default: nop })
|
|
|
|
expect(vnode.shapeFlag).toBe(
|
2020-03-24 02:47:04 +08:00
|
|
|
ShapeFlags.ELEMENT | ShapeFlags.SLOTS_CHILDREN
|
2019-10-09 22:28:43 +08:00
|
|
|
)
|
|
|
|
})
|
2019-08-22 10:00:48 +08:00
|
|
|
|
2019-10-09 22:28:43 +08:00
|
|
|
test('string', () => {
|
|
|
|
const vnode = createVNode('p', null, 'foo')
|
|
|
|
expect(vnode.children).toBe('foo')
|
|
|
|
expect(vnode.shapeFlag).toBe(
|
2020-03-24 02:47:04 +08:00
|
|
|
ShapeFlags.ELEMENT | ShapeFlags.TEXT_CHILDREN
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('element with slots', () => {
|
|
|
|
const children = [createVNode('span', null, 'hello')]
|
|
|
|
const vnode = createVNode('div', null, {
|
|
|
|
default: () => children
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(vnode.children).toBe(children)
|
|
|
|
expect(vnode.shapeFlag).toBe(
|
|
|
|
ShapeFlags.ELEMENT | ShapeFlags.ARRAY_CHILDREN
|
2019-10-09 22:28:43 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-08-22 10:00:48 +08:00
|
|
|
|
2019-10-11 23:41:28 +08:00
|
|
|
test('normalizeVNode', () => {
|
|
|
|
// null / undefined -> Comment
|
|
|
|
expect(normalizeVNode(null)).toMatchObject({ type: Comment })
|
|
|
|
expect(normalizeVNode(undefined)).toMatchObject({ type: Comment })
|
|
|
|
|
2020-01-07 00:57:19 +08:00
|
|
|
// boolean -> Comment
|
|
|
|
// this is for usage like `someBoolean && h('div')` and behavior consistency
|
|
|
|
// with 2.x (#574)
|
|
|
|
expect(normalizeVNode(true)).toMatchObject({ type: Comment })
|
|
|
|
expect(normalizeVNode(false)).toMatchObject({ type: Comment })
|
|
|
|
|
2019-10-11 23:41:28 +08:00
|
|
|
// array -> Fragment
|
|
|
|
expect(normalizeVNode(['foo'])).toMatchObject({ type: Fragment })
|
|
|
|
|
|
|
|
// VNode -> VNode
|
|
|
|
const vnode = createVNode('div')
|
|
|
|
expect(normalizeVNode(vnode)).toBe(vnode)
|
|
|
|
|
|
|
|
// mounted VNode -> cloned VNode
|
|
|
|
const mounted = createVNode('div')
|
|
|
|
mounted.el = {}
|
2019-10-12 03:09:57 +08:00
|
|
|
const normalized = normalizeVNode(mounted)
|
|
|
|
expect(normalized).not.toBe(mounted)
|
2019-10-30 10:28:38 +08:00
|
|
|
expect(normalized).toEqual(mounted)
|
2019-10-11 23:41:28 +08:00
|
|
|
|
|
|
|
// primitive types
|
|
|
|
expect(normalizeVNode('foo')).toMatchObject({ type: Text, children: `foo` })
|
|
|
|
expect(normalizeVNode(1)).toMatchObject({ type: Text, children: `1` })
|
|
|
|
})
|
2019-08-22 10:00:48 +08:00
|
|
|
|
2019-10-11 23:48:20 +08:00
|
|
|
test('type shapeFlag inference', () => {
|
|
|
|
expect(createVNode('div').shapeFlag).toBe(ShapeFlags.ELEMENT)
|
|
|
|
expect(createVNode({}).shapeFlag).toBe(ShapeFlags.STATEFUL_COMPONENT)
|
|
|
|
expect(createVNode(() => {}).shapeFlag).toBe(
|
|
|
|
ShapeFlags.FUNCTIONAL_COMPONENT
|
|
|
|
)
|
|
|
|
expect(createVNode(Text).shapeFlag).toBe(0)
|
|
|
|
})
|
2019-08-22 10:00:48 +08:00
|
|
|
|
2019-10-11 23:48:20 +08:00
|
|
|
test('cloneVNode', () => {
|
|
|
|
const node1 = createVNode('div', { foo: 1 }, null)
|
|
|
|
expect(cloneVNode(node1)).toEqual(node1)
|
|
|
|
|
|
|
|
const node2 = createVNode({}, null, [node1])
|
|
|
|
const cloned2 = cloneVNode(node2)
|
|
|
|
expect(cloned2).toEqual(node2)
|
|
|
|
expect(cloneVNode(node2)).toEqual(node2)
|
|
|
|
expect(cloneVNode(node2)).toEqual(cloned2)
|
2020-06-16 03:57:37 +08:00
|
|
|
})
|
2020-04-25 00:42:46 +08:00
|
|
|
|
2020-06-16 03:57:37 +08:00
|
|
|
test('cloneVNode key normalization', () => {
|
2020-05-01 21:42:58 +08:00
|
|
|
// #1041 should use resolved key/ref
|
2020-04-25 00:42:46 +08:00
|
|
|
expect(cloneVNode(createVNode('div', { key: 1 })).key).toBe(1)
|
|
|
|
expect(cloneVNode(createVNode('div', { key: 1 }), { key: 2 }).key).toBe(2)
|
|
|
|
expect(cloneVNode(createVNode('div'), { key: 2 }).key).toBe(2)
|
2020-06-16 03:57:37 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// ref normalizes to [currentRenderingInstance, ref]
|
|
|
|
test('cloneVNode ref normalization', () => {
|
2021-03-06 00:10:06 +08:00
|
|
|
const mockInstance1 = { type: {} } as any
|
|
|
|
const mockInstance2 = { type: {} } as any
|
2020-06-16 03:57:37 +08:00
|
|
|
|
|
|
|
setCurrentRenderingInstance(mockInstance1)
|
|
|
|
const original = createVNode('div', { ref: 'foo' })
|
2021-12-10 23:49:01 +08:00
|
|
|
expect(original.ref).toMatchObject({
|
|
|
|
i: mockInstance1,
|
|
|
|
r: 'foo',
|
|
|
|
f: false
|
|
|
|
})
|
2020-06-16 03:57:37 +08:00
|
|
|
|
|
|
|
// clone and preserve original ref
|
|
|
|
const cloned1 = cloneVNode(original)
|
2021-12-10 23:49:01 +08:00
|
|
|
expect(cloned1.ref).toMatchObject({ i: mockInstance1, r: 'foo', f: false })
|
2020-06-16 03:57:37 +08:00
|
|
|
|
|
|
|
// cloning with new ref, but with same context instance
|
|
|
|
const cloned2 = cloneVNode(original, { ref: 'bar' })
|
2021-12-10 23:49:01 +08:00
|
|
|
expect(cloned2.ref).toMatchObject({ i: mockInstance1, r: 'bar', f: false })
|
2020-06-16 03:57:37 +08:00
|
|
|
|
|
|
|
// cloning and adding ref to original that has no ref
|
|
|
|
const original2 = createVNode('div')
|
|
|
|
const cloned3 = cloneVNode(original2, { ref: 'bar' })
|
2021-12-10 23:49:01 +08:00
|
|
|
expect(cloned3.ref).toMatchObject({ i: mockInstance1, r: 'bar', f: false })
|
2020-06-16 03:57:37 +08:00
|
|
|
|
|
|
|
// cloning with different context instance
|
|
|
|
setCurrentRenderingInstance(mockInstance2)
|
|
|
|
|
|
|
|
// clone and preserve original ref
|
|
|
|
const cloned4 = cloneVNode(original)
|
|
|
|
// #1311 should preserve original context instance!
|
2021-12-10 23:49:01 +08:00
|
|
|
expect(cloned4.ref).toMatchObject({ i: mockInstance1, r: 'foo', f: false })
|
2020-06-16 03:57:37 +08:00
|
|
|
|
|
|
|
// cloning with new ref, but with same context instance
|
|
|
|
const cloned5 = cloneVNode(original, { ref: 'bar' })
|
2020-07-08 18:32:42 +08:00
|
|
|
// new ref should use current context instance and overwrite original
|
2021-12-10 23:49:01 +08:00
|
|
|
expect(cloned5.ref).toMatchObject({ i: mockInstance2, r: 'bar', f: false })
|
2020-06-16 03:57:37 +08:00
|
|
|
|
|
|
|
// cloning and adding ref to original that has no ref
|
|
|
|
const cloned6 = cloneVNode(original2, { ref: 'bar' })
|
2021-12-10 23:49:01 +08:00
|
|
|
expect(cloned6.ref).toMatchObject({ i: mockInstance2, r: 'bar', f: false })
|
|
|
|
|
|
|
|
const original3 = createVNode('div', { ref: 'foo', ref_for: true })
|
|
|
|
expect(original3.ref).toMatchObject({
|
|
|
|
i: mockInstance2,
|
|
|
|
r: 'foo',
|
|
|
|
f: true
|
|
|
|
})
|
|
|
|
const cloned7 = cloneVNode(original3, { ref: 'bar', ref_for: true })
|
|
|
|
expect(cloned7.ref).toMatchObject({ i: mockInstance2, r: 'bar', f: true })
|
|
|
|
|
|
|
|
const r = ref()
|
|
|
|
const original4 = createVNode('div', { ref: r, ref_key: 'foo' })
|
|
|
|
expect(original4.ref).toMatchObject({
|
|
|
|
i: mockInstance2,
|
|
|
|
r,
|
|
|
|
k: 'foo'
|
|
|
|
})
|
|
|
|
const cloned8 = cloneVNode(original4)
|
|
|
|
expect(cloned8.ref).toMatchObject({ i: mockInstance2, r, k: 'foo' })
|
2020-09-15 06:55:00 +08:00
|
|
|
|
|
|
|
setCurrentRenderingInstance(null)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('cloneVNode ref merging', () => {
|
2021-03-06 00:10:06 +08:00
|
|
|
const mockInstance1 = { type: {} } as any
|
|
|
|
const mockInstance2 = { type: {} } as any
|
2020-09-15 06:55:00 +08:00
|
|
|
|
|
|
|
setCurrentRenderingInstance(mockInstance1)
|
|
|
|
const original = createVNode('div', { ref: 'foo' })
|
2021-12-10 23:49:01 +08:00
|
|
|
expect(original.ref).toMatchObject({ i: mockInstance1, r: 'foo', f: false })
|
2020-09-15 06:55:00 +08:00
|
|
|
|
|
|
|
// clone and preserve original ref
|
|
|
|
setCurrentRenderingInstance(mockInstance2)
|
|
|
|
const cloned1 = cloneVNode(original, { ref: 'bar' }, true)
|
2021-12-10 23:49:01 +08:00
|
|
|
expect(cloned1.ref).toMatchObject([
|
|
|
|
{ i: mockInstance1, r: 'foo', f: false },
|
|
|
|
{ i: mockInstance2, r: 'bar', f: false }
|
2020-09-15 06:55:00 +08:00
|
|
|
])
|
2020-04-25 00:42:46 +08:00
|
|
|
|
2020-06-16 03:57:37 +08:00
|
|
|
setCurrentRenderingInstance(null)
|
2019-10-11 23:48:20 +08:00
|
|
|
})
|
2019-08-23 05:13:25 +08:00
|
|
|
|
2020-08-26 21:37:28 +08:00
|
|
|
test('cloneVNode class normalization', () => {
|
|
|
|
const vnode = createVNode('div')
|
|
|
|
const expectedProps = {
|
|
|
|
class: 'a b'
|
|
|
|
}
|
|
|
|
expect(cloneVNode(vnode, { class: 'a b' }).props).toMatchObject(
|
|
|
|
expectedProps
|
|
|
|
)
|
|
|
|
expect(cloneVNode(vnode, { class: ['a', 'b'] }).props).toMatchObject(
|
|
|
|
expectedProps
|
|
|
|
)
|
|
|
|
expect(
|
|
|
|
cloneVNode(vnode, { class: { a: true, b: true } }).props
|
|
|
|
).toMatchObject(expectedProps)
|
|
|
|
expect(
|
|
|
|
cloneVNode(vnode, { class: [{ a: true, b: true }] }).props
|
|
|
|
).toMatchObject(expectedProps)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('cloneVNode style normalization', () => {
|
|
|
|
const vnode = createVNode('div')
|
|
|
|
const expectedProps = {
|
|
|
|
style: {
|
|
|
|
color: 'blue',
|
|
|
|
width: '300px'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(
|
|
|
|
cloneVNode(vnode, { style: 'color: blue; width: 300px;' }).props
|
|
|
|
).toMatchObject(expectedProps)
|
|
|
|
expect(
|
|
|
|
cloneVNode(vnode, {
|
|
|
|
style: {
|
|
|
|
color: 'blue',
|
|
|
|
width: '300px'
|
|
|
|
}
|
|
|
|
}).props
|
|
|
|
).toMatchObject(expectedProps)
|
|
|
|
expect(
|
|
|
|
cloneVNode(vnode, {
|
|
|
|
style: [
|
|
|
|
{
|
|
|
|
color: 'blue',
|
|
|
|
width: '300px'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}).props
|
|
|
|
).toMatchObject(expectedProps)
|
|
|
|
})
|
|
|
|
|
2019-10-11 22:13:04 +08:00
|
|
|
describe('mergeProps', () => {
|
|
|
|
test('class', () => {
|
2021-07-21 01:58:43 +08:00
|
|
|
let props1: Data = { class: { c: true } }
|
2019-10-11 22:13:04 +08:00
|
|
|
let props2: Data = { class: ['cc'] }
|
|
|
|
let props3: Data = { class: [{ ccc: true }] }
|
|
|
|
let props4: Data = { class: { cccc: true } }
|
|
|
|
expect(mergeProps(props1, props2, props3, props4)).toMatchObject({
|
|
|
|
class: 'c cc ccc cccc'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('style', () => {
|
|
|
|
let props1: Data = {
|
2021-07-21 01:58:43 +08:00
|
|
|
style: [
|
|
|
|
{
|
|
|
|
color: 'red',
|
|
|
|
fontSize: 10
|
|
|
|
}
|
|
|
|
]
|
2019-10-11 22:13:04 +08:00
|
|
|
}
|
|
|
|
let props2: Data = {
|
|
|
|
style: [
|
|
|
|
{
|
|
|
|
color: 'blue',
|
2019-11-09 11:41:55 +08:00
|
|
|
width: '200px'
|
2019-10-11 22:13:04 +08:00
|
|
|
},
|
|
|
|
{
|
2019-11-09 11:41:55 +08:00
|
|
|
width: '300px',
|
2019-10-11 22:13:04 +08:00
|
|
|
height: '300px',
|
|
|
|
fontSize: 30
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
expect(mergeProps(props1, props2)).toMatchObject({
|
|
|
|
style: {
|
|
|
|
color: 'blue',
|
2019-11-09 11:41:55 +08:00
|
|
|
width: '300px',
|
2019-10-11 22:13:04 +08:00
|
|
|
height: '300px',
|
|
|
|
fontSize: 30
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 23:22:49 +08:00
|
|
|
|
|
|
|
test('style w/ strings', () => {
|
2020-05-06 23:14:07 +08:00
|
|
|
let props1: Data = {
|
|
|
|
style: 'width:100px;right:10;top:10'
|
|
|
|
}
|
|
|
|
let props2: Data = {
|
|
|
|
style: [
|
|
|
|
{
|
|
|
|
color: 'blue',
|
|
|
|
width: '200px'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
width: '300px',
|
|
|
|
height: '300px',
|
|
|
|
fontSize: 30
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
expect(mergeProps(props1, props2)).toMatchObject({
|
|
|
|
style: {
|
|
|
|
color: 'blue',
|
|
|
|
width: '300px',
|
|
|
|
height: '300px',
|
|
|
|
fontSize: 30,
|
2020-05-06 23:22:49 +08:00
|
|
|
right: '10',
|
|
|
|
top: '10'
|
2020-05-06 23:14:07 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-10-11 22:13:04 +08:00
|
|
|
|
|
|
|
test('handlers', () => {
|
2021-07-20 06:24:18 +08:00
|
|
|
let clickHandler1 = function () {}
|
|
|
|
let clickHandler2 = function () {}
|
|
|
|
let focusHandler2 = function () {}
|
2019-10-11 22:13:04 +08:00
|
|
|
|
2020-05-01 21:42:58 +08:00
|
|
|
let props1: Data = { onClick: clickHandler1 }
|
|
|
|
let props2: Data = { onClick: clickHandler2, onFocus: focusHandler2 }
|
2019-10-11 22:13:04 +08:00
|
|
|
expect(mergeProps(props1, props2)).toMatchObject({
|
2020-05-01 21:42:58 +08:00
|
|
|
onClick: [clickHandler1, clickHandler2],
|
|
|
|
onFocus: focusHandler2
|
2019-10-11 22:13:04 +08:00
|
|
|
})
|
2022-01-21 14:13:29 +08:00
|
|
|
let props3: Data = { onClick: undefined }
|
|
|
|
expect(mergeProps(props1, props3)).toMatchObject({
|
|
|
|
onClick: clickHandler1
|
|
|
|
})
|
2019-10-11 22:13:04 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('default', () => {
|
|
|
|
let props1: Data = { foo: 'c' }
|
|
|
|
let props2: Data = { foo: {}, bar: ['cc'] }
|
|
|
|
let props3: Data = { baz: { ccc: true } }
|
|
|
|
expect(mergeProps(props1, props2, props3)).toMatchObject({
|
|
|
|
foo: {},
|
|
|
|
bar: ['cc'],
|
|
|
|
baz: { ccc: true }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-12-31 00:25:44 +08:00
|
|
|
|
|
|
|
describe('dynamic children', () => {
|
2020-01-22 23:45:27 +08:00
|
|
|
test('with patchFlags', () => {
|
2019-12-31 00:25:44 +08:00
|
|
|
const hoist = createVNode('div')
|
|
|
|
let vnode1
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, [
|
|
|
|
hoist,
|
|
|
|
(vnode1 = createVNode('div', null, 'text', PatchFlags.TEXT))
|
|
|
|
]))
|
2019-12-31 00:25:44 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([vnode1])
|
|
|
|
})
|
|
|
|
|
2020-02-14 07:28:40 +08:00
|
|
|
test('should not track vnodes with only HYDRATE_EVENTS flag', () => {
|
|
|
|
const hoist = createVNode('div')
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, [
|
|
|
|
hoist,
|
|
|
|
createVNode('div', null, 'text', PatchFlags.HYDRATE_EVENTS)
|
|
|
|
]))
|
2020-02-14 07:28:40 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([])
|
|
|
|
})
|
|
|
|
|
2019-12-31 00:25:44 +08:00
|
|
|
test('many times call openBlock', () => {
|
|
|
|
const hoist = createVNode('div')
|
|
|
|
let vnode1, vnode2, vnode3
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
2019-12-31 00:25:44 +08:00
|
|
|
createBlock('div', null, [
|
|
|
|
hoist,
|
2021-07-20 06:24:18 +08:00
|
|
|
(vnode1 = createVNode('div', null, 'text', PatchFlags.TEXT)),
|
|
|
|
(vnode2 =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, [
|
|
|
|
hoist,
|
|
|
|
(vnode3 = createVNode('div', null, 'text', PatchFlags.TEXT))
|
|
|
|
])))
|
|
|
|
]))
|
2019-12-31 00:25:44 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([vnode1, vnode2])
|
|
|
|
expect(vnode2.dynamicChildren).toStrictEqual([vnode3])
|
|
|
|
})
|
2020-01-22 23:45:27 +08:00
|
|
|
|
|
|
|
test('with stateful component', () => {
|
|
|
|
const hoist = createVNode('div')
|
|
|
|
let vnode1
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, [
|
|
|
|
hoist,
|
|
|
|
(vnode1 = createVNode({}, null, 'text'))
|
|
|
|
]))
|
2020-01-22 23:45:27 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([vnode1])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with functional component', () => {
|
|
|
|
const hoist = createVNode('div')
|
|
|
|
let vnode1
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, [
|
|
|
|
hoist,
|
|
|
|
(vnode1 = createVNode(() => {}, null, 'text'))
|
|
|
|
]))
|
2020-01-22 23:45:27 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([vnode1])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with suspense', () => {
|
|
|
|
const hoist = createVNode('div')
|
|
|
|
let vnode1
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, [
|
|
|
|
hoist,
|
|
|
|
(vnode1 = createVNode(() => {}, null, 'text'))
|
|
|
|
]))
|
2020-01-22 23:45:27 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([vnode1])
|
|
|
|
})
|
2020-04-25 00:18:51 +08:00
|
|
|
|
|
|
|
// #1039
|
|
|
|
// <component :is="foo">{{ bar }}</component>
|
|
|
|
// - content is compiled as slot
|
2020-05-01 21:42:58 +08:00
|
|
|
// - dynamic component resolves to plain element, but as a block
|
2020-04-25 00:18:51 +08:00
|
|
|
// - block creation disables its own tracking, accidentally causing the
|
|
|
|
// slot content (called during the block node creation) to be missed
|
|
|
|
test('element block should track normalized slot children', () => {
|
|
|
|
const hoist = createVNode('div')
|
2020-12-03 06:05:30 +08:00
|
|
|
let vnode1: any
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, {
|
|
|
|
default: () => {
|
|
|
|
return [
|
|
|
|
hoist,
|
|
|
|
(vnode1 = createVNode('div', null, 'text', PatchFlags.TEXT))
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}))
|
2020-04-25 00:18:51 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([vnode1])
|
|
|
|
})
|
2020-05-18 22:20:05 +08:00
|
|
|
|
|
|
|
test('openBlock w/ disableTracking: true', () => {
|
|
|
|
const hoist = createVNode('div')
|
|
|
|
let vnode1
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, [
|
|
|
|
// a v-for fragment block generated by the compiler
|
|
|
|
// disables tracking because it always diffs its
|
|
|
|
// children.
|
|
|
|
(vnode1 =
|
|
|
|
(openBlock(true),
|
|
|
|
createBlock(Fragment, null, [
|
|
|
|
hoist,
|
|
|
|
/*vnode2*/ createVNode(() => {}, null, 'text')
|
|
|
|
])))
|
|
|
|
]))
|
2020-05-18 22:20:05 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([vnode1])
|
|
|
|
expect(vnode1.dynamicChildren).toStrictEqual([])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('openBlock without disableTracking: true', () => {
|
|
|
|
const hoist = createVNode('div')
|
|
|
|
let vnode1, vnode2
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, [
|
|
|
|
(vnode1 =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock(Fragment, null, [
|
|
|
|
hoist,
|
|
|
|
(vnode2 = createVNode(() => {}, null, 'text'))
|
|
|
|
])))
|
|
|
|
]))
|
2020-05-18 22:20:05 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([vnode1])
|
|
|
|
expect(vnode1.dynamicChildren).toStrictEqual([vnode2])
|
|
|
|
})
|
2020-09-03 00:15:36 +08:00
|
|
|
|
|
|
|
test('should not track openBlock() when tracking is disabled', () => {
|
|
|
|
let vnode1
|
2021-07-20 06:24:18 +08:00
|
|
|
const vnode =
|
|
|
|
(openBlock(),
|
|
|
|
createBlock('div', null, [
|
|
|
|
setBlockTracking(-1),
|
|
|
|
(vnode1 = (openBlock(), createBlock('div'))),
|
|
|
|
setBlockTracking(1),
|
|
|
|
vnode1
|
|
|
|
]))
|
2020-09-03 00:15:36 +08:00
|
|
|
expect(vnode.dynamicChildren).toStrictEqual([])
|
|
|
|
})
|
2019-12-31 00:25:44 +08:00
|
|
|
})
|
2020-03-24 04:54:28 +08:00
|
|
|
|
|
|
|
describe('transformVNodeArgs', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
// reset
|
|
|
|
transformVNodeArgs()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('no-op pass through', () => {
|
|
|
|
transformVNodeArgs(args => args)
|
|
|
|
const vnode = createVNode('div', { id: 'foo' }, 'hello')
|
|
|
|
expect(vnode).toMatchObject({
|
|
|
|
type: 'div',
|
|
|
|
props: { id: 'foo' },
|
|
|
|
children: 'hello',
|
|
|
|
shapeFlag: ShapeFlags.ELEMENT | ShapeFlags.TEXT_CHILDREN
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('direct override', () => {
|
|
|
|
transformVNodeArgs(() => ['div', { id: 'foo' }, 'hello'])
|
|
|
|
const vnode = createVNode('p')
|
|
|
|
expect(vnode).toMatchObject({
|
|
|
|
type: 'div',
|
|
|
|
props: { id: 'foo' },
|
|
|
|
children: 'hello',
|
|
|
|
shapeFlag: ShapeFlags.ELEMENT | ShapeFlags.TEXT_CHILDREN
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('receive component instance as 2nd arg', () => {
|
|
|
|
transformVNodeArgs((args, instance) => {
|
|
|
|
if (instance) {
|
|
|
|
return ['h1', null, instance.type.name]
|
|
|
|
} else {
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const App = {
|
|
|
|
// this will be the name of the component in the h1
|
|
|
|
name: 'Root Component',
|
|
|
|
render() {
|
|
|
|
return h('p') // this will be overwritten by the transform
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp(App).mount(root)
|
|
|
|
expect(serializeInner(root)).toBe('<h1>Root Component</h1>')
|
|
|
|
})
|
2020-05-03 04:16:51 +08:00
|
|
|
|
|
|
|
test('should not be observable', () => {
|
|
|
|
const a = createVNode('div')
|
|
|
|
const b = reactive(a)
|
|
|
|
expect(b).toBe(a)
|
|
|
|
expect(isReactive(b)).toBe(false)
|
|
|
|
})
|
2020-03-24 04:54:28 +08:00
|
|
|
})
|
2019-08-22 10:00:48 +08:00
|
|
|
})
|