walker.spec.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { Walker } from '../walker.ts'
  3. import type { RouteLocationNormalized, Router } from 'vue-router'
  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. })