descriptions.test.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { nextTick, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { describe, expect, test } from 'vitest'
  4. import ElTag from '@element-plus/components/tag'
  5. import ElDescriptions from '../src/description.vue'
  6. import ElDescriptionsItem from '../src/description-item'
  7. describe('Descriptions.vue', () => {
  8. test('render test', () => {
  9. const wrapper = mount(() => (
  10. <ElDescriptions title="title" extra="extra">
  11. {Array.from({ length: 4 }).map((_, index) => (
  12. <ElDescriptionsItem label={String(index)} />
  13. ))}
  14. </ElDescriptions>
  15. ))
  16. expect(wrapper.find('.el-descriptions__title').text()).toEqual('title')
  17. expect(wrapper.find('.el-descriptions__extra').text()).toEqual('extra')
  18. expect(wrapper.findAll('.el-descriptions__label').length).toEqual(4)
  19. expect(wrapper.findAll('.el-descriptions__content').length).toEqual(4)
  20. })
  21. test('render empty label', () => {
  22. const wrapper = mount(() => (
  23. <ElDescriptions>
  24. {Array.from({ length: 3 }).map(() => (
  25. <ElDescriptionsItem />
  26. ))}
  27. </ElDescriptions>
  28. ))
  29. expect(wrapper.findAll('.el-descriptions__label').length).toEqual(0)
  30. expect(wrapper.findAll('.el-descriptions__content').length).toEqual(3)
  31. })
  32. test('should render border props', () => {
  33. const wrapper = mount(() => (
  34. <ElDescriptions border>
  35. <ElDescriptionsItem />
  36. </ElDescriptions>
  37. ))
  38. expect(wrapper.find('table').classes()).toContain('is-bordered')
  39. })
  40. test('should render align props', () => {
  41. const wrapper = mount(() => (
  42. <ElDescriptions border>
  43. {Array.from({ length: 3 }).map(() => (
  44. <ElDescriptionsItem align="right" labelAlign="center" />
  45. ))}
  46. </ElDescriptions>
  47. ))
  48. expect(wrapper.find('.el-descriptions__label').classes()).toContain(
  49. 'is-center'
  50. )
  51. expect(wrapper.find('.el-descriptions__content').classes()).toContain(
  52. 'is-right'
  53. )
  54. })
  55. test('should render width props', () => {
  56. const wrapper = mount(() => (
  57. <ElDescriptions border>
  58. {Array.from({ length: 3 }).map(() => (
  59. <ElDescriptionsItem width="50px" min-width="60px" />
  60. ))}
  61. </ElDescriptions>
  62. ))
  63. expect(
  64. wrapper.find('.el-descriptions__label').attributes('style')
  65. ).toContain('width: 50px; min-width: 60px;')
  66. expect(
  67. wrapper.find('.el-descriptions__content').attributes('style')
  68. ).toContain('width: 50px; min-width: 60px;')
  69. })
  70. test('should render class props', () => {
  71. const wrapper = mount(() => (
  72. <ElDescriptions border>
  73. {Array.from({ length: 3 }).map(() => (
  74. <ElDescriptionsItem
  75. class-name="class-name"
  76. label-class-name="label-class-name"
  77. />
  78. ))}
  79. </ElDescriptions>
  80. ))
  81. expect(wrapper.find('.el-descriptions__label').classes()).toContain(
  82. 'label-class-name'
  83. )
  84. expect(wrapper.find('.el-descriptions__content').classes()).toContain(
  85. 'class-name'
  86. )
  87. })
  88. test('should render column props', async () => {
  89. const border = ref(false)
  90. const wrapper = mount(() => (
  91. <ElDescriptions column={5} border={border.value}>
  92. {Array.from({ length: 10 }).map(() => (
  93. <ElDescriptionsItem />
  94. ))}
  95. </ElDescriptions>
  96. ))
  97. expect(wrapper.find('tr').element.children.length).toEqual(5)
  98. border.value = true
  99. await nextTick()
  100. expect(wrapper.find('tr').element.children.length).toEqual(10)
  101. })
  102. test('should render direction props', async () => {
  103. const direction = ref<'horizontal' | 'vertical'>('horizontal')
  104. const wrapper = mount(() => (
  105. <ElDescriptions column={5} direction={direction.value} border>
  106. {Array.from({ length: 10 }).map((item) => (
  107. <ElDescriptionsItem label={String(item)}>
  108. {String(item)}
  109. </ElDescriptionsItem>
  110. ))}
  111. </ElDescriptions>
  112. ))
  113. expect(wrapper.find('tr').element.children.length).toEqual(10)
  114. expect(wrapper.findAll('tr')[0].element.children[0].innerHTML).toEqual(
  115. wrapper.findAll('tr')[0].element.children[1].innerHTML
  116. )
  117. direction.value = 'vertical'
  118. await nextTick()
  119. expect(wrapper.find('tr').element.children.length).toEqual(5)
  120. expect(wrapper.findAll('tr')[0].element.children[0].innerHTML).toEqual(
  121. wrapper.findAll('tr')[1].element.children[0].innerHTML
  122. )
  123. })
  124. test('should render title slots', async () => {
  125. const wrapper = mount(() => (
  126. <ElDescriptions
  127. v-slots={{
  128. title: () => 'title',
  129. default: () =>
  130. Array.from({ length: 10 }).map(() => <ElDescriptionsItem />),
  131. }}
  132. ></ElDescriptions>
  133. ))
  134. expect(wrapper.find('.el-descriptions__title').text()).toEqual('title')
  135. })
  136. test('should render span props', async () => {
  137. const wrapper = mount(() => (
  138. <ElDescriptions>
  139. <ElDescriptionsItem label="1">1</ElDescriptionsItem>
  140. <ElDescriptionsItem label="2" span={2}>
  141. 2
  142. </ElDescriptionsItem>
  143. <ElDescriptionsItem label="3">3</ElDescriptionsItem>
  144. <ElDescriptionsItem label="4">4</ElDescriptionsItem>
  145. </ElDescriptions>
  146. ))
  147. expect(wrapper.findAll('td')[1].element.getAttribute('colSpan')).toEqual(
  148. '2'
  149. )
  150. expect(wrapper.findAll('td')[3].element.getAttribute('colSpan')).toEqual(
  151. '2'
  152. )
  153. })
  154. test('re-rendered when slots is updated', async () => {
  155. const CHANGE_VALUE = 'company'
  156. const remarks = ref(['school', 'hospital'])
  157. const onClick = () => {
  158. remarks.value[0] = CHANGE_VALUE
  159. }
  160. const wrapper = mount(() => (
  161. <>
  162. {remarks.value.map((remark, index) => (
  163. <ElDescriptions key={index} title={remark}>
  164. <ElDescriptionsItem label={remark}>
  165. <ElTag size="small">{remark}</ElTag>
  166. </ElDescriptionsItem>
  167. </ElDescriptions>
  168. ))}
  169. <button onClick={onClick}>click</button>
  170. </>
  171. ))
  172. wrapper.find('button').trigger('click')
  173. await nextTick()
  174. expect(wrapper.findComponent(ElTag).text()).toBe(CHANGE_VALUE)
  175. })
  176. })