Skip to content

Commit

Permalink
#3 Replace waitForResource internals with native API, add WIP test fo…
Browse files Browse the repository at this point in the history
…r Extensions
  • Loading branch information
gidztech committed Jan 17, 2019
1 parent df62153 commit eda021a
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 81 deletions.
58 changes: 29 additions & 29 deletions docs/classes/extensions.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/classes/hackydate.html
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ <h3>constructor</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/12f80ed/src/HackyDate.ts#L2">HackyDate.ts:2</a></li>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/df62153/src/HackyDate.ts#L2">HackyDate.ts:2</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -1006,7 +1006,7 @@ <h3>date</h3>
<div class="tsd-signature tsd-kind-icon">date<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">Date</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/12f80ed/src/HackyDate.ts#L2">HackyDate.ts:2</a></li>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/df62153/src/HackyDate.ts#L2">HackyDate.ts:2</a></li>
</ul>
</aside>
</section>
Expand All @@ -1023,7 +1023,7 @@ <h3>now</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/12f80ed/src/HackyDate.ts#L10">HackyDate.ts:10</a></li>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/df62153/src/HackyDate.ts#L10">HackyDate.ts:10</a></li>
</ul>
</aside>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">number</span></h4>
Expand Down
6 changes: 3 additions & 3 deletions docs/globals.html
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> DEFAULT_<wbr>TIMEOUT_<wbr>M
<div class="tsd-signature tsd-kind-icon">DEFAULT_<wbr>TIMEOUT_<wbr>MS<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol"> =&nbsp;5000</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/12f80ed/src/index.ts#L4">index.ts:4</a></li>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/df62153/src/index.ts#L4">index.ts:4</a></li>
</ul>
</aside>
</section>
Expand All @@ -998,7 +998,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> <span class="tsd-flag ts-fl
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/12f80ed/src/index.ts#L11">index.ts:11</a></li>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/df62153/src/index.ts#L11">index.ts:11</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1033,7 +1033,7 @@ <h3>serialize<wbr>Function<wbr>With<wbr>Args</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/12f80ed/src/external/Serialization.ts#L90">external/Serialization.ts:90</a></li>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/df62153/src/external/Serialization.ts#L90">external/Serialization.ts:90</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down
6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> DEFAULT_<wbr>TIMEOUT_<wbr>M
<div class="tsd-signature tsd-kind-icon">DEFAULT_<wbr>TIMEOUT_<wbr>MS<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span><span class="tsd-signature-symbol"> =&nbsp;5000</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/12f80ed/src/index.ts#L4">index.ts:4</a></li>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/df62153/src/index.ts#L4">index.ts:4</a></li>
</ul>
</aside>
</section>
Expand All @@ -999,7 +999,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> <span class="tsd-flag ts-fl
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/12f80ed/src/index.ts#L11">index.ts:11</a></li>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/df62153/src/index.ts#L11">index.ts:11</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1034,7 +1034,7 @@ <h3>serialize<wbr>Function<wbr>With<wbr>Args</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/12f80ed/src/external/Serialization.ts#L90">external/Serialization.ts:90</a></li>
<li>Defined in <a href="https://github.com/HuddleEng/puppeteer-extensions/blob/df62153/src/external/Serialization.ts#L90">external/Serialization.ts:90</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down
47 changes: 47 additions & 0 deletions src/Extensions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as puppeteer from 'puppeteer';
import Extensions from './Extensions';

jest.setTimeout(10000);

describe('Extensions', () => {
describe('waitForResource', () => {
let browser;

beforeAll(async () => {
browser = await puppeteer.launch();
});

it('should respond with true if resource response is found', async () => {
const page = await browser.newPage();

const extensions = new Extensions(page, 10000);
const response = extensions.waitForResource('main');

await page.goto('https://www.gideonpyzer.com');

await response;
expect(response).toBeTruthy();
});

it('should timeout when waiting for resource that will not be requested for', async () => {
const page = await browser.newPage();

const extensions = new Extensions(page, 10000);
const response = extensions.waitForResource('bla', 500);

await page.goto('https://www.gideonpyzer.com');

try {
await response;
} catch (e) {
expect(e.message).toBe(
'Timeout exceeded while waiting for event'
);
}
});

afterAll(async () => {
browser.close();
});
});
});
33 changes: 6 additions & 27 deletions src/Extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,12 @@ export default class Extensions {
resource: string,
timeout: number = this.defaultTimeout
) {
return new Promise((resolve, reject) => {
const resourceRequestHasResponded = (): boolean => {
const resourceRequest = this.resourceRequests.find(
r => r.url().indexOf(resource) !== -1
);
if (resourceRequest) {
return isSuccessfulResponse(resourceRequest);
}

return false;
};

if (resourceRequestHasResponded()) {
resolve();
} else {
pollFor({
checkFn: async () => {
return resourceRequestHasResponded();
},
interval: 100,
timeout,
timeoutMsg: 'Timeout waiting for resource match.'
})
.then(resolve)
.catch(reject);
}
});
return this.puppeteerPage.waitForResponse(
response =>
response.url().indexOf(resource) > -1 &&
isSuccessfulResponse(response),
{ timeout }
);
}

/**
Expand Down
22 changes: 9 additions & 13 deletions src/Utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { Request } from 'puppeteer';
import { Response } from 'puppeteer';
import { isSuccessfulResponse, pollFor } from './Utils';

describe('Utils', () => {
describe('isSuccessfulResponse', () => {
const mockRequestWithStatus = (status: number) => {
return jest.fn<Request>(() => ({
response: jest.fn(() => {
return {
status: () => status
};
})
return jest.fn<Response>(() => ({
status: () => status
}));
};

it('correctly determines successful response', () => {
let MockRequest = mockRequestWithStatus(200);
expect(isSuccessfulResponse(new MockRequest())).toBe(true);
MockRequest = mockRequestWithStatus(304);
expect(isSuccessfulResponse(new MockRequest())).toBe(true);
let MockResponse = mockRequestWithStatus(200);
expect(isSuccessfulResponse(new MockResponse())).toBe(true);
MockResponse = mockRequestWithStatus(304);
expect(isSuccessfulResponse(new MockResponse())).toBe(true);
});
it('correctly determines unsuccessful response', () => {
const MockRequest = mockRequestWithStatus(500);
expect(isSuccessfulResponse(new MockRequest())).toBe(false);
const MockResponse = mockRequestWithStatus(500);
expect(isSuccessfulResponse(new MockResponse())).toBe(false);
});
});

Expand Down
4 changes: 1 addition & 3 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { Request, Response } from 'puppeteer';
/**
* @hidden
*/
const isSuccessfulResponse = (request: Request): boolean => {
const response: Response | null = request.response();

const isSuccessfulResponse = (response: Response): boolean => {
if (response) {
return response.status() === 200 || response.status() === 304;
}
Expand Down

0 comments on commit eda021a

Please sign in to comment.