hooks.spec.ts 2.7 KB

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