useAbortNavigation.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { describe, it, expect, vi, beforeEach } from 'vitest'
  3. import { useAbortNavigation } from '../useAbortNavigation.ts'
  4. const waitForVariantConfirmationMock = vi.fn()
  5. vi.mock('vue-router', () => ({
  6. onBeforeRouteLeave: vi.fn(),
  7. onBeforeRouteUpdate: vi.fn(),
  8. }))
  9. vi.mock('#shared/composables/useConfirmation.ts', () => ({
  10. useConfirmation: () => ({
  11. waitForVariantConfirmation: waitForVariantConfirmationMock,
  12. }),
  13. }))
  14. describe('useAbortNavigation', () => {
  15. let confirmCallbackMock: ReturnType<typeof vi.fn>
  16. let shouldConfirmNavigationMock: ReturnType<typeof vi.fn>
  17. beforeEach(() => {
  18. confirmCallbackMock = vi.fn()
  19. shouldConfirmNavigationMock = vi.fn()
  20. })
  21. it('allows navigation if shouldConfirmNavigation returns false', async () => {
  22. shouldConfirmNavigationMock.mockReturnValue(false)
  23. useAbortNavigation({
  24. confirmCallback: confirmCallbackMock,
  25. shouldConfirmNavigation: <() => boolean>shouldConfirmNavigationMock,
  26. })
  27. expect(waitForVariantConfirmationMock).not.toHaveBeenCalled()
  28. })
  29. it('aborts navigation if confirmation is not given', async () => {
  30. shouldConfirmNavigationMock.mockReturnValue(true)
  31. waitForVariantConfirmationMock.mockResolvedValue(false)
  32. useAbortNavigation({
  33. confirmCallback: confirmCallbackMock,
  34. shouldConfirmNavigation: <() => boolean>shouldConfirmNavigationMock,
  35. })
  36. // expect(result).toBe(false)
  37. // expect(waitForVariantConfirmationMock).toHaveBeenCalledWith('unsaved')
  38. expect(confirmCallbackMock).not.toHaveBeenCalled()
  39. })
  40. it.todo('confirms navigation if confirmation is given', async () => {
  41. shouldConfirmNavigationMock.mockReturnValue(true)
  42. waitForVariantConfirmationMock.mockResolvedValue(true)
  43. useAbortNavigation({
  44. confirmCallback: confirmCallbackMock,
  45. shouldConfirmNavigation: <() => boolean>shouldConfirmNavigationMock,
  46. })
  47. // expect(result).toBe(true)
  48. // expect(waitForVariantConfirmationMock).toHaveBeenCalledWith('unsaved')
  49. expect(confirmCallbackMock).toHaveBeenCalled()
  50. })
  51. it.todo(
  52. 'handles onBeforeRouteLeave similarly to onBeforeRouteUpdate',
  53. async () => {
  54. shouldConfirmNavigationMock.mockReturnValue(true)
  55. waitForVariantConfirmationMock.mockResolvedValue(true)
  56. useAbortNavigation({
  57. confirmCallback: confirmCallbackMock,
  58. shouldConfirmNavigation: <() => boolean>shouldConfirmNavigationMock,
  59. })
  60. // expect(result).toBe(true)
  61. // expect(waitForVariantConfirmationMock).toHaveBeenCalledWith('unsaved')
  62. expect(confirmCallbackMock).toHaveBeenCalled()
  63. },
  64. )
  65. })