parentChain.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {
  2. h,
  3. Component,
  4. observable,
  5. nextTick,
  6. renderInstance
  7. } from '@vue/runtime-test'
  8. describe('Parent chain management', () => {
  9. it('should have correct $parent / $root / $children', async () => {
  10. let child: any
  11. let grandChildren: any[] = []
  12. const state = observable({ ok: true })
  13. class Parent extends Component {
  14. render() {
  15. return h(Child)
  16. }
  17. }
  18. class Child extends Component {
  19. created() {
  20. child = this
  21. }
  22. render() {
  23. return [state.ok ? h(GrandChild) : null, h(GrandChild)]
  24. }
  25. }
  26. class GrandChild extends Component {
  27. created() {
  28. grandChildren.push(this)
  29. }
  30. unmounted() {
  31. grandChildren.splice(grandChildren.indexOf(this), 1)
  32. }
  33. render() {
  34. return h('div')
  35. }
  36. }
  37. const parent = await renderInstance(Parent)
  38. expect(child.$parent).toBe(parent)
  39. expect(child.$root).toBe(parent)
  40. grandChildren.forEach(grandChild => {
  41. expect(grandChild.$parent).toBe(child)
  42. expect(grandChild.$root).toBe(parent)
  43. })
  44. expect(parent.$children).toEqual([child])
  45. expect(grandChildren.length).toBe(2)
  46. expect(child.$children).toEqual(grandChildren)
  47. state.ok = false
  48. await nextTick()
  49. expect(grandChildren.length).toBe(1)
  50. expect(child.$children).toEqual(grandChildren)
  51. })
  52. it('should have correct $parent / $root w/ functional component in between', async () => {
  53. let child: any
  54. let grandChildren: any[] = []
  55. const state = observable({ ok: true })
  56. class Parent extends Component {
  57. render() {
  58. return h(FunctionalChild)
  59. }
  60. }
  61. const FunctionalChild = () => h(Child)
  62. class Child extends Component {
  63. created() {
  64. child = this
  65. }
  66. render() {
  67. return [
  68. state.ok ? h(FunctionalGrandChild) : null,
  69. h(FunctionalGrandChild)
  70. ]
  71. }
  72. }
  73. const FunctionalGrandChild = () => h(GrandChild)
  74. class GrandChild extends Component {
  75. created() {
  76. grandChildren.push(this)
  77. }
  78. unmounted() {
  79. grandChildren.splice(grandChildren.indexOf(this), 1)
  80. }
  81. render() {}
  82. }
  83. const parent = await renderInstance(Parent)
  84. expect(child.$parent).toBe(parent)
  85. expect(child.$root).toBe(parent)
  86. grandChildren.forEach(grandChild => {
  87. expect(grandChild.$parent).toBe(child)
  88. expect(grandChild.$root).toBe(parent)
  89. })
  90. expect(parent.$children).toEqual([child])
  91. expect(grandChildren.length).toBe(2)
  92. expect(child.$children).toEqual(grandChildren)
  93. state.ok = false
  94. await nextTick()
  95. expect(grandChildren.length).toBe(1)
  96. expect(child.$children).toEqual(grandChildren)
  97. })
  98. })