1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { describe, it, expect, vi, beforeEach } from 'vitest'
- import { mockRouterHooks } from '#tests/support/mock-vue-router.ts'
- import { useAbortNavigation } from '../useAbortNavigation.ts'
- const waitForVariantConfirmationMock = vi.fn()
- mockRouterHooks()
- 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()
- },
- )
- })
|