// Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
import { createTestingPinia } from '@pinia/testing'
import { useHtmlLinks } from '../useHtmlLinks.ts'
const buildSampleElement = () => {
const elem = document.createElement('div')
elem.innerHTML = `some text
external
internal
blank
mention
her text`
return elem
}
const routerPush = vi.fn()
vi.mock('vue-router', async () => {
return {
useRouter: () => {
return {
push: routerPush,
}
},
}
})
describe('setupLinkHandlers', () => {
createTestingPinia({ createSpy: vi.fn })
it('replaces mention links with URLs to user profile', async () => {
const elem = buildSampleElement()
const { setupLinksHandlers } = useHtmlLinks('/desktop')
setupLinksHandlers(elem)
elem.querySelector('a[mention]')?.click()
expect(routerPush).toHaveBeenCalledWith('/users/123')
})
it('opens internal link via the router', () => {
const elem = buildSampleElement()
const { setupLinksHandlers } = useHtmlLinks('/desktop')
setupLinksHandlers(elem)
elem.querySelector('a[internal]')?.click()
expect(routerPush).toHaveBeenCalledWith('/test')
})
it('opens internal _blank link in a new tab', () => {
vi.stubGlobal('open', vi.fn())
const spy = vi.spyOn(window, 'open')
const elem = buildSampleElement()
const { setupLinksHandlers } = useHtmlLinks('/desktop')
setupLinksHandlers(elem)
elem.querySelector('a[blank]')?.click()
expect(spy).toHaveBeenCalledWith('/desktop/blank', '_blank')
})
it('opens external link directly', () => {
vi.stubGlobal('open', vi.fn())
const spy = vi.spyOn(window, 'open')
const elem = buildSampleElement()
const { setupLinksHandlers } = useHtmlLinks('/desktop')
setupLinksHandlers(elem)
elem.querySelector('a[external]')?.click()
expect(routerPush).not.toHaveBeenCalled()
expect(spy).not.toHaveBeenCalled()
})
})