|
@@ -1,6 +1,6 @@
|
|
|
// Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
|
|
|
|
|
|
-import { domFrom } from '../dom.ts'
|
|
|
+import { domFrom, waitForImagesToLoad } from '../dom.ts'
|
|
|
|
|
|
describe('domFrom', () => {
|
|
|
const input = '<div>test</div>'
|
|
@@ -23,3 +23,58 @@ describe('domFrom', () => {
|
|
|
expect(firstNode.childNodes[0]).toBeInstanceOf(Text)
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+describe('waitForImagesToLoad', () => {
|
|
|
+ it('resolves immediately if no images are present', async () => {
|
|
|
+ const container = document.createElement('div')
|
|
|
+
|
|
|
+ const promise = await waitForImagesToLoad(container)
|
|
|
+
|
|
|
+ expect(promise).toEqual([])
|
|
|
+ })
|
|
|
+
|
|
|
+ it('resolves when all images load successfully', async () => {
|
|
|
+ const container = document.createElement('div')
|
|
|
+ const img1 = document.createElement('img')
|
|
|
+ const img2 = document.createElement('img')
|
|
|
+ container.appendChild(img1)
|
|
|
+ container.appendChild(img2)
|
|
|
+
|
|
|
+ const loadEvent = new Event('load')
|
|
|
+
|
|
|
+ setTimeout(() => {
|
|
|
+ img1.dispatchEvent(loadEvent)
|
|
|
+ img2.dispatchEvent(loadEvent)
|
|
|
+ }, 0)
|
|
|
+
|
|
|
+ const promises = await waitForImagesToLoad(container)
|
|
|
+
|
|
|
+ expect(promises).toHaveLength(2)
|
|
|
+ promises.forEach((promise) => {
|
|
|
+ expect(promise.status).toBe('fulfilled')
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('rejects if any image fails to load', async () => {
|
|
|
+ const container = document.createElement('div')
|
|
|
+ const img1 = document.createElement('img')
|
|
|
+ const img2 = document.createElement('img')
|
|
|
+ container.appendChild(img1)
|
|
|
+ container.appendChild(img2)
|
|
|
+
|
|
|
+ const loadEvent = new Event('error')
|
|
|
+ const errorEvent = new Event('error')
|
|
|
+
|
|
|
+ setTimeout(() => {
|
|
|
+ img1.dispatchEvent(loadEvent)
|
|
|
+ img2.dispatchEvent(errorEvent)
|
|
|
+ }, 0)
|
|
|
+
|
|
|
+ const promises = await waitForImagesToLoad(container)
|
|
|
+
|
|
|
+ promises.forEach((promise) => {
|
|
|
+ expect(promise.status).toBe('rejected')
|
|
|
+ })
|
|
|
+ expect(promises).toHaveLength(2)
|
|
|
+ })
|
|
|
+})
|