diff --git a/core/src/components/img/test/basic/img.e2e.ts b/core/src/components/img/test/basic/img.e2e.ts index 8dbed904f0..07d8fa4edd 100644 --- a/core/src/components/img/test/basic/img.e2e.ts +++ b/core/src/components/img/test/basic/img.e2e.ts @@ -1,30 +1,94 @@ import { expect } from '@playwright/test'; +import type { EventSpy } from '@utils/test/playwright'; import { test } from '@utils/test/playwright'; test.describe('img: basic', () => { - test('should not have visual regressions', async ({ page }) => { - await page.goto('/src/components/img/test/basic'); + test.beforeEach(({ skip }) => { + skip.rtl(); + skip.mode('ios'); + }); - /** - * The first `ion-img` resolves before the spy can be created, - * since the image is initially visible and the IO fires. - * - * We manually look at the image's completion state to determine - * that it loaded, before creating a spy for the second image. - */ - const img = await page.locator('.img-part img'); - await img.evaluate((el: HTMLImageElement) => el.complete); + test.describe('image successfully loads', () => { + let ionImgWillLoad: EventSpy; + let ionImgDidLoad: EventSpy; - const ionImgDidLoad = await page.spyOnEvent('ionImgDidLoad'); - /** - * Resizing the viewport will cause the intersection observers of - * the remaining images to fire, which will cause the `ionImgDidLoad` - * event to fire. - */ - await page.setIonViewport(); + test.beforeEach(async ({ page }) => { + await page.route('**/*', (route) => { + if (route.request().resourceType() === 'image') { + return route.fulfill({ + status: 200, + contentType: 'image/png', + body: Buffer.from( + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIwAAAABJRU5ErkJggg==', + 'base64' + ), + }); + } + return route.continue(); + }); - await ionImgDidLoad.next(); + /** + * We render the img intentionally without providing a source, + * to allow the event spies to be set-up before the events + * can be emitted. + * + * Later we will assign an image source to load. + */ + await page.setContent(''); - expect(await page.screenshot()).toMatchSnapshot(`img-basic-${page.getSnapshotSettings()}.png`); + ionImgDidLoad = await page.spyOnEvent('ionImgDidLoad'); + ionImgWillLoad = await page.spyOnEvent('ionImgWillLoad'); + + const ionImg = await page.locator('ion-img'); + await ionImg.evaluate((el: HTMLIonImgElement) => { + el.src = 'https://via.placeholder.com/150'; + return el; + }); + }); + + test('should emit ionImgWillLoad', async () => { + await ionImgWillLoad.next(); + + expect(ionImgWillLoad).toHaveReceivedEventTimes(1); + }); + + test('should emit ionImgDidLoad', async () => { + await ionImgDidLoad.next(); + + expect(ionImgWillLoad).toHaveReceivedEventTimes(1); + }); + }); + + test.describe('image fails to load', () => { + let ionError: EventSpy; + + test.beforeEach(async ({ page }) => { + await page.route('**/*', (route) => + route.request().resourceType() === 'image' ? route.abort() : route.continue() + ); + + /** + * We render the img intentionally without providing a source, + * to allow the event spies to be set-up before the events + * can be emitted. + * + * Later we will assign an image source to load. + */ + await page.setContent(''); + + ionError = await page.spyOnEvent('ionError'); + + const ionImg = await page.locator('ion-img'); + await ionImg.evaluate((el: HTMLIonImgElement) => { + el.src = 'https://via.placeholder.com/150'; + return el; + }); + }); + + test('should emit ionError', async () => { + await ionError.next(); + + expect(ionError).toHaveReceivedEventTimes(1); + }); }); }); diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-ltr-Mobile-Chrome-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-ltr-Mobile-Chrome-linux.png deleted file mode 100644 index 967a29ade8..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-ltr-Mobile-Chrome-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-ltr-Mobile-Firefox-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-ltr-Mobile-Firefox-linux.png deleted file mode 100644 index 2c9c0aeac3..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-ltr-Mobile-Firefox-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-ltr-Mobile-Safari-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-ltr-Mobile-Safari-linux.png deleted file mode 100644 index 99593005d0..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-ltr-Mobile-Safari-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-rtl-Mobile-Chrome-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-rtl-Mobile-Chrome-linux.png deleted file mode 100644 index 0ac7c5031b..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-rtl-Mobile-Chrome-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-rtl-Mobile-Firefox-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-rtl-Mobile-Firefox-linux.png deleted file mode 100644 index 1e2860bd6f..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-rtl-Mobile-Firefox-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-rtl-Mobile-Safari-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-rtl-Mobile-Safari-linux.png deleted file mode 100644 index 3559bc57b7..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-ios-rtl-Mobile-Safari-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-ltr-Mobile-Chrome-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-ltr-Mobile-Chrome-linux.png deleted file mode 100644 index 2d5ccb3be2..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-ltr-Mobile-Chrome-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-ltr-Mobile-Firefox-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-ltr-Mobile-Firefox-linux.png deleted file mode 100644 index 34c2dc317a..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-ltr-Mobile-Firefox-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-ltr-Mobile-Safari-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-ltr-Mobile-Safari-linux.png deleted file mode 100644 index 7f771b0796..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-ltr-Mobile-Safari-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-rtl-Mobile-Chrome-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-rtl-Mobile-Chrome-linux.png deleted file mode 100644 index 090988a3cb..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-rtl-Mobile-Chrome-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-rtl-Mobile-Firefox-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-rtl-Mobile-Firefox-linux.png deleted file mode 100644 index 9063cbc12e..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-rtl-Mobile-Firefox-linux.png and /dev/null differ diff --git a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-rtl-Mobile-Safari-linux.png b/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-rtl-Mobile-Safari-linux.png deleted file mode 100644 index 8b026e7344..0000000000 Binary files a/core/src/components/img/test/basic/img.e2e.ts-snapshots/img-basic-md-rtl-Mobile-Safari-linux.png and /dev/null differ