Skip to content

Commit

Permalink
prettier #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien TASSIN committed May 9, 2018
1 parent 7816701 commit 663f353
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
"private": false,
"devDependencies": {
"eslint": "^4.19.1",
"eslint-config-prettier": "^2.9.0",
"husky": "^0.14.3",
"jest": "^22.4.3",
"lint-staged": "^7.1.0",
"prettier": "1.12.1",
"puppeteer": "^1.3.0"
},
"scripts": {
"lint": "eslint",
"prettier:write":
"prettier --single-quote --trailing-comma es5 --write src/**/*",
"lint": "eslint src",
"test": "jest",
"precommit": "lint-staged"
},
Expand Down
42 changes: 20 additions & 22 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const PendingXHR = require('../index').PendingXHR;
const http = require('http');
const puppeteer = require('puppeteer');
const PendingXHR = require("../index").PendingXHR;
const http = require("http");
const puppeteer = require("puppeteer");
const port = 8907;
const xhrBackendPort = 8908;
const util = require('util');
const util = require("util");
// const sleep = util.promisify(setTimeout);

function timeout(ms) {
Expand All @@ -14,7 +14,6 @@ async function sleep(delay) {
return;
}


const OK_NO_XHR = `
<html>
OK_NO_XHR
Expand Down Expand Up @@ -66,13 +65,13 @@ setTimeout(() => {
`;

const requestHandler = (request, response) => {
if (request.url === '/with_infinite_xhr') {
if (request.url === "/with_infinite_xhr") {
response.statusCode = 200;
response.end(OK_WITH_1_SLOW_XHR);
} else if (request.url === '/with_xhr') {
} else if (request.url === "/with_xhr") {
response.statusCode = 200;
response.end(OK_WITH_1_XHR);
} else if (request.url === '/with_xhr_failing') {
} else if (request.url === "/with_xhr_failing") {
response.statusCode = 200;
response.end(OK_WITH_1_XHR);
} else {
Expand All @@ -82,15 +81,15 @@ const requestHandler = (request, response) => {
};

const backendRequestHandler = (request, response) => {
if (request.url === '/infinite') {
if (request.url === "/infinite") {
// This xhr will never end
} else if (request.url === '/fail') {
} else if (request.url === "/fail") {
response.statusCode = 500;
response.end('boom');
response.end("boom");
} else {
response.end();
}
}
};

let server;
let backendServer;
Expand All @@ -115,17 +114,16 @@ afterEach(async () => {
await backendServer.close();
});

describe('PendingXHR', () => {

describe('pendingXhrCount', () => {
it('returns 0 if no xhr pending count', async () => {
describe("PendingXHR", () => {
describe("pendingXhrCount", () => {
it("returns 0 if no xhr pending count", async () => {
const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/no_xhr`);
expect(pendingXHR.pendingXhrCount()).toEqual(0);
});

it('returns the xhr pending count', async () => {
it("returns the xhr pending count", async () => {
const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/with_infinite_xhr`);
Expand All @@ -134,15 +132,15 @@ describe('PendingXHR', () => {
});
});

describe('waitForAllXhrFinished', async () => {
it('returns immediatly if no xhr pending count', async () => {
describe("waitForAllXhrFinished", async () => {
it("returns immediatly if no xhr pending count", async () => {
const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/no_xhr`);
await pendingXHR.waitForAllXhrFinished();
});

it('waits for all xhr to end', async () => {
it("waits for all xhr to end", async () => {
const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/with_xhr`);
Expand All @@ -151,7 +149,7 @@ describe('PendingXHR', () => {
expect(pendingXHR.pendingXhrCount()).toEqual(0);
});

it('handle correctly failed xhr', async () => {
it("handle correctly failed xhr", async () => {
const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/with_xhr_failing`);
Expand All @@ -160,4 +158,4 @@ describe('PendingXHR', () => {
expect(pendingXHR.pendingXhrCount()).toEqual(0);
});
});
});
});
39 changes: 25 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
class PendingXHR {

constructor(page) {
this.page = page;
this.resourceType = 'xhr';
this.resourceType = "xhr";
// page.setRequestInterception(true);
this.pendingXhrs = new Set();
this.finishedWithSuccessXhrs = new Set();
this.finishedWithErrorsXhrs = new Set();
page.on('request', request => {
console.log(Date.now(), 'new request', request.resourceType(), ' to ', request.url());
page.on("request", request => {
console.log(
Date.now(),
"new request",
request.resourceType(),
" to ",
request.url()
);
if (request.resourceType() === this.resourceType) {
this.pendingXhrs.add(request);
if(!this.promise) {
if (!this.promise) {
this.promise = new Promise((resolve, reject) => {
this.resolver = resolve;
});
this.promise.then(() => {
console.log('All xhr are ended');
console.log("All xhr are ended");
});
}
}
}
});
page.on('requestfailed', request => {
page.on("requestfailed", request => {
if (request.resourceType() === this.resourceType) {
console.log(this.pendingXhrs);
this.pendingXhrs.delete(request);
this.finishedWithErrorsXhrs.add(request);
if(this.resolver) {
if (this.resolver) {
this.resolver();
delete this.promise;
delete this.resolver;
}
}
});
page.on('requestfinished', request => {
console.log(Date.now(), 'request finished', request.resourceType(), ' to ', request.url());
page.on("requestfinished", request => {
console.log(
Date.now(),
"request finished",
request.resourceType(),
" to ",
request.url()
);
if (request.resourceType() === this.resourceType) {
this.pendingXhrs.delete(request);
this.finishedWithSuccessXhrs.add(request);
if(this.resolver) {
if (this.resolver) {
this.resolver();
delete this.promise;
delete this.resolver;
Expand All @@ -51,8 +62,8 @@ class PendingXHR {
if (this.pendingXhrCount() === 0) {
return true;
}
if(this.promise) {
console.log('promise exists', this.promise);
if (this.promise) {
console.log("promise exists", this.promise);
await this.promise;
}
}
Expand Down

0 comments on commit 663f353

Please sign in to comment.