Skip to content

Commit 5f27d94

Browse files
committed
test(angular): update utils
1 parent 7d5be0a commit 5f27d94

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

packages/angular/test/base/e2e/src/lazy/router-link.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ test.describe('Router Link', () => {
132132
});
133133
});
134134

135+
// Angular sets the `tabindex` to `"0"` on any element that uses
136+
// the `routerLink` directive. Ionic removes the `tabindex` from
137+
// components that wrap an `a` or `button` element, so we are
138+
// checking here that it is only removed from Ionic components.
139+
// https://github.com/ionic-team/ionic-framework/issues/20632
135140
test.describe('tabindex', () => {
136141
test('should have tabindex="0" with a native span', async ({ page }) => {
137142
await expect(page.locator('#span')).toHaveAttribute('tabindex', '0');

packages/angular/test/base/e2e/src/lazy/routing.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { test, expect } from '@playwright/test';
2-
import { ionSwipeToGoBack, ionPageVisible, ionPageHidden, ionPageDoesNotExist, testStack } from '../../utils/test-utils';
2+
import { ionSwipeToGoBack } from '../../utils/drag-utils';
3+
import { ionPageVisible, ionPageHidden, ionPageDoesNotExist, testStack } from '../../utils/test-utils';
34

45
test.describe('Routing', () => {
56
test.beforeEach(async ({ page }) => {

packages/angular/test/base/e2e/utils/drag-utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Locator, Page } from '@playwright/test';
22

3+
// Drag an element by a given amount of pixels
34
export const dragElementBy = async (
45
element: Locator,
56
page: Page,
@@ -21,3 +22,18 @@ export const dragElementBy = async (
2122
await page.mouse.move(endX, endY);
2223
await page.mouse.up();
2324
};
25+
26+
// Simulate swipe gesture for going back
27+
export const ionSwipeToGoBack = async (page: Page, shouldGoBack = false) => {
28+
const viewport = page.viewportSize();
29+
if (!viewport) return;
30+
31+
const startX = 50;
32+
const endX = shouldGoBack ? viewport.width - 50 : 50;
33+
const y = viewport.height / 2;
34+
35+
await page.mouse.move(startX, y);
36+
await page.mouse.down();
37+
await page.mouse.move(endX, y);
38+
await page.mouse.up();
39+
}

packages/angular/test/base/e2e/utils/test-utils.ts

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@ export async function testStack(page: Page, selector: string, expectedStack: str
44
const elements = page.locator(`${selector} > *`);
55
const count = await elements.count();
66

7-
// Ensure we have at least the expected number of elements
8-
expect(count).toBeGreaterThanOrEqual(expectedStack.length);
9-
10-
for (let i = 0; i < expectedStack.length; i++) {
11-
const element = elements.nth(i);
12-
// Check that the element tag name matches the expected component name
13-
const tagName = await element.evaluate(el => el.tagName.toLowerCase());
14-
expect(tagName).toBe(expectedStack[i]);
7+
// Get the actual stack (tag names of child elements), filtering out non-components
8+
const actualStack: string[] = [];
9+
for (let i = 0; i < count; i++) {
10+
const tagName = await elements.nth(i).evaluate(el => el.tagName.toLowerCase());
11+
// Filter out non-component elements like 'slot', 'div', etc.
12+
if (tagName.includes('-') || tagName.startsWith('app-') || tagName.startsWith('ion-')) {
13+
actualStack.push(tagName);
14+
}
1515
}
16+
17+
// Compare the actual stack with the expected stack
18+
expect(actualStack).toEqual(expectedStack);
1619
}
1720

1821
export async function testLifeCycle(page: Page, selector: string, expectedCounts: Record<string, number>) {
19-
for (const [event, count] of Object.entries(expectedCounts)) {
20-
// Look for text content within the specific selector
21-
await expect(page.locator(selector).locator(`text=${event}: ${count}`)).toBeVisible();
22-
}
22+
await expect(page.locator(`${selector} #ngOnInit`)).toHaveText('1');
23+
await expect(page.locator(`${selector} #ionViewWillEnter`)).toHaveText(expectedCounts.ionViewWillEnter.toString());
24+
await expect(page.locator(`${selector} #ionViewDidEnter`)).toHaveText(expectedCounts.ionViewDidEnter.toString());
25+
await expect(page.locator(`${selector} #ionViewWillLeave`)).toHaveText(expectedCounts.ionViewWillLeave.toString());
26+
await expect(page.locator(`${selector} #ionViewDidLeave`)).toHaveText(expectedCounts.ionViewDidLeave.toString());
2327
}
2428

2529
export async function ionPageVisible(page: Page, selector: string) {
@@ -41,21 +45,6 @@ export async function ionTabClick(page: Page, tabName: string) {
4145
await page.locator(`ion-tab-button`).filter({ hasText: tabName }).click();
4246
}
4347

44-
export async function ionSwipeToGoBack(page: Page, shouldGoBack = false) {
45-
// Simulate swipe gesture for going back
46-
const viewport = page.viewportSize();
47-
if (!viewport) return;
48-
49-
const startX = 50;
50-
const endX = shouldGoBack ? viewport.width - 50 : 50;
51-
const y = viewport.height / 2;
52-
53-
await page.mouse.move(startX, y);
54-
await page.mouse.down();
55-
await page.mouse.move(endX, y);
56-
await page.mouse.up();
57-
}
58-
5948
export async function testTabTitle(page: Page, title: string) {
6049
const tab = await getSelectedTab(page);
6150
await expect(tab.locator('ion-title')).toHaveText(title);
@@ -104,6 +93,9 @@ export async function testUrlEquals(page: Page, url: string) {
10493
}
10594

10695
export async function testForward(page: Page) {
96+
// Wait for navigation to complete
97+
await page.waitForTimeout(100);
98+
10799
await testStack(page, 'ion-router-outlet', ['app-router-link', 'app-router-link-page']);
108100
await testLifeCycle(page, 'app-router-link-page', {
109101
ionViewWillEnter: 1,
@@ -114,6 +106,7 @@ export async function testForward(page: Page) {
114106
await expect(page.locator('app-router-link-page #canGoBack')).toHaveText('true');
115107

116108
await page.goBack();
109+
await page.waitForTimeout(100);
117110
await testStack(page, 'ion-router-outlet', ['app-router-link']);
118111
await testLifeCycle(page, 'app-router-link', {
119112
ionViewWillEnter: 2,
@@ -134,7 +127,7 @@ export async function testRoot(page: Page) {
134127
await expect(page.locator('app-router-link-page #canGoBack')).toHaveText('false');
135128

136129
await page.goBack();
137-
await page.waitForTimeout(100); // Wait for back navigation to complete
130+
await page.waitForTimeout(100);
138131
await testStack(page, 'ion-router-outlet', ['app-router-link']);
139132
await testLifeCycle(page, 'app-router-link', {
140133
ionViewWillEnter: 1,
@@ -145,7 +138,6 @@ export async function testRoot(page: Page) {
145138
}
146139

147140
export async function testBack(page: Page) {
148-
// First check that we're on the router-link-page
149141
await testStack(page, 'ion-router-outlet', ['app-router-link-page']);
150142
await testLifeCycle(page, 'app-router-link-page', {
151143
ionViewWillEnter: 1,
@@ -155,7 +147,6 @@ export async function testBack(page: Page) {
155147
});
156148
await expect(page.locator('app-router-link-page #canGoBack')).toHaveText('false');
157149

158-
// Then go back
159150
await page.goBack();
160151
await page.waitForTimeout(100);
161152
await testStack(page, 'ion-router-outlet', ['app-router-link']);

0 commit comments

Comments
 (0)