-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: stabilize image loading for visual regression tests (#3023)
- Loading branch information
1 parent
aa55e7f
commit 6000abc
Showing
5 changed files
with
35 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,41 @@ | ||
import type { SbbImageElement } from '../../image.js'; | ||
import { isSafari } from '../dom.js'; | ||
|
||
export async function waitForImageReady( | ||
element: HTMLImageElement | SbbImageElement, | ||
timeoutInMilliseconds = 2 * 1000, | ||
): Promise<void> { | ||
const imgElement = | ||
element.localName === 'sbb-image' | ||
? (element.shadowRoot?.querySelector<HTMLImageElement>('.sbb-image__img') ?? null) | ||
: (element as HTMLImageElement); | ||
|
||
if (!imgElement) { | ||
throw new Error('img tag not found'); | ||
} | ||
|
||
if (!element.complete) { | ||
await new Promise<void>((resolve, reject) => { | ||
const timeout = setTimeout(() => reject('image loading timeout'), timeoutInMilliseconds); | ||
element.addEventListener('load', () => { | ||
clearTimeout(timeout); | ||
resolve(); | ||
|
||
imgElement.decode().then(() => { | ||
if (isSafari && element.localName === 'sbb-image') { | ||
// On a test page this is only happening once (first time an image is loaded). Therefore, the impact is very small. | ||
setTimeout(resolve, 100); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
|
||
element.addEventListener('error', () => { | ||
clearTimeout(timeout); | ||
reject('image error'); | ||
}); | ||
}); | ||
} else { | ||
await imgElement.decode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters