parentChain.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. h,
  3. Component,
  4. render,
  5. nodeOps,
  6. observable,
  7. nextTick
  8. } from '@vue/runtime-test'
  9. describe('Parent chain management', () => {
  10. it('should have correct $parent / $root / $children', async () => {
  11. let child: any
  12. let grandChildren: any[] = []
  13. const state = observable({ ok: true })
  14. class Parent extends Component {
  15. render() {
  16. return h(Child)
  17. }
  18. }
  19. class Child extends Component {
  20. created() {
  21. child = this
  22. }
  23. render() {
  24. return [state.ok ? h(GrandChild) : null, h(GrandChild)]
  25. }
  26. }
  27. class GrandChild extends Component {
  28. created() {
  29. grandChildren.push(this)
  30. }
  31. unmounted() {
  32. grandChildren.splice(grandChildren.indexOf(this), 1)
  33. }
  34. render() {}
  35. }
  36. const root = nodeOps.createElement('div')
  37. const parent = render(h(Parent), root) as Component
  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 root = nodeOps.createElement('div')
  84. const parent = render(h(Parent), root) as Component
  85. expect(child.$parent).toBe(parent)
  86. expect(child.$root).toBe(parent)
  87. grandChildren.forEach(grandChild => {
  88. expect(grandChild.$parent).toBe(child)
  89. expect(grandChild.$root).toBe(parent)
  90. })
  91. expect(parent.$children).toEqual([child])
  92. expect(grandChildren.length).toBe(2)
  93. expect(child.$children).toEqual(grandChildren)
  94. state.ok = false
  95. await nextTick()
  96. expect(grandChildren.length).toBe(1)
  97. expect(child.$children).toEqual(grandChildren)
  98. })
  99. })