123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { describe, it, expect, vi, beforeEach } from 'vitest'
- import { useAbortNavigation } from '../useAbortNavigation.ts'
- const waitForVariantConfirmationMock = vi.fn()
- vi.mock('vue-router', () => ({
- onBeforeRouteLeave: vi.fn(),
- onBeforeRouteUpdate: vi.fn(),
- }))
- vi.mock('#shared/composables/useConfirmation.ts', () => ({
- useConfirmation: () => ({
- waitForVariantConfirmation: waitForVariantConfirmationMock,
- }),
- }))
- describe('useAbortNavigation', () => {
- let confirmCallbackMock: ReturnType<typeof vi.fn>
- let shouldConfirmNavigationMock: ReturnType<typeof vi.fn>
- beforeEach(() => {
- confirmCallbackMock = vi.fn()
- shouldConfirmNavigationMock = vi.fn()
- })
- it('allows navigation if shouldConfirmNavigation returns false', async () => {
- shouldConfirmNavigationMock.mockReturnValue(false)
- useAbortNavigation({
- confirmCallback: confirmCallbackMock,
- shouldConfirmNavigation: <() => boolean>shouldConfirmNavigationMock,
- })
- expect(waitForVariantConfirmationMock).not.toHaveBeenCalled()
- })
- it('aborts navigation if confirmation is not given', async () => {
- shouldConfirmNavigationMock.mockReturnValue(true)
- waitForVariantConfirmationMock.mockResolvedValue(false)
- useAbortNavigation({
- confirmCallback: confirmCallbackMock,
- shouldConfirmNavigation: <() => boolean>shouldConfirmNavigationMock,
- })
- // expect(result).toBe(false)
- // expect(waitForVariantConfirmationMock).toHaveBeenCalledWith('unsaved')
- expect(confirmCallbackMock).not.toHaveBeenCalled()
- })
- it.todo('confirms navigation if confirmation is given', async () => {
- shouldConfirmNavigationMock.mockReturnValue(true)
- waitForVariantConfirmationMock.mockResolvedValue(true)
- useAbortNavigation({
- confirmCallback: confirmCallbackMock,
- shouldConfirmNavigation: <() => boolean>shouldConfirmNavigationMock,
- })
- // expect(result).toBe(true)
- // expect(waitForVariantConfirmationMock).toHaveBeenCalledWith('unsaved')
- expect(confirmCallbackMock).toHaveBeenCalled()
- })
- it.todo(
- 'handles onBeforeRouteLeave similarly to onBeforeRouteUpdate',
- async () => {
- shouldConfirmNavigationMock.mockReturnValue(true)
- waitForVariantConfirmationMock.mockResolvedValue(true)
- useAbortNavigation({
- confirmCallback: confirmCallbackMock,
- shouldConfirmNavigation: <() => boolean>shouldConfirmNavigationMock,
- })
- // expect(result).toBe(true)
- // expect(waitForVariantConfirmationMock).toHaveBeenCalledWith('unsaved')
- expect(confirmCallbackMock).toHaveBeenCalled()
- },
- )
- })
|