tooltip.spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { fireEvent, waitFor } from '@testing-library/vue'
  3. import { beforeEach, describe, vi } from 'vitest'
  4. import renderComponent from '#tests/support/components/renderComponent.ts'
  5. import { mockLocale } from '#tests/support/mock-locale.ts'
  6. describe('TooltipDirective', () => {
  7. describe('on non-touch device', () => {
  8. it('should show/hide tooltip on hover', async () => {
  9. const wrapper = renderComponent({
  10. template: `
  11. <div v-tooltip="'Hello, Tooltip'">Foo Test World</div>
  12. `,
  13. })
  14. await wrapper.events.hover(wrapper.getByText('Foo Test World'))
  15. await waitFor(() =>
  16. expect(wrapper.queryByText('Hello, Tooltip')).toBeInTheDocument(),
  17. )
  18. await wrapper.events.unhover(wrapper.getByText('Foo Test World'))
  19. await waitFor(() => {
  20. expect(wrapper.queryByText('Hello, Tooltip')).not.toBeInTheDocument()
  21. })
  22. })
  23. it('has accessibility attribute', async () => {
  24. const wrapper = renderComponent({
  25. template: `
  26. <div v-tooltip="'Hello, Tooltip'">Foo Test World</div>
  27. `,
  28. })
  29. await waitFor(() =>
  30. expect(wrapper.queryByLabelText('Hello, Tooltip')).toBeInTheDocument(),
  31. )
  32. })
  33. it('should hide tooltip on scroll', async () => {
  34. const wrapper = renderComponent({
  35. template: `
  36. <div v-tooltip="'Hello, Tooltip'">Foo Test World</div>
  37. `,
  38. })
  39. await wrapper.events.hover(wrapper.getByText('Foo Test World'))
  40. await waitFor(() =>
  41. expect(wrapper.queryByText('Hello, Tooltip')).toBeInTheDocument(),
  42. )
  43. window.dispatchEvent(new Event('scroll'))
  44. await waitFor(() =>
  45. expect(wrapper.queryByText('Hello, Tooltip')).not.toBeInTheDocument(),
  46. )
  47. })
  48. })
  49. describe('on touch device', () => {
  50. beforeEach(() => {
  51. vi.mock('#shared/composables/useTouchDevice.ts', () => ({
  52. useTouchDevice: vi
  53. .fn()
  54. .mockReturnValue({ isTouchDevice: { value: true } }),
  55. }))
  56. })
  57. it('should hide tooltip on first touch', async () => {
  58. const wrapper = renderComponent({
  59. template: `
  60. <div v-tooltip="'Hello, Tooltip'">Foo Test World</div>
  61. `,
  62. })
  63. await fireEvent.touchStart(wrapper.getByText('Foo Test World'))
  64. await waitFor(() => {
  65. expect(wrapper.queryByText('Hello, Tooltip')).toBeInTheDocument()
  66. })
  67. await fireEvent.touchStart(wrapper.getByText('Foo Test World'))
  68. await fireEvent.touchEnd(wrapper.getByText('Foo Test World'))
  69. await waitFor(() =>
  70. expect(wrapper.queryByText('Hello, Tooltip')).not.toBeInTheDocument(),
  71. )
  72. })
  73. it('updated tooltip locale', async () => {
  74. const translationSpy = mockLocale('Hello, Tooltip', 'Hola, Tooltip')
  75. const wrapper = renderComponent({
  76. template: `
  77. <div v-tooltip="$t('Hello, Tooltip')">Foo Test World</div>
  78. `,
  79. })
  80. await wrapper.events.hover(wrapper.getByText('Foo Test World'))
  81. await waitFor(() => {
  82. expect(wrapper.queryByText('Hola, Tooltip')).toBeInTheDocument()
  83. expect(wrapper.getByLabelText('Hola, Tooltip')).toBeInTheDocument()
  84. })
  85. expect(translationSpy).toHaveBeenCalledOnce()
  86. })
  87. })
  88. describe('modifiers', () => {
  89. it('supports onlyTruncation', async () => {
  90. let wrapper = renderComponent({
  91. template: `
  92. <div :style="{width: '400px'}">
  93. <div v-tooltip.onlyTruncation="'Short Text'">Foo Test World</div>
  94. </div>
  95. `,
  96. })
  97. await wrapper.events.hover(wrapper.getByText('Foo Test World'))
  98. await waitFor(() => {
  99. expect(wrapper.queryByText('Short Text')).not.toBeInTheDocument()
  100. })
  101. wrapper = renderComponent({
  102. template: `
  103. <div :style="{width: '50px'}">
  104. <div v-tooltip.onlyTruncation="'This is a very long text that will be truncated'">Long Text</div>
  105. </div>
  106. `,
  107. })
  108. await wrapper.events.hover(wrapper.getByText('Long Text'))
  109. await waitFor(() => {
  110. expect(
  111. wrapper.queryByText(
  112. 'This is a very long text that will be truncated',
  113. ),
  114. ).toBeInTheDocument()
  115. })
  116. })
  117. })
  118. })