hooks.spec.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { useState, h, nextTick, useEffect, Component } from '../src'
  2. import { renderInstance, serialize, triggerEvent } from '@vue/runtime-test'
  3. describe('hooks', () => {
  4. it('useState', async () => {
  5. class Counter extends Component {
  6. render() {
  7. const [count, setCount] = useState(0)
  8. return h(
  9. 'div',
  10. {
  11. onClick: () => {
  12. setCount(count + 1)
  13. }
  14. },
  15. count
  16. )
  17. }
  18. }
  19. const counter = await renderInstance(Counter)
  20. expect(serialize(counter.$el)).toBe(`<div>0</div>`)
  21. triggerEvent(counter.$el, 'click')
  22. await nextTick()
  23. expect(serialize(counter.$el)).toBe(`<div>1</div>`)
  24. })
  25. it('should be usable via hooks() method', async () => {
  26. class Counter extends Component {
  27. hooks() {
  28. const [count, setCount] = useState(0)
  29. return {
  30. count,
  31. setCount
  32. }
  33. }
  34. render() {
  35. const { count, setCount } = this as any
  36. return h(
  37. 'div',
  38. {
  39. onClick: () => {
  40. setCount(count + 1)
  41. }
  42. },
  43. count
  44. )
  45. }
  46. }
  47. const counter = await renderInstance(Counter)
  48. expect(serialize(counter.$el)).toBe(`<div>0</div>`)
  49. triggerEvent(counter.$el, 'click')
  50. await nextTick()
  51. expect(serialize(counter.$el)).toBe(`<div>1</div>`)
  52. })
  53. it('useEffect', async () => {
  54. let effect = -1
  55. class Counter extends Component {
  56. render() {
  57. const [count, setCount] = useState(0)
  58. useEffect(() => {
  59. effect = count
  60. })
  61. return h(
  62. 'div',
  63. {
  64. onClick: () => {
  65. setCount(count + 1)
  66. }
  67. },
  68. count
  69. )
  70. }
  71. }
  72. const counter = await renderInstance(Counter)
  73. expect(effect).toBe(0)
  74. triggerEvent(counter.$el, 'click')
  75. await nextTick()
  76. expect(effect).toBe(1)
  77. })
  78. it('useEffect with empty keys', async () => {
  79. // TODO
  80. })
  81. it('useEffect with keys', async () => {
  82. // TODO
  83. })
  84. it('useData', () => {
  85. // TODO
  86. })
  87. it('useMounted/useUnmounted/useUpdated', () => {
  88. // TODO
  89. })
  90. it('useWatch', () => {
  91. // TODO
  92. })
  93. it('useComputed', () => {
  94. // TODO
  95. })
  96. })