walker.spec.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { RouteLocationNormalized, Router } from 'vue-router'
  3. import { Walker } from '../walker.ts'
  4. const buildRouter = () =>
  5. ({
  6. afterEach: vi.fn(),
  7. back: vi.fn(),
  8. push: vi.fn(),
  9. }) as any as Router
  10. describe('testing walker', () => {
  11. it('has fallback route, if no back is in history', async () => {
  12. const router = buildRouter()
  13. const walker = new Walker(router)
  14. expect(walker.hasBackUrl).toBe(false)
  15. expect(walker.getBackUrl('/fallback')).toBe('/fallback')
  16. await walker.back('/fallback')
  17. expect(router.push).toHaveBeenCalledWith('/fallback')
  18. })
  19. it('has back route, if there is back route in history', async () => {
  20. window.history.replaceState({ back: '/back' }, '', '/back')
  21. const router = buildRouter()
  22. const walker = new Walker(router)
  23. expect(walker.hasBackUrl).toBe(true)
  24. expect(walker.getBackUrl('/fallback')).toBe('/back')
  25. await walker.back('/fallback')
  26. expect(router.back).toHaveBeenCalled()
  27. })
  28. it('changes back route after changing route', () => {
  29. window.history.replaceState({ back: null }, '', '/back')
  30. const router = buildRouter()
  31. const walker = new Walker(router)
  32. expect(walker.hasBackUrl).toBe(false)
  33. const route = {} as RouteLocationNormalized
  34. const afterEach = vi.mocked(router.afterEach).mock.calls[0][0]
  35. window.history.replaceState({ back: '/back' }, '', '/back')
  36. afterEach(route, route)
  37. expect(walker.hasBackUrl).toBe(true)
  38. expect(walker.getBackUrl('/fallback')).toBe('/back')
  39. })
  40. it('does not cycle with ignore list match', async () => {
  41. window.history.replaceState(
  42. { back: '/tickets/1/information/customer' },
  43. '',
  44. '/tickets/1/information/customer',
  45. )
  46. const router = buildRouter()
  47. const walker = new Walker(router)
  48. await walker.back('/fallback', ['/tickets/1/information'])
  49. expect(router.push).toHaveBeenCalledWith('/fallback')
  50. })
  51. it('does not cycle without ignore list match', async () => {
  52. window.history.replaceState(
  53. { back: '/tickets/1/information/customer' },
  54. '',
  55. '/tickets/1/information/customer',
  56. )
  57. const router = buildRouter()
  58. const walker = new Walker(router)
  59. await walker.back('/fallback', ['/tickets/99999/information'])
  60. expect(router.back).toHaveBeenCalled()
  61. })
  62. })