useHtmlInlineImages.spec.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { describe, expect, vi } from 'vitest'
  3. import { ref } from 'vue'
  4. import type { ImageViewerFile } from '#shared/composables/useImageViewer.ts'
  5. import { useHtmlInlineImages } from '../useHtmlInlineImages.ts'
  6. const buildSampleElement = () => {
  7. const elem = document.createElement('div')
  8. elem.innerHTML = `some text <img src="nonexistant.jpg">another text`
  9. return elem
  10. }
  11. describe('populateInlineImages', () => {
  12. it('clears existing entries in inlineImages', () => {
  13. const inlineImages = ref<ImageViewerFile[]>([])
  14. inlineImages.value.push({ name: 'sample entry' })
  15. buildSampleElement()
  16. const { populateInlineImages } = useHtmlInlineImages(inlineImages, vi.fn())
  17. populateInlineImages(document.createElement('div'))
  18. expect(inlineImages.value.length).toBe(0)
  19. })
  20. it('adds images to inline images', () => {
  21. const inlineImages = ref<ImageViewerFile[]>([])
  22. const elem = buildSampleElement()
  23. const { populateInlineImages } = useHtmlInlineImages(inlineImages, vi.fn())
  24. populateInlineImages(elem)
  25. expect(inlineImages.value).toEqual([
  26. {
  27. inline: 'http://localhost:3000/nonexistant.jpg',
  28. name: '',
  29. type: 'image/jpeg',
  30. },
  31. ])
  32. })
  33. it('adds onclick callback to images', () => {
  34. const inlineImages = ref<ImageViewerFile[]>([])
  35. const elem = buildSampleElement()
  36. const mockedCallback = vi.fn()
  37. const { populateInlineImages } = useHtmlInlineImages(
  38. inlineImages,
  39. mockedCallback,
  40. )
  41. populateInlineImages(elem)
  42. elem.querySelector('img')?.click()
  43. expect(mockedCallback).toHaveBeenCalledWith(0)
  44. })
  45. })