progress.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { nextTick, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { describe, expect, test } from 'vitest'
  4. import { CircleClose } from '@element-plus/icons-vue'
  5. import Progress from '../src/progress.vue'
  6. describe('Progress.vue', () => {
  7. test('percent', () => {
  8. const wrapper = mount(() => <Progress percentage={66} />)
  9. expect(wrapper.find('.el-progress__text').text()).toBe('66%')
  10. expect(wrapper.find('.el-progress-bar__inner').attributes('style')).toBe(
  11. 'width: 66%; animation-duration: 3s;'
  12. )
  13. })
  14. test('status', () => {
  15. const wrapper = mount(() => <Progress status="exception" />)
  16. expect(wrapper.classes()).toContain('is-exception')
  17. expect(wrapper.findComponent(CircleClose).exists()).toBe(true)
  18. })
  19. test('text inside', () => {
  20. const wrapper = mount(() => <Progress textInside />)
  21. expect(wrapper.classes()).toContain('el-progress--text-inside')
  22. })
  23. test('stroke width', () => {
  24. const wrapper = mount(() => <Progress strokeWidth={7} />)
  25. expect(wrapper.find('.el-progress-bar__outer').attributes('style')).toBe(
  26. 'height: 7px;'
  27. )
  28. })
  29. test('show text', () => {
  30. const wrapper = mount(() => <Progress showText={false} />)
  31. expect(wrapper.find('.el-progress__text').exists()).toBe(false)
  32. })
  33. test('circle', () => {
  34. const wrapper = mount(() => <Progress type="circle" />)
  35. expect(wrapper.classes()).toContain('el-progress--circle')
  36. })
  37. test('dashboard', () => {
  38. const wrapper = mount(() => <Progress type="dashboard" />)
  39. expect(wrapper.classes()).toContain('el-progress--dashboard')
  40. })
  41. test('width', () => {
  42. const wrapper = mount(() => <Progress type="circle" width={120} />)
  43. expect(wrapper.find('.el-progress-circle').attributes('style')).toBe(
  44. 'height: 120px; width: 120px;'
  45. )
  46. })
  47. test('color', () => {
  48. const wrapper = mount(() => <Progress color="rgb(255, 255, 255)" />)
  49. expect(
  50. wrapper.find('.el-progress-bar__inner').attributes('style')
  51. ).toContain('background-color: rgb(255, 255, 255);')
  52. })
  53. test('color is function', async () => {
  54. const percentage = ref(0)
  55. const wrapper = mount(() => (
  56. <Progress
  57. percentage={percentage.value}
  58. color={(percentage: number) => {
  59. if (percentage > 50) {
  60. return 'rgb(4, 5, 6)'
  61. } else {
  62. return 'rgb(1, 2, 3)'
  63. }
  64. }}
  65. />
  66. ))
  67. expect(
  68. wrapper.find('.el-progress-bar__inner').attributes('style')
  69. ).toContain('background-color: rgb(1, 2, 3);')
  70. percentage.value = 60
  71. await nextTick()
  72. expect(
  73. wrapper.find('.el-progress-bar__inner').attributes('style')
  74. ).toContain('background-color: rgb(4, 5, 6);')
  75. })
  76. test('color is array', async () => {
  77. const percentage = ref(0)
  78. const wrapper = mount(() => (
  79. <Progress
  80. percentage={percentage.value}
  81. color={[
  82. { color: 'rgb(1, 1, 1)', percentage: 10 },
  83. { color: 'rgb(9, 9, 9)', percentage: 90 },
  84. ]}
  85. />
  86. ))
  87. percentage.value = 9
  88. await nextTick()
  89. expect(
  90. wrapper.find('.el-progress-bar__inner').attributes('style')
  91. ).toContain('background-color: rgb(1, 1, 1);')
  92. percentage.value = 89
  93. await nextTick()
  94. expect(
  95. wrapper.find('.el-progress-bar__inner').attributes('style')
  96. ).toContain('background-color: rgb(9, 9, 9);')
  97. })
  98. test('format', () => {
  99. const wrapper = mount(() => (
  100. <Progress
  101. percentage={100}
  102. format={(percent: number) => `占比${percent}%`}
  103. />
  104. ))
  105. expect(wrapper.find('.el-progress__text').text()).toBe('占比100%')
  106. })
  107. test('slot', () => {
  108. const wrapper = mount(() => (
  109. <Progress
  110. v-slots={{
  111. default: () => '自定义内容',
  112. }}
  113. />
  114. ))
  115. expect(wrapper.find('.el-progress__text').text()).toBe('自定义内容')
  116. })
  117. })