LayoutContent.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { beforeAll } from 'vitest'
  3. import { h } from 'vue'
  4. import { renderComponent } from '#tests/support/components/index.ts'
  5. import LayoutContent, {
  6. type Props,
  7. } from '#desktop/components/layout/LayoutContent.vue'
  8. const breadcrumbItems = [
  9. {
  10. label: 'Test Profile',
  11. route: '/test-profile',
  12. },
  13. {
  14. label: 'Test Foo',
  15. route: '/test-foo',
  16. },
  17. ]
  18. const renderLayoutContent = (
  19. slots: typeof LayoutContent.slots,
  20. props?: Props,
  21. ) => {
  22. return renderComponent(LayoutContent, {
  23. props: {
  24. ...props,
  25. breadcrumbItems: props?.breadcrumbItems || breadcrumbItems,
  26. },
  27. slots,
  28. dialog: true,
  29. router: true,
  30. })
  31. }
  32. describe('LayoutContent', () => {
  33. beforeAll(() => {
  34. const main = document.createElement('main')
  35. main.id = 'page-main-content'
  36. document.body.appendChild(main)
  37. })
  38. afterAll(() => {
  39. document.body.innerHTML = ''
  40. })
  41. it('renders component with default slot content', () => {
  42. const wrapper = renderLayoutContent({ default: () => 'Hello Test World!' })
  43. expect(wrapper.getByText('Hello Test World!')).toBeInTheDocument()
  44. expect(wrapper.getByText('Test Profile')).toBeInTheDocument()
  45. expect(wrapper.getByText('Test Foo')).toBeInTheDocument()
  46. })
  47. it('renders namespaced content', () => {
  48. const wrapper = renderLayoutContent({
  49. default: () => 'Hello Test World!',
  50. headerRight: () => 'Hello Content Right',
  51. helpPage: () => 'Hello Help Text',
  52. })
  53. expect(wrapper.getByText('Hello Test World!')).toBeInTheDocument()
  54. expect(wrapper.getByText('Hello Content Right')).toBeInTheDocument()
  55. expect(wrapper.queryByText('Hello Help Text')).not.toBeInTheDocument() // should not show help text in main content area
  56. })
  57. it('shows helpText and hide helpText button if hideDefault prop is true', () => {
  58. const wrapper = renderLayoutContent(
  59. {
  60. default: () => 'Hello Test World!',
  61. helpPage: () => 'Hello Help Text',
  62. },
  63. { showInlineHelp: true, breadcrumbItems },
  64. )
  65. expect(wrapper.queryByText('Hello Test World!')).not.toBeInTheDocument() // should not show default content
  66. expect(
  67. wrapper.queryByRole('button', { name: 'Help' }),
  68. ).not.toBeInTheDocument()
  69. expect(wrapper.getByText('Hello Help Text')).toBeInTheDocument()
  70. })
  71. it('shows help dialog with multiple paragraphs', async () => {
  72. const wrapper = renderLayoutContent(
  73. {
  74. default: () => 'Hello Test World!',
  75. },
  76. { breadcrumbItems, helpText: ['Hello Test World!', 'Hello Test Foo!'] },
  77. )
  78. await wrapper.events.click(wrapper.getByRole('button', { name: 'Help' }))
  79. expect(await wrapper.findByText('Hello Test World!')).toBeInTheDocument()
  80. expect(await wrapper.findByText('Hello Test Foo!')).toBeInTheDocument()
  81. })
  82. it('shows help dialog with single paragraph', async () => {
  83. const wrapper = renderLayoutContent(
  84. {
  85. default: () => 'Hello Default Slot Content!',
  86. },
  87. { breadcrumbItems, helpText: 'Hello Test Text World!' },
  88. )
  89. await wrapper.events.click(wrapper.getByRole('button', { name: 'Help' }))
  90. expect(
  91. await wrapper.findByText('Hello Test Text World!'),
  92. ).toBeInTheDocument()
  93. })
  94. it('shows custom help text component', async () => {
  95. const wrapper = renderLayoutContent(
  96. {
  97. default: () => 'Hello Test World!',
  98. helpPage: () => h('h1', 'Hello custom Help Text'),
  99. },
  100. { breadcrumbItems },
  101. )
  102. await wrapper.events.click(wrapper.getByRole('button', { name: 'Help' }))
  103. expect(
  104. await wrapper.findByText('Hello custom Help Text'),
  105. ).toBeInTheDocument()
  106. })
  107. it('allows custom widths', async () => {
  108. const wrapper = renderLayoutContent(
  109. {
  110. default: () => 'Hello Test World!',
  111. },
  112. { breadcrumbItems, width: 'narrow' },
  113. )
  114. expect(wrapper.getByTestId('layout-wrapper')).toHaveStyle({
  115. maxWidth: '600px',
  116. })
  117. })
  118. })