Skip to content

Commit

Permalink
fix(types): add types for waitForEvent (#1601)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder committed Mar 31, 2020
1 parent 13a6c89 commit 08aebc7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 11 additions & 0 deletions utils/generate_types/index.js
Expand Up @@ -141,6 +141,7 @@ function createEventDescriptions(classDesc) {
const argName = argNameForType(type);
const params = argName ? `${argName} : ${type}` : '';
descriptions.push({
type,
params,
eventName,
comment: value.comment
Expand All @@ -167,6 +168,16 @@ function classBody(classDesc) {
parts.push(members.map(member => {
if (member.kind === 'event')
return '';
if (member.name === 'waitForEvent') {
const parts = [];
for (const {eventName, params, comment, type} of eventDescriptions) {
if (comment)
parts.push(writeComment(comment, ' '));
parts.push(` ${member.name}(event: '${eventName}', optionsOrPredicate?: { predicate?: (${params}) => boolean, timeout?: number }): Promise<${type}>;\n`);
}

return parts.join('\n');
}
const jsdoc = memberJSDOC(member, ' ');
const args = argsFromMember(member, classDesc.name);
const type = typeToString(member.type, classDesc.name, member.name);
Expand Down
26 changes: 25 additions & 1 deletion utils/generate_types/test/test.ts
Expand Up @@ -290,6 +290,31 @@ playwright.chromium.launch().then(async browser => {
browser.close();
})();

// waitForEvent
(async () => {
const browser = await playwright.webkit.launch();
const page = await browser.newPage();
{
const frame = await page.waitForEvent('frameattached');
const assertion: AssertType<playwright.Frame, typeof frame> = true;
}
{
const worker = await page.waitForEvent('worker', {
predicate: worker => {
const condition: AssertType<playwright.Worker, typeof worker> = true;
return true;
}
});
const assertion: AssertType<playwright.Worker, typeof worker> = true;
}
{
const newPage = await page.context().waitForEvent('page', {
timeout: 500
});
const assertion: AssertType<playwright.Page, typeof newPage> = true;
}
})();

// typed handles
(async () => {
const browser = await playwright.webkit.launch();
Expand Down Expand Up @@ -515,7 +540,6 @@ playwright.chromium.launch().then(async browser => {
}
}


await browser.close();
})();

Expand Down

0 comments on commit 08aebc7

Please sign in to comment.