hooks.spec.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { withHooks, useState, h, nextTick, useEffect } from '../src'
  2. import { renderIntsance, 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 = renderIntsance(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('useEffect', async () => {
  24. let effect = -1
  25. const Counter = withHooks(() => {
  26. const [count, setCount] = useState(0)
  27. useEffect(() => {
  28. effect = count
  29. })
  30. return h(
  31. 'div',
  32. {
  33. onClick: () => {
  34. setCount(count + 1)
  35. }
  36. },
  37. count
  38. )
  39. })
  40. const counter = renderIntsance(Counter)
  41. expect(effect).toBe(0)
  42. triggerEvent(counter.$el, 'click')
  43. await nextTick()
  44. expect(effect).toBe(1)
  45. })
  46. it('useEffect with empty keys', async () => {
  47. // TODO
  48. })
  49. it('useEffect with keys', async () => {
  50. // TODO
  51. })
  52. })