Skip to content

Commit

Permalink
{feature} add isOpen variant to screen types
Browse files Browse the repository at this point in the history
This makes it easier to write tests that span multiple pages,
for example

```ts
class ScreenOne extends ComponentHarness {
	static path = '/one';

	#next = this.locatorFor('#next');

	async goToNext() {
		await (await this.next()).click();
	}
}

class ScreenTwo extends ComponentHarness {
	static async isOpen(page) {
	  return page.url().endsWith('/two');
	}
}

test('navigation', ({open}) => {
  const screenOne = await open(ScreenOne);

  await screenOne.goToNext();

  const screenTwo = await open(ScreenTwo);

  // ...
});
```

Screens with a path or open function can also define an
isOpen function. If that function is present and it yields
a truthy result, then the open function will not be called
or no navigation will be triggered to the path.
  • Loading branch information
bgotink committed Oct 10, 2024
1 parent edd62cc commit 0f96f97
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/c84ee467.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@ngx-playwright/test": patch
19 changes: 13 additions & 6 deletions packages/test/src/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ export async function openScreen(baseURL, page, harnessEnvironment, screen) {
);
}

if (hasOpenFunction(screen)) {
await screen.open(page, baseURL, (screen) =>
openScreen(baseURL, page, harnessEnvironment, screen),
);
} else {
await page.goto(new URL(screen.path, baseURL).href);
if (
typeof screen.isOpen !== "function" ||
!(await screen.isOpen(page, baseURL))
) {
if (hasOpenFunction(screen)) {
await screen.open(page, baseURL, (screen) =>
openScreen(baseURL, page, harnessEnvironment, screen),
);
} else if ("path" in screen && typeof screen.path === "string") {
await page.goto(new URL(screen.path, baseURL).href);
} else {
throw new Error("Expected screen to be open but it wasn't");
}
}

return harnessEnvironment.getHarness(screen);
Expand Down
13 changes: 12 additions & 1 deletion packages/test/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {Page} from "@playwright/test";
export interface PlaywrightScreenWithPath<T extends AnyComponentHarness>
extends ComponentHarnessConstructor<T> {
readonly path: string;

isOpen?(page: Page, baseUrl: string): Promise<boolean>;
}

export interface PlaywrightScreenWithOpenFunction<T extends AnyComponentHarness>
Expand All @@ -17,11 +19,20 @@ export interface PlaywrightScreenWithOpenFunction<T extends AnyComponentHarness>
baseUrl: string,
opener: PlaywrightScreenOpener,
): Promise<void>;

isOpen?(page: Page, baseUrl: string): Promise<boolean>;
}

export interface PlaywrightScreenWithIsOpenFunction<
T extends AnyComponentHarness,
> extends ComponentHarnessConstructor<T> {
isOpen(page: Page, baseUrl: string): Promise<boolean>;
}

export type PlaywrightScreen<T extends AnyComponentHarness> =
| PlaywrightScreenWithOpenFunction<T>
| PlaywrightScreenWithPath<T>;
| PlaywrightScreenWithPath<T>
| PlaywrightScreenWithIsOpenFunction<T>;

export interface PlaywrightScreenOpener {
<T extends AnyComponentHarness>(screen: PlaywrightScreen<T>): Promise<T>;
Expand Down

0 comments on commit 0f96f97

Please sign in to comment.