use-id.test.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { config, mount } from '@vue/test-utils'
  2. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  3. import { ID_INJECTION_KEY, useId, useIdInjection } from '../use-id'
  4. describe('no injection value', () => {
  5. afterEach(() => {
  6. document.body.innerHTML = ''
  7. })
  8. it('useIdInjection', () => {
  9. const wrapper = mount({
  10. setup() {
  11. const idInjection = useIdInjection()
  12. return idInjection
  13. },
  14. template: '<div></div>',
  15. })
  16. expect(wrapper.vm.prefix).toMatch(/^\d{0,4}$/)
  17. expect(wrapper.vm.current).toBe(0)
  18. })
  19. it('useId', () => {
  20. const wrapper = mount({
  21. setup() {
  22. const id = useId()
  23. return { id }
  24. },
  25. })
  26. expect(wrapper.vm.id).toMatch(/^el-id-\d{0,4}-\d+$/)
  27. })
  28. })
  29. describe('with injection value', () => {
  30. beforeEach(() => {
  31. config.global.provide = {
  32. [ID_INJECTION_KEY as symbol]: {
  33. prefix: 1024,
  34. current: 0,
  35. },
  36. }
  37. })
  38. afterEach(() => {
  39. document.body.innerHTML = ''
  40. config.global.provide = {}
  41. })
  42. it('useIdInjection', () => {
  43. const wrapper = mount({
  44. setup() {
  45. const idInjection = useIdInjection()
  46. return idInjection
  47. },
  48. })
  49. expect(wrapper.vm.prefix).toBe(1024)
  50. expect(wrapper.vm.current).toBe(0)
  51. })
  52. it('useId', () => {
  53. const wrapper = mount({
  54. setup() {
  55. const id = useId()
  56. return { id }
  57. },
  58. })
  59. expect(wrapper.vm.id).toBe('el-id-1024-0')
  60. })
  61. })