LayoutHeader.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { expect } from 'vitest'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import { i18n } from '#shared/i18n.ts'
  5. import LayoutHeader from '../LayoutHeader.vue'
  6. describe('mobile app header', () => {
  7. it("doesn't render, if not specified", () => {
  8. const view = renderComponent(LayoutHeader, { router: true })
  9. expect(view.queryByTestId('appHeader')).not.toBeInTheDocument()
  10. })
  11. describe('title prop tests', () => {
  12. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  13. let view: ReturnType<typeof renderComponent>
  14. beforeEach(() => {
  15. view = renderComponent(LayoutHeader, {
  16. props: { title: 'Test' },
  17. router: true,
  18. })
  19. })
  20. it('renders translated title, if specified', async () => {
  21. expect(view.getByTestId('appHeader')).toBeInTheDocument()
  22. expect(view.getByText('Test')).toBeInTheDocument()
  23. i18n.setTranslationMap(new Map([['Test2', 'Translated']]))
  24. await view.rerender({ title: 'Test2' })
  25. expect(view.getByText('Translated')).toBeInTheDocument()
  26. })
  27. it('should be by default a h1', () => {
  28. expect(view.getByRole('heading', { level: 1 })).toBeInTheDocument()
  29. })
  30. it('can add custom class to title', async () => {
  31. await view.rerender({
  32. title: 'Test',
  33. titleClass: 'test-class',
  34. })
  35. expect(view.getByText('Test')).toHaveClass('test-class')
  36. })
  37. })
  38. it('renders back button, if specified', async () => {
  39. const view = renderComponent(LayoutHeader, {
  40. props: {
  41. backUrl: '/foo',
  42. backTitle: 'Back',
  43. },
  44. router: true,
  45. })
  46. const backButton = view.getByText('Back')
  47. expect(backButton).toBeInTheDocument()
  48. i18n.setTranslationMap(new Map([['Test2', 'Translated']]))
  49. await view.rerender({ backTitle: 'Test2', backUrl: '/bar' })
  50. expect(view.getByText('Translated')).toBeInTheDocument()
  51. })
  52. it('renders action, if specified', async () => {
  53. const onAction = vi.fn()
  54. const view = renderComponent(LayoutHeader, {
  55. props: {
  56. onAction,
  57. actionTitle: 'Action',
  58. },
  59. router: true,
  60. })
  61. const actionButton = view.getByText('Action')
  62. expect(actionButton).toBeInTheDocument()
  63. await view.events.click(actionButton)
  64. expect(onAction).toHaveBeenCalled()
  65. i18n.setTranslationMap(new Map([['Test2', 'Translated']]))
  66. await view.rerender({ actionTitle: 'Test2', onAction })
  67. expect(view.getByText('Translated')).toBeInTheDocument()
  68. })
  69. it('hides action, if specified', async () => {
  70. const onAction = vi.fn()
  71. const view = renderComponent(LayoutHeader, {
  72. props: {
  73. onAction,
  74. actionTitle: 'Action',
  75. actionHidden: true,
  76. },
  77. router: true,
  78. })
  79. expect(view.queryByText('Action')).not.toBeInTheDocument()
  80. })
  81. describe('slots test', () => {
  82. it('display all slots', () => {
  83. const view = renderComponent(LayoutHeader, {
  84. slots: {
  85. before: `<span>Step 1</span>`,
  86. default: `<h2>Test Heading</h2>`,
  87. after: `Action`,
  88. },
  89. router: true,
  90. })
  91. expect(view.getByText('Step 1')).toBeInTheDocument()
  92. expect(view.getByRole('heading', { level: 2 })).toBeInTheDocument()
  93. expect(view.getByText('Action')).toBeInTheDocument()
  94. })
  95. it('shows fallback if partial slots are used', () => {
  96. const view = renderComponent(LayoutHeader, {
  97. title: 'Test',
  98. slots: {
  99. before: `<span>Step 1</span>`,
  100. after: `Action`,
  101. },
  102. router: true,
  103. })
  104. expect(view.getByText('Step 1')).toBeInTheDocument()
  105. expect(view.getByRole('heading', { level: 1 })).toBeInTheDocument()
  106. expect(view.getByText('Action')).toBeInTheDocument()
  107. })
  108. })
  109. })