CommonOverlayContainer.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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' },
  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"></div>`,
  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. })