|
| 1 | +import { |
| 2 | + h, |
| 3 | + render, |
| 4 | + nodeOps, |
| 5 | + VNodeProps, |
| 6 | + TestElement, |
| 7 | + NodeTypes, |
| 8 | + VNode |
| 9 | +} from '@vue/runtime-test' |
| 10 | + |
| 11 | +describe('renderer: vnode hooks', () => { |
| 12 | + function assertHooks(hooks: VNodeProps, vnode1: VNode, vnode2: VNode) { |
| 13 | + const root = nodeOps.createElement('div') |
| 14 | + render(vnode1, root) |
| 15 | + expect(hooks.onVnodeBeforeMount).toHaveBeenCalledWith(vnode1, null) |
| 16 | + expect(hooks.onVnodeMounted).toHaveBeenCalledWith(vnode1, null) |
| 17 | + expect(hooks.onVnodeBeforeUpdate).not.toHaveBeenCalled() |
| 18 | + expect(hooks.onVnodeUpdated).not.toHaveBeenCalled() |
| 19 | + expect(hooks.onVnodeBeforeUnmount).not.toHaveBeenCalled() |
| 20 | + expect(hooks.onVnodeUnmounted).not.toHaveBeenCalled() |
| 21 | + |
| 22 | + // update |
| 23 | + render(vnode2, root) |
| 24 | + expect(hooks.onVnodeBeforeMount).toHaveBeenCalledTimes(1) |
| 25 | + expect(hooks.onVnodeMounted).toHaveBeenCalledTimes(1) |
| 26 | + expect(hooks.onVnodeBeforeUpdate).toHaveBeenCalledWith(vnode2, vnode1) |
| 27 | + expect(hooks.onVnodeUpdated).toHaveBeenCalledWith(vnode2, vnode1) |
| 28 | + expect(hooks.onVnodeBeforeUnmount).not.toHaveBeenCalled() |
| 29 | + expect(hooks.onVnodeUnmounted).not.toHaveBeenCalled() |
| 30 | + |
| 31 | + // unmount |
| 32 | + render(null, root) |
| 33 | + expect(hooks.onVnodeBeforeMount).toHaveBeenCalledTimes(1) |
| 34 | + expect(hooks.onVnodeMounted).toHaveBeenCalledTimes(1) |
| 35 | + expect(hooks.onVnodeBeforeUpdate).toHaveBeenCalledTimes(1) |
| 36 | + expect(hooks.onVnodeUpdated).toHaveBeenCalledTimes(1) |
| 37 | + expect(hooks.onVnodeBeforeUnmount).toHaveBeenCalledWith(vnode2, null) |
| 38 | + expect(hooks.onVnodeUnmounted).toHaveBeenCalledWith(vnode2, null) |
| 39 | + } |
| 40 | + |
| 41 | + test('should work on element', () => { |
| 42 | + const hooks: VNodeProps = { |
| 43 | + onVnodeBeforeMount: jest.fn(), |
| 44 | + onVnodeMounted: jest.fn(), |
| 45 | + onVnodeBeforeUpdate: jest.fn(vnode => { |
| 46 | + expect((vnode.el as TestElement).children[0]).toMatchObject({ |
| 47 | + type: NodeTypes.TEXT, |
| 48 | + text: 'foo' |
| 49 | + }) |
| 50 | + }), |
| 51 | + onVnodeUpdated: jest.fn(vnode => { |
| 52 | + expect((vnode.el as TestElement).children[0]).toMatchObject({ |
| 53 | + type: NodeTypes.TEXT, |
| 54 | + text: 'bar' |
| 55 | + }) |
| 56 | + }), |
| 57 | + onVnodeBeforeUnmount: jest.fn(), |
| 58 | + onVnodeUnmounted: jest.fn() |
| 59 | + } |
| 60 | + |
| 61 | + assertHooks(hooks, h('div', hooks, 'foo'), h('div', hooks, 'bar')) |
| 62 | + }) |
| 63 | + |
| 64 | + test('should work on component', () => { |
| 65 | + const Comp = (props: { msg: string }) => props.msg |
| 66 | + |
| 67 | + const hooks: VNodeProps = { |
| 68 | + onVnodeBeforeMount: jest.fn(), |
| 69 | + onVnodeMounted: jest.fn(), |
| 70 | + onVnodeBeforeUpdate: jest.fn(vnode => { |
| 71 | + expect(vnode.el as TestElement).toMatchObject({ |
| 72 | + type: NodeTypes.TEXT, |
| 73 | + text: 'foo' |
| 74 | + }) |
| 75 | + }), |
| 76 | + onVnodeUpdated: jest.fn(vnode => { |
| 77 | + expect(vnode.el as TestElement).toMatchObject({ |
| 78 | + type: NodeTypes.TEXT, |
| 79 | + text: 'bar' |
| 80 | + }) |
| 81 | + }), |
| 82 | + onVnodeBeforeUnmount: jest.fn(), |
| 83 | + onVnodeUnmounted: jest.fn() |
| 84 | + } |
| 85 | + |
| 86 | + assertHooks( |
| 87 | + hooks, |
| 88 | + h(Comp, { |
| 89 | + ...hooks, |
| 90 | + msg: 'foo' |
| 91 | + }), |
| 92 | + h(Comp, { |
| 93 | + ...hooks, |
| 94 | + msg: 'bar' |
| 95 | + }) |
| 96 | + ) |
| 97 | + }) |
| 98 | +}) |
0 commit comments