CommonOverlayContainer.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { afterEach, expect } from 'vitest'
  3. import renderComponent from '#tests/support/components/renderComponent.ts'
  4. import CommonOverlayContainer from '#desktop/components/CommonOverlayContainer/CommonOverlayContainer.vue'
  5. const html = String.raw
  6. describe('CommonOverlayContainer', () => {
  7. let app: HTMLElement
  8. let wrapper: ReturnType<typeof renderComponent>
  9. beforeEach(() => {
  10. app = document.createElement('div')
  11. app.id = 'app'
  12. document.body.appendChild(app)
  13. wrapper = renderComponent(CommonOverlayContainer, {
  14. props: { tag: 'div', fullscreen: true },
  15. attachTo: app,
  16. })
  17. })
  18. afterEach(() => {
  19. document.body.innerHTML = ''
  20. })
  21. it('renders correctly with a11y specification', async () => {
  22. expect(wrapper.getByRole('dialog')).toBeInTheDocument()
  23. expect(app.querySelector('[role=presentation]')).toBeInTheDocument()
  24. await wrapper.rerender({ tag: 'aside' })
  25. expect(wrapper.getByRole('complementary')).toBeInTheDocument()
  26. })
  27. it('hides background when showBackdrop is false', async () => {
  28. await wrapper.rerender({ showBackdrop: false })
  29. expect(app.querySelector('[role=presentation]')).not.toBeInTheDocument()
  30. })
  31. it('should emit close event when backdrop is clicked, by default', async () => {
  32. const view = renderComponent({
  33. template: html`<div id="test-backdrop" />`,
  34. })
  35. const dialog = renderComponent(CommonOverlayContainer, {
  36. props: {
  37. tag: 'div',
  38. teleportTo: '#test-backdrop',
  39. },
  40. })
  41. await view.events.click(
  42. view
  43. .getAllByRole('presentation', {
  44. hidden: true,
  45. })
  46. .at(-1) as HTMLElement,
  47. )
  48. expect(dialog.emitted('click-background')).toHaveLength(1)
  49. })
  50. it('should not emit close event when backdrop is clicked, if specified', async () => {
  51. const view = renderComponent({
  52. template: html`<div id="test-backdrop"></div>`,
  53. })
  54. const dialog = renderComponent(CommonOverlayContainer, {
  55. props: {
  56. tag: 'div',
  57. teleportTo: '#test-backdrop',
  58. noCloseOnBackdropClick: true,
  59. },
  60. })
  61. await view.events.click(
  62. view
  63. .getAllByRole('presentation', {
  64. hidden: true,
  65. })
  66. .at(-1) as HTMLElement,
  67. )
  68. expect(dialog.emitted('click-background')).toBeUndefined()
  69. })
  70. it('displays the component over entire viewport', () => {
  71. const mainElement = document.createElement('main')
  72. mainElement.id = 'main-content'
  73. app.insertAdjacentElement('beforeend', mainElement)
  74. const dialog = renderComponent(CommonOverlayContainer, {
  75. props: {
  76. fullscreen: false,
  77. },
  78. })
  79. expect(mainElement.children).include(dialog.baseElement)
  80. })
  81. })