component.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {
  2. h,
  3. ref,
  4. render,
  5. nodeOps,
  6. nextTick,
  7. defineComponent
  8. } from '@vue/runtime-test'
  9. describe('renderer: component', () => {
  10. test.todo('should work')
  11. test.todo('shouldUpdateComponent')
  12. test.todo('componentProxy')
  13. test.todo('componentProps')
  14. describe('slots', () => {
  15. test('should respect $stable flag', async () => {
  16. const flag1 = ref(1)
  17. const flag2 = ref(2)
  18. const spy = jest.fn()
  19. const Child = () => {
  20. spy()
  21. return 'child'
  22. }
  23. const App = {
  24. setup() {
  25. return () => [
  26. flag1.value,
  27. h(
  28. Child,
  29. { n: flag2.value },
  30. {
  31. foo: () => 'foo',
  32. $stable: true
  33. }
  34. )
  35. ]
  36. }
  37. }
  38. render(h(App), nodeOps.createElement('div'))
  39. expect(spy).toHaveBeenCalledTimes(1)
  40. // parent re-render, props didn't change, slots are stable
  41. // -> child should not update
  42. flag1.value++
  43. await nextTick()
  44. expect(spy).toHaveBeenCalledTimes(1)
  45. // parent re-render, props changed
  46. // -> child should update
  47. flag2.value++
  48. await nextTick()
  49. expect(spy).toHaveBeenCalledTimes(2)
  50. })
  51. })
  52. test('emit', async () => {
  53. let noMatchEmitResult: any
  54. let singleEmitResult: any
  55. let multiEmitResult: any
  56. const Child = defineComponent({
  57. setup(_, { emit }) {
  58. noMatchEmitResult = emit('foo')
  59. singleEmitResult = emit('bar')
  60. multiEmitResult = emit('baz')
  61. return () => h('div')
  62. }
  63. })
  64. const App = {
  65. setup() {
  66. return () =>
  67. h(Child, {
  68. // emit triggering single handler
  69. onBar: () => 1,
  70. // emit triggering multiple handlers
  71. onBaz: [() => Promise.resolve(2), () => Promise.resolve(3)]
  72. })
  73. }
  74. }
  75. render(h(App), nodeOps.createElement('div'))
  76. // assert return values from emit
  77. expect(noMatchEmitResult).toMatchObject([])
  78. expect(singleEmitResult).toMatchObject([1])
  79. expect(await Promise.all(multiEmitResult)).toMatchObject([2, 3])
  80. })
  81. // for v-model:foo-bar usage in DOM templates
  82. test('emit update:xxx events should trigger kebab-case equivalent', () => {
  83. const Child = defineComponent({
  84. setup(_, { emit }) {
  85. emit('update:fooBar', 1)
  86. return () => h('div')
  87. }
  88. })
  89. const handler = jest.fn()
  90. const App = {
  91. setup() {
  92. return () =>
  93. h(Child, {
  94. 'onUpdate:foo-bar': handler
  95. })
  96. }
  97. }
  98. render(h(App), nodeOps.createElement('div'))
  99. expect(handler).toHaveBeenCalled()
  100. })
  101. })