dom.spec.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { domFrom, waitForImagesToLoad } from '../dom.ts'
  3. describe('domFrom', () => {
  4. const input = '<div>test</div>'
  5. it('parses dom and returns exact string representation', () => {
  6. const dom = domFrom(input)
  7. expect(dom.innerHTML).toBe(input)
  8. })
  9. it('parses dom and returns matching structure', () => {
  10. const dom = domFrom(input)
  11. expect(dom).toBeInstanceOf(HTMLElement)
  12. expect(dom.childNodes.length).toBe(1)
  13. const firstNode = dom.childNodes[0]
  14. expect(firstNode.textContent).toBe('test')
  15. expect(firstNode.childNodes[0]).toBeInstanceOf(Text)
  16. })
  17. })
  18. describe('waitForImagesToLoad', () => {
  19. it('resolves immediately if no images are present', async () => {
  20. const container = document.createElement('div')
  21. const promise = await waitForImagesToLoad(container)
  22. expect(promise).toEqual([])
  23. })
  24. it('resolves when all images load successfully', async () => {
  25. const container = document.createElement('div')
  26. const img1 = document.createElement('img')
  27. const img2 = document.createElement('img')
  28. container.appendChild(img1)
  29. container.appendChild(img2)
  30. const loadEvent = new Event('load')
  31. setTimeout(() => {
  32. img1.dispatchEvent(loadEvent)
  33. img2.dispatchEvent(loadEvent)
  34. }, 0)
  35. const promises = await waitForImagesToLoad(container)
  36. expect(promises).toHaveLength(2)
  37. promises.forEach((promise) => {
  38. expect(promise.status).toBe('fulfilled')
  39. })
  40. })
  41. it('rejects if any image fails to load', async () => {
  42. const container = document.createElement('div')
  43. const img1 = document.createElement('img')
  44. const img2 = document.createElement('img')
  45. container.appendChild(img1)
  46. container.appendChild(img2)
  47. const loadEvent = new Event('error')
  48. const errorEvent = new Event('error')
  49. setTimeout(() => {
  50. img1.dispatchEvent(loadEvent)
  51. img2.dispatchEvent(errorEvent)
  52. }, 0)
  53. const promises = await waitForImagesToLoad(container)
  54. promises.forEach((promise) => {
  55. expect(promise.status).toBe('rejected')
  56. })
  57. expect(promises).toHaveLength(2)
  58. })
  59. })