LayoutContent.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 app = document.createElement('div')
  35. app.id = 'app'
  36. document.body.appendChild(app)
  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(
  45. wrapper.getByRole('link', { name: 'Test Profile' }),
  46. ).toBeInTheDocument()
  47. expect(wrapper.getByRole('link', { name: 'Test Foo' })).toBeInTheDocument()
  48. })
  49. it('renders namespaced content', () => {
  50. const wrapper = renderLayoutContent({
  51. default: () => 'Hello Test World!',
  52. headerRight: () => 'Hello Content Right',
  53. helpPage: () => 'Hello Help Text',
  54. })
  55. expect(wrapper.getByText('Hello Test World!')).toBeInTheDocument()
  56. expect(wrapper.getByText('Hello Content Right')).toBeInTheDocument()
  57. expect(wrapper.queryByText('Hello Help Text')).not.toBeInTheDocument() // should not show help text in main content area
  58. })
  59. it('shows helpText and hide helpText button if hideDefault prop is true', () => {
  60. const wrapper = renderLayoutContent(
  61. {
  62. default: () => 'Hello Test World!',
  63. helpPage: () => 'Hello Help Text',
  64. },
  65. { showInlineHelp: true, breadcrumbItems },
  66. )
  67. expect(wrapper.queryByText('Hello Test World!')).not.toBeInTheDocument() // should not show default content
  68. expect(
  69. wrapper.queryByRole('button', { name: 'Help' }),
  70. ).not.toBeInTheDocument()
  71. expect(wrapper.getByText('Hello Help Text')).toBeInTheDocument()
  72. })
  73. it('shows help dialog with multiple paragraphs', async () => {
  74. const wrapper = renderLayoutContent(
  75. {
  76. default: () => 'Hello Test World!',
  77. },
  78. { breadcrumbItems, helpText: ['Hello Test World!', 'Hello Test Foo!'] },
  79. )
  80. await wrapper.events.click(wrapper.getByRole('button', { name: 'Help' }))
  81. expect(await wrapper.findByText('Hello Test World!')).toBeInTheDocument()
  82. expect(await wrapper.findByText('Hello Test Foo!')).toBeInTheDocument()
  83. })
  84. it('shows help dialog with single paragraph', async () => {
  85. const wrapper = renderLayoutContent(
  86. {
  87. default: () => 'Hello Default Slot Content!',
  88. },
  89. { breadcrumbItems, helpText: 'Hello Test Text World!' },
  90. )
  91. await wrapper.events.click(wrapper.getByRole('button', { name: 'Help' }))
  92. expect(
  93. await wrapper.findByText('Hello Test Text World!'),
  94. ).toBeInTheDocument()
  95. })
  96. it('shows custom help text component', async () => {
  97. const wrapper = renderLayoutContent(
  98. {
  99. default: () => 'Hello Test World!',
  100. helpPage: () => h('h1', 'Hello custom Help Text'),
  101. },
  102. { breadcrumbItems },
  103. )
  104. await wrapper.events.click(wrapper.getByRole('button', { name: 'Help' }))
  105. expect(
  106. await wrapper.findByText('Hello custom Help Text'),
  107. ).toBeInTheDocument()
  108. })
  109. it('allows custom widths', async () => {
  110. const wrapper = renderLayoutContent(
  111. {
  112. default: () => 'Hello Test World!',
  113. },
  114. { breadcrumbItems, width: 'narrow' },
  115. )
  116. expect(wrapper.getByTestId('layout-wrapper')).toHaveStyle({
  117. maxWidth: '600px',
  118. })
  119. })
  120. })