test(img): migrate to generators (#27319)

Issue number: N/A

---------

<!-- Please refer to our contributing documentation for any questions on
submitting a pull request, or let us know here if you need any help:
https://ionicframework.com/docs/building/contributing -->

<!-- Some docs updates need to be made in the `ionic-docs` repo, in a
separate PR. See
https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#modifying-documentation
for details. -->

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type (bugfix, feature,
etc). Submit multiple pull requests if needed. -->

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

Img tests use legacy syntax

## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- Img tests use modern syntax

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!-- If this introduces a breaking change, please describe the impact
and migration path for existing applications below. -->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->
This commit is contained in:
Liam DeBeasi
2023-04-28 11:10:51 -04:00
committed by GitHub
parent bc05c57c3c
commit 17b43293bb
4 changed files with 114 additions and 112 deletions

View File

@@ -1,95 +0,0 @@
import { expect } from '@playwright/test';
import type { EventSpy } from '@utils/test/playwright';
import { test } from '@utils/test/playwright';
test.describe('img: basic', () => {
test.beforeEach(({ skip }) => {
skip.rtl();
skip.mode('ios');
});
// TODO FW-3596
test.describe.skip('image successfully loads', () => {
let ionImgWillLoad: EventSpy;
let ionImgDidLoad: EventSpy;
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();
});
/**
* 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('<ion-img></ion-img>');
ionImgDidLoad = await page.spyOnEvent('ionImgDidLoad');
ionImgWillLoad = await page.spyOnEvent('ionImgWillLoad');
const ionImg = 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('<ion-img></ion-img>');
ionError = await page.spyOnEvent('ionError');
const ionImg = 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);
});
});
});

View File

@@ -0,0 +1,92 @@
import { expect } from '@playwright/test';
import type { EventSpy } from '@utils/test/playwright';
import { configs, test } from '@utils/test/playwright';
configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
test.describe(title('img: basic'), () => {
// TODO FW-3596
test.describe.skip('image successfully loads', () => {
let ionImgWillLoad: EventSpy;
let ionImgDidLoad: EventSpy;
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();
});
/**
* 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('<ion-img></ion-img>', config);
ionImgDidLoad = await page.spyOnEvent('ionImgDidLoad');
ionImgWillLoad = await page.spyOnEvent('ionImgWillLoad');
const ionImg = 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('<ion-img></ion-img>', config);
ionError = await page.spyOnEvent('ionError');
const ionImg = 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);
});
});
});
});

View File

@@ -1,17 +0,0 @@
import { expect } from '@playwright/test';
import { test } from '@utils/test/playwright';
test.describe('img: draggable', () => {
test('should correctly set draggable attribute on inner img element', async ({ page }) => {
await page.goto('/src/components/img/test/draggable');
const imgDraggableTrue = page.locator('#img-draggable-true img');
await expect(imgDraggableTrue).toHaveAttribute('draggable', 'true');
const imgDraggableFalse = page.locator('#img-draggable-false img');
await expect(imgDraggableFalse).toHaveAttribute('draggable', 'false');
const imgDraggableUnset = page.locator('#img-draggable-unset img');
expect(await imgDraggableUnset.getAttribute('draggable')).toBeNull();
});
});

View File

@@ -0,0 +1,22 @@
import { expect } from '@playwright/test';
import { configs, test } from '@utils/test/playwright';
/**
* This behavior does not vary across modes/directions.
*/
configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => {
test.describe(title('img: draggable'), () => {
test('should correctly set draggable attribute on inner img element', async ({ page }) => {
await page.goto('/src/components/img/test/draggable', config);
const imgDraggableTrue = page.locator('#img-draggable-true img');
await expect(imgDraggableTrue).toHaveAttribute('draggable', 'true');
const imgDraggableFalse = page.locator('#img-draggable-false img');
await expect(imgDraggableFalse).toHaveAttribute('draggable', 'false');
const imgDraggableUnset = page.locator('#img-draggable-unset img');
expect(await imgDraggableUnset.getAttribute('draggable')).toBeNull();
});
});
});