CommonSectionCollapse.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { within } from '@testing-library/vue'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import { useSessionStore } from '#shared/stores/session.ts'
  5. import CommonSectionCollapse, { type Props } from '../CommonSectionCollapse.vue'
  6. const html = String.raw
  7. const renderCommonSectionCollapse = (props: Partial<Props> = {}) => {
  8. return renderComponent(CommonSectionCollapse, {
  9. props: {
  10. id: 'test-id',
  11. title: 'foobar',
  12. ...props,
  13. },
  14. slots: {
  15. default: html` <template #default="{ headerId }">
  16. <nav :aria-labelledby="headerId" />
  17. </template>`,
  18. },
  19. store: true,
  20. })
  21. }
  22. describe('CommonSectionCollapse', () => {
  23. beforeEach(() => {
  24. localStorage.clear()
  25. })
  26. it('toggles content on heading click', async () => {
  27. const view = renderCommonSectionCollapse()
  28. expect(view.getByRole('navigation')).toBeInTheDocument()
  29. const header = view.getByRole('banner')
  30. const heading = within(header).getByRole('heading', { level: 3 })
  31. expect(heading).toHaveTextContent('foobar')
  32. await view.events.click(heading)
  33. expect(view.queryByRole('navigation')).not.toBeInTheDocument()
  34. })
  35. it('toggles content on button click', async () => {
  36. const view = renderCommonSectionCollapse()
  37. expect(view.getByRole('navigation')).toBeInTheDocument()
  38. const header = view.getByRole('banner')
  39. const button = within(header).getByRole('button')
  40. await view.events.click(button)
  41. expect(view.queryByRole('navigation')).not.toBeInTheDocument()
  42. })
  43. it('restores collapsed state initially', () => {
  44. const { userId } = useSessionStore()
  45. localStorage.setItem(`${userId}-test-id-section-collapsed`, 'true')
  46. const view = renderCommonSectionCollapse()
  47. expect(view.queryByRole('navigation')).not.toBeInTheDocument()
  48. })
  49. it('provides a11y text to the default slot', () => {
  50. const view = renderCommonSectionCollapse({
  51. title: 'a11y',
  52. })
  53. expect(view.getByRole('navigation', { name: 'a11y' })).toBeInTheDocument()
  54. })
  55. it('supports no collapse (title only) mode', async () => {
  56. const view = renderCommonSectionCollapse({
  57. noCollapse: true,
  58. })
  59. expect(view.getByRole('navigation')).toBeInTheDocument()
  60. const header = view.getByRole('banner')
  61. const heading = within(header).getByRole('heading', { level: 3 })
  62. expect(heading).toHaveTextContent('foobar')
  63. expect(within(header).queryByRole('button')).not.toBeInTheDocument()
  64. await view.events.click(heading)
  65. expect(view.getByRole('navigation')).toBeInTheDocument()
  66. })
  67. it('supports hiding the header', async () => {
  68. const view = renderCommonSectionCollapse({
  69. noHeader: true,
  70. })
  71. expect(view.getByRole('navigation')).toBeInTheDocument()
  72. expect(view.queryByRole('banner')).not.toBeInTheDocument()
  73. })
  74. })