space.test.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { describe, expect, it, vi } from 'vitest'
  4. import Space from '../src/space'
  5. const AXIOM = 'Rem is the best girl'
  6. describe('Space.vue', () => {
  7. it('render test', async () => {
  8. const wrapper = mount(<Space>{AXIOM}</Space>)
  9. expect(wrapper.text()).toEqual(AXIOM)
  10. await wrapper.setProps({
  11. direction: 'vertical',
  12. wrap: true,
  13. })
  14. expect(wrapper.find('.el-space--vertical').exists()).toBe(true)
  15. expect(wrapper.find('.el-space').attributes('style')).toContain(
  16. 'flex-wrap: wrap'
  17. )
  18. })
  19. it('sizes', async () => {
  20. const warnHandler = vi.fn()
  21. const wrapper = mount(
  22. <Space size="large">
  23. {Array.from({ length: 2 }).map((_, idx) => {
  24. return `test${idx}`
  25. })}
  26. </Space>,
  27. {
  28. global: {
  29. config: {
  30. warnHandler,
  31. },
  32. },
  33. }
  34. )
  35. await nextTick()
  36. expect(wrapper.find('.el-space__item').attributes('style')).toContain(
  37. 'margin-right: 16px'
  38. )
  39. await wrapper.setProps({
  40. size: 30,
  41. })
  42. await nextTick()
  43. expect(wrapper.find('.el-space__item').attributes('style')).toContain(
  44. 'margin-right: 30px'
  45. )
  46. await wrapper.setProps({
  47. size: [10, 20],
  48. })
  49. expect(wrapper.find('.el-space__item').attributes('style')).toContain(
  50. 'margin-right: 10px'
  51. )
  52. expect(wrapper.find('.el-space__item').attributes('style')).toContain(
  53. 'padding-bottom: 20px'
  54. )
  55. await wrapper.setProps({
  56. size: 'unknown',
  57. })
  58. expect(warnHandler).toHaveBeenCalled()
  59. expect(wrapper.find('.el-space__item').attributes('style')).toContain(
  60. 'margin-right: 8px'
  61. )
  62. })
  63. it('should render with spacer', async () => {
  64. const stringSpacer = '|'
  65. const wrapper = mount(
  66. <Space size="large" spacer={stringSpacer}>
  67. {Array.from({ length: 2 }).map((_, idx) => {
  68. return `test${idx}`
  69. })}
  70. </Space>
  71. )
  72. await nextTick()
  73. expect(wrapper.element.children).toHaveLength(3)
  74. expect(wrapper.text()).toContain(stringSpacer)
  75. // 2 elements expecting only 1 spacer
  76. expect(wrapper.text().split(stringSpacer)).toHaveLength(2)
  77. const testSpacerCls = 'test-spacer-cls'
  78. // vnode type spacer
  79. await wrapper.setProps({
  80. spacer: <div class={testSpacerCls} />,
  81. })
  82. expect(wrapper.findAll(`.${testSpacerCls}`)).toHaveLength(1)
  83. expect(wrapper.element.children).toHaveLength(3)
  84. })
  85. it('fill', async () => {
  86. const wrapper = mount(
  87. <Space fill>
  88. {Array.from({ length: 2 }).map((_, idx) => {
  89. return `test${idx}`
  90. })}
  91. </Space>
  92. )
  93. await nextTick()
  94. expect(wrapper.find('.el-space').attributes('style')).toContain(
  95. 'flex-wrap: wrap'
  96. )
  97. expect(wrapper.find('.el-space__item').attributes('style')).toContain(
  98. 'flex-grow: 1'
  99. )
  100. expect(wrapper.find('.el-space__item').attributes('style')).toContain(
  101. 'min-width: 100%'
  102. )
  103. // custom fill ratio
  104. await wrapper.setProps({
  105. fillRatio: 50,
  106. })
  107. await nextTick()
  108. expect(wrapper.find('.el-space__item').attributes('style')).toContain(
  109. 'min-width: 50%'
  110. )
  111. })
  112. })