tooltip.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.todo('detects truncation if modifier is set', async () => {
  90. // :TODO - Move this to a real browser env -> Cypress
  91. let wrapper = renderComponent({
  92. template: `
  93. <div :style="{width: '400px'}">
  94. <div v-tooltip.truncate="'Foo Test world'">Short Text</div>
  95. </div>
  96. `,
  97. })
  98. await wrapper.events.hover(wrapper.getByText('Short Text'))
  99. await waitFor(() => {
  100. expect(wrapper.queryByText('Foo Test world')).not.toBeInTheDocument()
  101. })
  102. wrapper = renderComponent({
  103. template: `
  104. <div :style="{width: '50px'}">
  105. <div v-tooltip.truncate="'Foo Test world'">This is a very long text that will be truncated</div>
  106. </div>
  107. `,
  108. })
  109. await wrapper.events.hover(
  110. wrapper.getByText('This is a very long text that will be truncated'),
  111. )
  112. })
  113. })
  114. })