CommonTooltip.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { flushPromises } from '@vue/test-utils'
  3. import { h } from 'vue'
  4. import renderComponent, {
  5. type ExtendedRenderResult,
  6. } from '#tests/support/components/renderComponent.ts'
  7. import CommonTooltipVue, { type Props } from '../CommonTooltip.vue'
  8. const CustomComponent = (props: any) => {
  9. return props.state
  10. ? h('div', { 'data-test-id': 'customTooltip' }, 'tooltip')
  11. : null
  12. }
  13. CustomComponent.props = ['id', 'messages', 'heading', 'state']
  14. const renderTooltip = (type: 'popup' | 'inline', props: Props) => {
  15. return renderComponent(CommonTooltipVue, {
  16. props,
  17. slots: {
  18. default: '<span data-test-id="trigger">T</span>',
  19. },
  20. visuals: {
  21. objectAttributes: {} as any,
  22. tooltip: {
  23. type,
  24. component: CustomComponent,
  25. },
  26. },
  27. })
  28. }
  29. type Context = { view: ExtendedRenderResult }
  30. describe('rendering "inline" tooltip', async () => {
  31. const test = it<Context>
  32. beforeEach<Context>((context) => {
  33. context.view = renderTooltip('inline', { name: 'inline' })
  34. })
  35. test('renders tooltip when mousing over', async ({ view }) => {
  36. await view.events.hover(view.getByTestId('trigger'))
  37. expect(view.getByTestId('customTooltip')).toBeInTheDocument()
  38. await view.events.keyboard('{Escape}')
  39. expect(view.queryByTestId('customTooltip')).not.toBeInTheDocument()
  40. })
  41. test('renders tooltip when focusing', async ({ view }) => {
  42. view.getByTestId('tooltipTrigger').focus()
  43. await flushPromises()
  44. expect(await view.findByTestId('customTooltip')).toBeInTheDocument()
  45. await view.events.keyboard('{Escape}')
  46. expect(view.queryByTestId('customTooltip')).not.toBeInTheDocument()
  47. })
  48. })
  49. describe('rendering "popup" tooltip', () => {
  50. const test = it<Context>
  51. beforeEach<Context>((context) => {
  52. context.view = renderTooltip('popup', { name: 'inline' })
  53. })
  54. test("doesn't render popup when mousing over", async ({ view }) => {
  55. await view.events.hover(view.getByTestId('trigger'))
  56. expect(view.queryByTestId('customTooltip')).not.toBeInTheDocument()
  57. })
  58. test("doesn't render popup when focusing", async ({ view }) => {
  59. view.getByTestId('tooltipTrigger').focus()
  60. await flushPromises()
  61. expect(view.queryByTestId('customTooltip')).not.toBeInTheDocument()
  62. })
  63. test('renders when clicked on a button', async ({ view }) => {
  64. await view.events.click(view.getByTestId('trigger'))
  65. expect(view.getByTestId('customTooltip')).toBeInTheDocument()
  66. await view.events.keyboard('{Escape}')
  67. expect(view.queryByTestId('customTooltip')).not.toBeInTheDocument()
  68. })
  69. })