use-deprecated.test.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { computed, defineComponent, nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. import { debugWarn } from '@element-plus/utils'
  5. import { useDeprecated } from '../use-deprecated'
  6. const AXIOM = 'Rem is the best girl'
  7. vi.mock('@element-plus/utils/error', async () => {
  8. return {
  9. ...(await vi.importActual<any>('@element-plus/utils/error')),
  10. debugWarn: vi.fn(),
  11. }
  12. })
  13. const DummyComponent = defineComponent({
  14. props: {
  15. shouldWarn: Boolean,
  16. },
  17. setup(props) {
  18. useDeprecated(
  19. {
  20. from: 'oldApi',
  21. replacement: 'newApi',
  22. scope: 'dummyComponent',
  23. version: 'some version',
  24. ref: '',
  25. },
  26. computed(() => props.shouldWarn)
  27. )
  28. return () => AXIOM
  29. },
  30. })
  31. describe('useDeprecated', () => {
  32. afterEach(() => {
  33. vi.restoreAllMocks()
  34. })
  35. it('should warn when condition is true', async () => {
  36. mount(DummyComponent, {
  37. props: {
  38. shouldWarn: true,
  39. },
  40. })
  41. await nextTick()
  42. expect(debugWarn).toHaveBeenCalled()
  43. })
  44. it('should not warn when condition is false', async () => {
  45. mount(DummyComponent)
  46. await nextTick()
  47. expect(debugWarn).not.toHaveBeenCalled()
  48. })
  49. })