Skip to content

Commit

Permalink
Merge pull request #56 from grafana/async/js-handle
Browse files Browse the repository at this point in the history
Update JSHandle to return Promises
  • Loading branch information
allansson authored Jun 18, 2024
2 parents ec7918a + 70ad1ef commit b5ef679
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
12 changes: 6 additions & 6 deletions types/k6/experimental/browser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1791,12 +1791,12 @@ export interface JSHandle<T = any> {
* an instance of `ElementHandle`.
* @returns The ElementHandle if available.
*/
asElement(): ElementHandle | null;
asElement(): Promise<ElementHandle | null>;

/**
* Stops referencing the element handle.
*/
dispose(): void;
dispose(): Promise<void>;

/**
* Evaluates the page function and returns its return value.
Expand All @@ -1805,7 +1805,7 @@ export interface JSHandle<T = any> {
* @param args The arguments to pass to the page function.
* @returns The return value of `pageFunction`.
*/
evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg?: Arg): R;
evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg?: Arg): Promise<R>;

/**
* Evaluates the page function and returns a `JSHandle`.
Expand All @@ -1815,20 +1815,20 @@ export interface JSHandle<T = any> {
* @param args The arguments to pass to the page function.
* @returns A JSHandle of the return value of `pageFunction`.
*/
evaluateHandle<R, Arg>(pageFunction: PageFunction<Arg, R>, arg?: Arg): JSHandle<R>;
evaluateHandle<R, Arg>(pageFunction: PageFunction<Arg, R>, arg?: Arg): Promise<JSHandle<R>>;

/**
* Fetches a map with own property names of of the `JSHandle` with their values as
* `JSHandle` instances.
* @returns A map with property names as keys and `JSHandle` instances for the property values.
*/
getProperties(): Map<string, JSHandle>;
getProperties(): Promise<Map<string, JSHandle>>;

/**
* Fetches a JSON representation of the object.
* @returns A JSON representation of the object.
*/
jsonValue(): any;
jsonValue(): Promise<any>;
}

/**
Expand Down
28 changes: 14 additions & 14 deletions types/k6/test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,54 +1150,54 @@ async function test() {
//
const jsHandle = await page.evaluateHandle(() => null);

// $ExpectType ElementHandle | null
// $ExpectType Promise<ElementHandle | null>
jsHandle.asElement();

// $ExpectType void
// $ExpectType Promise<void>
jsHandle.dispose();

// @ts-expect-error
jsHandle.evaluate();
// @ts-expect-error
jsHandle.evaluate(1);
// @ExpectType void
// @ExpectType Promise<void>
jsHandle.evaluate("");
// @ExpectType void
// @ExpectType Promise<void>
jsHandle.evaluate(() => {});
// @ExpectType string
// @ExpectType Promise<string>
jsHandle.evaluate(() => {
"";
});
// @ExpectType string
// @ExpectType Promise<string>
jsHandle.evaluate((a: string) => {
a;
}, "");
// @ExpectType string[]
// @ExpectType Promise<string[]>
jsHandle.evaluate((a: string[]) => a, [""]);

// @ts-expect-error
jsHandle.evaluateHandle();
// @ts-expect-error
jsHandle.evaluateHandle(1);
// @ExpectType JSHandle
// @ExpectType Promise<JSHandle>
jsHandle.evaluateHandle("");
// @ExpectType JSHandle
// @ExpectType Promise<JSHandle>
jsHandle.evaluateHandle(() => {});
// @ExpectType JSHandle
// @ExpectType Promise<JSHandle>
jsHandle.evaluateHandle(() => {
"";
});
// @ExpectType JSHandle
// @ExpectType Promise<JSHandle>
jsHandle.evaluateHandle((a: string) => {
a;
}, "");
// @ExpectType JSHandle
// @ExpectType Promise<JSHandle>
jsHandle.evaluateHandle((a: string[]) => a, [""]);

// $ExpectType Map<string, JSHandle<any>>
// $ExpectType Promise<Map<string, JSHandle<any>>>
jsHandle.getProperties();

// $ExpectType any
// $ExpectType Promise<any>
jsHandle.jsonValue();

//
Expand Down

0 comments on commit b5ef679

Please sign in to comment.