CommonBackButton.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { flushPromises } from '@vue/test-utils'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import { EnumTextDirection } from '#shared/graphql/types.ts'
  5. import { i18n } from '#shared/i18n.ts'
  6. import { useLocaleStore } from '#shared/stores/locale.ts'
  7. import CommonBackButton from '../CommonBackButton.vue'
  8. // $walker.back not tested because there is a unit test for it
  9. describe('rendering common back button', () => {
  10. it('renders label', async () => {
  11. window.history.replaceState({ back: '/back' }, '/back')
  12. const view = renderComponent(CommonBackButton, {
  13. router: true,
  14. props: {
  15. fallback: '/back-url',
  16. },
  17. })
  18. expect(view.getByRole('button', { name: 'Go back' })).toBeInTheDocument()
  19. await view.rerender({
  20. label: 'Back',
  21. })
  22. expect(view.container).toHaveTextContent('Back')
  23. i18n.setTranslationMap(new Map([['Back', 'Zurück']]))
  24. await flushPromises()
  25. expect(view.container).toHaveTextContent('Zurück')
  26. expect(view.getByRole('button', { name: 'Go back' })).toBeInTheDocument()
  27. i18n.setTranslationMap(new Map([]))
  28. })
  29. it('renders home button, if no history is present', async () => {
  30. window.history.replaceState({}, '')
  31. const view = renderComponent(CommonBackButton, {
  32. router: true,
  33. props: {
  34. fallback: '/',
  35. },
  36. })
  37. expect(view.getByRole('button', { name: 'Go home' })).toBeInTheDocument()
  38. expect(view.getByIconName('home')).toBeInTheDocument()
  39. await view.rerender({
  40. label: 'Back',
  41. })
  42. expect(view.container).toHaveTextContent('Home')
  43. })
  44. it('renders home button, if history is present and previous route is home', async () => {
  45. window.history.replaceState({ back: '/' }, '/')
  46. const view = renderComponent(CommonBackButton, {
  47. router: true,
  48. props: {
  49. fallback: '/',
  50. },
  51. })
  52. expect(view.getByRole('button', { name: 'Go home' })).toBeInTheDocument()
  53. expect(view.getByIconName('home')).toBeInTheDocument()
  54. await view.rerender({
  55. label: 'Back',
  56. })
  57. expect(view.container).toHaveTextContent('Home')
  58. })
  59. it('renders back button, if history is present', async () => {
  60. window.history.replaceState(
  61. { back: '/tickets/1/information/customer' },
  62. '/tickets/1/information/customer',
  63. )
  64. const view = renderComponent(CommonBackButton, {
  65. router: true,
  66. props: {
  67. fallback: '/',
  68. },
  69. })
  70. expect(view.getByRole('button', { name: 'Go back' })).toBeInTheDocument()
  71. expect(view.getByIconName('chevron-left')).toBeInTheDocument()
  72. await view.rerender({
  73. label: 'Back',
  74. })
  75. expect(view.container).toHaveTextContent('Back')
  76. })
  77. it('back button changes direction when locale changes', async () => {
  78. window.history.replaceState(
  79. { back: '/tickets/1/information/customer' },
  80. '/tickets/1/information/customer',
  81. )
  82. const view = renderComponent(CommonBackButton, {
  83. router: true,
  84. props: {
  85. fallback: '/',
  86. },
  87. })
  88. expect(view.getByIconName('chevron-left')).toBeInTheDocument()
  89. const locale = useLocaleStore()
  90. locale.localeData = {
  91. dir: EnumTextDirection.Rtl,
  92. } as any
  93. await expect(
  94. view.findByIconName('chevron-right'),
  95. ).resolves.toBeInTheDocument()
  96. })
  97. })