Skip to content

Commit a6cef81

Browse files
committed
test(angular): fix regexp
1 parent 9694e16 commit a6cef81

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { test, expect } from '@playwright/test';
22
import { testForward, testRoot, testBack, testLifeCycle } from '../../utils/test-helpers';
33

4+
// Helper function to escape regex special characters
5+
function escapeRegExp(string: string): string {
6+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
7+
}
8+
49
test.describe('Router Link', () => {
510
test.beforeEach(async ({ page }) => {
611
await page.goto('/lazy/router-link?ionic:_testing=true');
@@ -19,9 +24,9 @@ test.describe('Router Link', () => {
1924
const expectedHash = `#${encodeURIComponent(fragment)}`;
2025

2126
// Check that URL contains all expected parts
22-
await expect(page).toHaveURL(new RegExp(expectedPath.replace(/=/g, '\\=')));
23-
await expect(page).toHaveURL(new RegExp(expectedSearch.replace(/\?/g, '\\?')));
24-
await expect(page).toHaveURL(new RegExp(expectedHash));
27+
await expect(page).toHaveURL(new RegExp(escapeRegExp(expectedPath)));
28+
await expect(page).toHaveURL(new RegExp(escapeRegExp(expectedSearch)));
29+
await expect(page).toHaveURL(new RegExp(escapeRegExp(expectedHash)));
2530
});
2631

2732
test('should return to a page with preserved query param and fragment', async ({ page }) => {
@@ -36,9 +41,9 @@ test.describe('Router Link', () => {
3641
const expectedSearch = `?token=${encodeURIComponent(queryParam)}`;
3742
const expectedHash = `#${encodeURIComponent(fragment)}`;
3843

39-
await expect(page).toHaveURL(new RegExp(expectedPath.replace(/=/g, '\\=')));
40-
await expect(page).toHaveURL(new RegExp(expectedSearch.replace(/\?/g, '\\?')));
41-
await expect(page).toHaveURL(new RegExp(expectedHash));
44+
await expect(page).toHaveURL(new RegExp(escapeRegExp(expectedPath)));
45+
await expect(page).toHaveURL(new RegExp(escapeRegExp(expectedSearch)));
46+
await expect(page).toHaveURL(new RegExp(escapeRegExp(expectedHash)));
4247
});
4348

4449
test('should preserve query param and fragment with defaultHref string', async ({ page }) => {
@@ -49,8 +54,8 @@ test.describe('Router Link', () => {
4954
const expectedSearch = '?token=ABC';
5055
const expectedHash = '#fragment';
5156

52-
await expect(page).toHaveURL(new RegExp(expectedSearch.replace(/\?/g, '\\?')));
53-
await expect(page).toHaveURL(new RegExp(expectedHash));
57+
await expect(page).toHaveURL(new RegExp(escapeRegExp(expectedSearch)));
58+
await expect(page).toHaveURL(new RegExp(escapeRegExp(expectedHash)));
5459
});
5560
});
5661

0 commit comments

Comments
 (0)