Skip to content

Commit bfc9daf

Browse files
committed
fix: review fixes
1 parent 1a4049c commit bfc9daf

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/decorators/rootSelectors.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ type PageFirstArgs<TClass extends AnyConstructor> =
1212
? [page: Page, ...rest: TRest]
1313
: never;
1414

15+
function isLocatorLike(
16+
value: object,
17+
): value is Pick<Locator, "locator" | "page"> {
18+
return (
19+
"locator" in value &&
20+
typeof (value as { locator?: unknown }).locator === "function" &&
21+
"page" in value &&
22+
typeof (value as { page?: unknown }).page === "function"
23+
);
24+
}
25+
1526
function resolvePage(value: unknown, className: string): Page {
1627
if (
1728
typeof value === "object" &&
1829
value !== null &&
1930
"locator" in value &&
20-
typeof (value as { locator?: unknown }).locator === "function"
31+
typeof (value as { locator?: unknown }).locator === "function" &&
32+
!isLocatorLike(value)
2133
) {
2234
return value as Page;
2335
}

src/tests/decorators/rootSelectors.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ describe("RootSelectorBy (via exported wrappers)", () => {
7272
);
7373
});
7474

75+
it("rejects Locator values passed as the first constructor arg", () => {
76+
@RootSelector()
77+
class ExternalRootPage {
78+
constructor(readonly page: Page) {}
79+
}
80+
81+
const locator = createMockLocator(mockPage);
82+
83+
expect(() => new ExternalRootPage(locator as unknown as Page)).toThrow(
84+
/ExternalRootPage.*must receive Playwright Page as the first constructor argument/,
85+
);
86+
expect(locator.locator).not.toHaveBeenCalledWith("body");
87+
});
88+
7589
it("RootSelector(id) uses p.getByTestId(id)", () => {
7690
@RootSelector("myId")
7791
class TestPage extends RootPageObject {}

0 commit comments

Comments
 (0)