trigger.test.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, it } from 'vitest'
  4. import { EVENT_CODE } from '@element-plus/constants'
  5. import { TOOLTIP_INJECTION_KEY } from '@element-plus/components/tooltip'
  6. import ElTooltipTrigger from '../src/trigger.vue'
  7. import { genTooltipProvides } from '../test-helper/provides'
  8. import type { VueWrapper } from '@vue/test-utils'
  9. const AXIOM = 'rem is the best girl'
  10. describe('<ElTooltipTrigger />', () => {
  11. const {
  12. controlled,
  13. id,
  14. open,
  15. onOpen,
  16. onClose,
  17. onToggle,
  18. onShow,
  19. onHide,
  20. onBeforeShow,
  21. onBeforeHide,
  22. } = genTooltipProvides()
  23. const defaultProvide = {
  24. [TOOLTIP_INJECTION_KEY as symbol]: {
  25. controlled,
  26. id,
  27. open,
  28. onOpen,
  29. onClose,
  30. onToggle,
  31. onShow,
  32. onHide,
  33. onBeforeShow,
  34. onBeforeHide,
  35. },
  36. }
  37. const createComponent = (props = {}) =>
  38. mount(
  39. {
  40. setup() {
  41. return () => <ElTooltipTrigger {...props}>{AXIOM}</ElTooltipTrigger>
  42. },
  43. },
  44. {
  45. global: {
  46. provide: defaultProvide,
  47. stubs: ['ElPopperTrigger'],
  48. },
  49. attachTo: document.body,
  50. }
  51. )
  52. let wrapper: VueWrapper<any>
  53. afterEach(() => {
  54. open.value = false
  55. controlled.value = false
  56. wrapper?.unmount()
  57. document.body.innerHTML = ''
  58. })
  59. describe('rendering', () => {
  60. it('should render correctly', async () => {
  61. wrapper = createComponent()
  62. })
  63. })
  64. describe('trigger event', () => {
  65. describe('controlled', () => {
  66. it('should not trigger anything when tooltip is controlled', async () => {
  67. wrapper = createComponent()
  68. controlled.value = true
  69. await nextTick()
  70. const { vm } = wrapper.findComponent(
  71. ElTooltipTrigger
  72. ) as VueWrapper<any>
  73. const blurEvt = new FocusEvent('blur')
  74. vm.onBlur(blurEvt)
  75. await nextTick()
  76. expect(onClose).not.toHaveBeenCalled()
  77. const focusEvt = new FocusEvent('focus')
  78. vm.onFocus(focusEvt)
  79. await nextTick()
  80. expect(onOpen).not.toHaveBeenCalled()
  81. const mousedownEvt = new MouseEvent('mousedown')
  82. vm.onClick(mousedownEvt)
  83. await nextTick()
  84. expect(onToggle).not.toHaveBeenCalled()
  85. const mouseenterEvt = new MouseEvent('mouseenter')
  86. vm.onMouseenter(mouseenterEvt)
  87. await nextTick()
  88. expect(onOpen).not.toHaveBeenCalled()
  89. const mouseleaveEvt = new MouseEvent('mouseleave')
  90. vm.onMouseleave(mouseleaveEvt)
  91. await nextTick()
  92. expect(onClose).not.toHaveBeenCalled()
  93. const contextmenuEvt = new MouseEvent('contextmenu')
  94. vm.onContextMenu(contextmenuEvt)
  95. await nextTick()
  96. expect(onToggle).not.toHaveBeenCalled()
  97. // keyboard evt
  98. const keyboardEvt = new KeyboardEvent('keydown')
  99. vm.onKeydown(keyboardEvt)
  100. await nextTick()
  101. expect(onToggle).not.toHaveBeenCalled()
  102. })
  103. })
  104. describe('uncontrolled', () => {
  105. it('should be able to dispatch event when uncontrolled', async () => {
  106. wrapper = createComponent()
  107. await nextTick()
  108. const { vm } = wrapper.findComponent(
  109. ElTooltipTrigger
  110. ) as VueWrapper<any>
  111. await wrapper.setProps({
  112. trigger: 'focus',
  113. })
  114. const blurEvt = new FocusEvent('blur')
  115. vm.onBlur(blurEvt)
  116. await nextTick()
  117. expect(onClose).toHaveBeenCalledTimes(1)
  118. const focusEvt = new FocusEvent('focus')
  119. vm.onFocus(focusEvt)
  120. await nextTick()
  121. expect(onOpen).toHaveBeenCalledTimes(1)
  122. await wrapper.setProps({
  123. trigger: 'click',
  124. })
  125. const mousedownEvt = new MouseEvent('mousedown')
  126. vm.onClick(mousedownEvt)
  127. await nextTick()
  128. await wrapper.setProps({
  129. trigger: 'hover',
  130. })
  131. expect(onToggle).toHaveBeenCalledTimes(1)
  132. const mouseenterEvt = new MouseEvent('mouseenter')
  133. vm.onMouseenter(mouseenterEvt)
  134. await nextTick()
  135. expect(onOpen).toHaveBeenCalledTimes(2)
  136. const mouseleaveEvt = new MouseEvent('mouseleave')
  137. vm.onMouseleave(mouseleaveEvt)
  138. await nextTick()
  139. expect(onClose).toHaveBeenCalledTimes(2)
  140. await wrapper.setProps({
  141. trigger: 'contextmenu',
  142. })
  143. const contextmenuEvt = new MouseEvent('contextmenu')
  144. vm.onContextMenu(contextmenuEvt)
  145. await nextTick()
  146. expect(onToggle).toHaveBeenCalledTimes(2)
  147. // keyboard evt
  148. let keyboardEvt = new KeyboardEvent('keydown', {
  149. code: EVENT_CODE.esc,
  150. })
  151. vm.onKeydown(keyboardEvt)
  152. await nextTick()
  153. expect(onToggle).toHaveBeenCalledTimes(2)
  154. keyboardEvt = new KeyboardEvent('keydown', {
  155. code: EVENT_CODE.enter,
  156. })
  157. vm.onKeydown(keyboardEvt)
  158. await nextTick()
  159. expect(onToggle).toHaveBeenCalledTimes(3)
  160. keyboardEvt = new KeyboardEvent('keydown', {
  161. code: EVENT_CODE.space,
  162. })
  163. vm.onKeydown(keyboardEvt)
  164. await nextTick()
  165. expect(onToggle).toHaveBeenCalledTimes(4)
  166. })
  167. })
  168. })
  169. })