component.spec.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. onBar: () => 1,
  69. onBaz: [() => Promise.resolve(2), () => Promise.resolve(3)]
  70. })
  71. }
  72. }
  73. render(h(App), nodeOps.createElement('div'))
  74. expect(noMatchEmitResult).toMatchObject([])
  75. expect(singleEmitResult).toMatchObject([1])
  76. expect(await Promise.all(multiEmitResult)).toMatchObject([2, 3])
  77. })
  78. })