LayoutContent.spec.ts 4.1 KB

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