chore: update types for JSHandle (#7650)

Closes #7583
This commit is contained in:
Jan Scheffler 2021-10-07 18:04:08 +02:00 committed by GitHub
parent b871d5dbb6
commit 2d37430838
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -191,7 +191,7 @@ export class JSHandle<HandleObjectType = unknown> {
/** Fetches a single property from the referenced object.
*/
async getProperty(propertyName: string): Promise<JSHandle | undefined> {
async getProperty(propertyName: string): Promise<JSHandle> {
const objectHandle = await this.evaluateHandle(
(object: Element, propertyName: string) => {
const result = { __proto__: null };
@ -201,7 +201,8 @@ export class JSHandle<HandleObjectType = unknown> {
propertyName
);
const properties = await objectHandle.getProperties();
const result = properties.get(propertyName) || null;
const result = properties.get(propertyName);
assert(result instanceof JSHandle);
await objectHandle.dispose();
return result;
}

View File

@ -15,6 +15,7 @@
*/
import expect from 'expect';
import { JSHandle } from '../lib/cjs/puppeteer/common/JSHandle.js';
import {
getTestState,
setupTestBrowserHooks,
@ -96,6 +97,19 @@ describe('JSHandle', function () {
const twoHandle = await aHandle.getProperty('two');
expect(await twoHandle.jsonValue()).toEqual(2);
});
it('should return a JSHandle even if the property does not exist', async () => {
const { page } = getTestState();
const aHandle = await page.evaluateHandle(() => ({
one: 1,
two: 2,
three: 3,
}));
const undefinedHandle = await aHandle.getProperty('doesnotexist');
expect(undefinedHandle).toBeInstanceOf(JSHandle);
expect(await undefinedHandle.jsonValue()).toBe(undefined);
});
});
describe('JSHandle.jsonValue', function () {