useAbortNavigation.spec.ts 2.6 KB

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