timeline.test.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { markRaw } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { describe, expect, test } from 'vitest'
  4. import { MoreFilled } from '@element-plus/icons-vue'
  5. import TimeLine from '../src/timeline'
  6. import TimeLineItem from '../src/timeline-item.vue'
  7. import type { TimelineItemProps } from '../src/timeline-item'
  8. const iconMoreFilled = markRaw(MoreFilled)
  9. const activities: {
  10. content: string
  11. timestamp: string
  12. placement?: TimelineItemProps['placement']
  13. hideTimestamp?: boolean
  14. }[] = [
  15. {
  16. content: 'Step 1: xxxxxx',
  17. timestamp: '2018-04-11',
  18. },
  19. {
  20. content: 'Step 2: xxxxxx',
  21. timestamp: '2018-04-13',
  22. },
  23. {
  24. content: 'Step 3: xxxxxx',
  25. timestamp: '2018-04-15',
  26. },
  27. ]
  28. describe('TimeLine.vue', () => {
  29. test('create', () => {
  30. const wrapper = mount(() => (
  31. <TimeLine>
  32. {activities.map((activity, index) => (
  33. <TimeLineItem key={index}>{activity.content}</TimeLineItem>
  34. ))}
  35. </TimeLine>
  36. ))
  37. const contentWrappers = wrapper.findAll('.el-timeline-item__content')
  38. contentWrappers.forEach((content, index) => {
  39. expect(content.text()).toEqual(activities[index].content)
  40. })
  41. })
  42. test('placement', () => {
  43. activities[0].placement = 'top'
  44. const wrapper = mount(() => (
  45. <TimeLine>
  46. {activities.map((activity, index) => (
  47. <TimeLineItem key={index} placement={activity.placement} />
  48. ))}
  49. </TimeLine>
  50. ))
  51. const timestampWrapper = wrapper.findAll('.el-timeline-item__timestamp')[0]
  52. expect(timestampWrapper.classes('is-top')).toBe(true)
  53. })
  54. test('hide-timestamp', () => {
  55. activities[0].hideTimestamp = true
  56. const wrapper = mount(() => (
  57. <TimeLine>
  58. {activities.map((activity, index) => (
  59. <TimeLineItem key={index} hide-timestamp={activity.hideTimestamp} />
  60. ))}
  61. </TimeLine>
  62. ))
  63. const timestampWrappers = wrapper.findAll('.el-timeline-item__timestamp')
  64. expect(timestampWrappers.length).toEqual(2)
  65. })
  66. test('color', () => {
  67. const wrapper = mount(() => (
  68. <TimeLine>
  69. <TimeLineItem color="#f00" />
  70. </TimeLine>
  71. ))
  72. const vm = wrapper.vm
  73. const nodeElm = vm.$el.querySelector('.el-timeline-item__node')
  74. expect(nodeElm.style.backgroundColor).toEqual('rgb(255, 0, 0)')
  75. })
  76. test('type', () => {
  77. const wrapper = mount(() => (
  78. <TimeLine>
  79. <TimeLineItem type="primary" />
  80. </TimeLine>
  81. ))
  82. const nodeWrapper = wrapper.find('.el-timeline-item__node')
  83. expect(nodeWrapper.classes('el-timeline-item__node--primary')).toBe(true)
  84. })
  85. test('size', () => {
  86. const wrapper = mount(() => (
  87. <TimeLine>
  88. <TimeLineItem size="large" />
  89. </TimeLine>
  90. ))
  91. const nodeWrapper = wrapper.find('.el-timeline-item__node')
  92. expect(nodeWrapper.classes('el-timeline-item__node--large')).toBe(true)
  93. })
  94. test('icon', () => {
  95. const wrapper = mount(() => (
  96. <TimeLine>
  97. <TimeLineItem icon={iconMoreFilled} />
  98. </TimeLine>
  99. ))
  100. expect(wrapper.find('.el-timeline-item__icon').exists()).toBe(true)
  101. expect(wrapper.findComponent(MoreFilled).exists()).toBe(true)
  102. })
  103. test('hollow', () => {
  104. const wrapper = mount(() => (
  105. <TimeLine>
  106. <TimeLineItem hollow />
  107. </TimeLine>
  108. ))
  109. const nodeWrapper = wrapper.find('.el-timeline-item__node')
  110. expect(nodeWrapper.classes('is-hollow')).toBe(true)
  111. })
  112. test('dot', () => {
  113. const wrapper = mount(() => (
  114. <TimeLine>
  115. <TimeLineItem
  116. v-slots={{
  117. dot: () => 'dot',
  118. }}
  119. />
  120. </TimeLine>
  121. ))
  122. const dotWrapper = wrapper.find('.el-timeline-item__dot')
  123. expect(dotWrapper.text()).toEqual('dot')
  124. expect(wrapper.find('.el-timeline-item__node').exists()).toBe(false)
  125. })
  126. test('center', () => {
  127. const wrapper = mount(() => (
  128. <TimeLine>
  129. {activities.map((_, index) => (
  130. <TimeLineItem key={index} center={index === 1} />
  131. ))}
  132. </TimeLine>
  133. ))
  134. const timestampWrappers = wrapper.findAll('.el-timeline-item')
  135. expect(timestampWrappers[1].classes()).toContain('el-timeline-item__center')
  136. })
  137. })