Skip to content

Commit

Permalink
test: add test to waitOnceForAllXhrFinished
Browse files Browse the repository at this point in the history
  • Loading branch information
jtassin committed Jun 14, 2020
1 parent 90414f1 commit fe6109c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 37 deletions.
7 changes: 2 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFilesAfterEnv: ['./jest.setup.ts'],
collectCoverageFrom: [
'src/*.{js,jsx}',
'!**/node_modules/**',
'!**/vendor/**',
],
testPathIgnorePatterns: ['node_modules/', '.history'],
collectCoverageFrom: ['src/*.{ts}', '!**/node_modules/**', '!**/vendor/**'],
};
18 changes: 15 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"devDependencies": {
"@types/jest": "26.0.0",
"@types/puppeteer": "3.0.0",
"@types/node": "^14.0.13",
"auto-changelog": "2.0.0",
"delay": "^4.3.0",
"eslint": "7.2.0",
"eslint-config-prettier": "6.11.0",
"eslint-plugin-jest": "23.13.2",
Expand Down
120 changes: 91 additions & 29 deletions src/__tests__/index.test.js → src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
const PendingXHR = require('../index').PendingXHR;
const http = require('http');
const puppeteer = require('puppeteer');
import {PendingXHR} from '../index';
import http from 'http';
import delay from 'delay';
import { launch, Browser } from 'puppeteer';

const port = 8907;
const xhrBackendPort = 8908;

function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleep(delay) {
await timeout(delay);
return;
}

const OK_NO_XHR = `
<html>
OK_NO_XHR
Expand Down Expand Up @@ -52,21 +46,21 @@ OK_WITH_1_SLOW_XHR
</html>
`;

let request1Resolver;
let request1Resolver: Function;
const request1Promise = () => {
return new Promise(resolve => {
return new Promise<[number, any]>(resolve => {
request1Resolver = resolve;
});
};

let request2Resolver;
let request2Resolver: Function;
const request2Promise = () => {
return new Promise(resolve => {
return new Promise<[number, any]>(resolve => {
request2Resolver = resolve;
});
};

const backendRequestHandler = async (request, response) => {
const backendRequestHandler = async (request: http.IncomingMessage, response: http.ServerResponse) => {
if (request.url === '/request1') {
const [statusCode, body] = await request1Promise();
response.statusCode = statusCode;
Expand All @@ -78,25 +72,25 @@ const backendRequestHandler = async (request, response) => {
}
};

let server;
let backendServer;
let browser;
let server: http.Server;
let backendServer: http.Server;
let browser: Browser;

describe('PendingXHR', () => {
beforeAll(async () => {
const args = [];
if (process.env.CI) {
args.push('--no-sandbox');
}
browser = await puppeteer.launch({
browser = await launch({
headless: true,
args,
});
});

beforeEach(async () => {
backendServer = http.createServer(backendRequestHandler);
await backendServer.listen(xhrBackendPort);
backendServer = await backendServer.listen(xhrBackendPort);
});

afterAll(async () => {
Expand All @@ -117,7 +111,7 @@ describe('PendingXHR', () => {
server.close(cb);
});

async function startServerReturning(html) {
async function startServerReturning(html: string) {
server = http.createServer((request, response) => {
response.statusCode = 200;
response.end(html);
Expand All @@ -142,18 +136,72 @@ describe('PendingXHR', () => {
expect(pendingXHR.pendingXhrCount()).toEqual(2);
setTimeout(() => {
request1Resolver([200, 'first xhr finished']);
});
await sleep(1000);
}, 0);
await delay(1000);
expect(pendingXHR.pendingXhrCount()).toEqual(1);
setTimeout(() => {
request2Resolver([200, 'second xhr finished']);
});
await sleep(1000);
}, 0);
await delay(1000);
expect(pendingXHR.pendingXhrCount()).toEqual(0);
});
});

describe('waitOnceForAllXhrFinished', () => {

it('returns and removes all listeners immediatly if no xhr pending', async () => {
await startServerReturning(OK_NO_XHR);
const page = await browser.newPage();
const count = page.listenerCount('request');
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/go`);
await pendingXHR.waitOnceForAllXhrFinished();
expect(page.listenerCount('request')).toBe(count)
});

describe.skip('one XHR', () => {
it('resolves removes all listeners once finished', async () => {
await startServerReturning(OK_WITH_1_XHR);
const page = await browser.newPage();
const count = page.listenerCount('request');
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/go`);
expect(pendingXHR.pendingXhrCount()).toEqual(1);
await pendingXHR.waitOnceForAllXhrFinished();
expect(pendingXHR.pendingXhrCount()).toEqual(0);
expect(page.listenerCount('request')).toBe(count)
})

})

describe.skip('several XHR', () => {
it('resolves removes all listeners once finished', async () => {
await startServerReturning(OK_WITH_2_XHR);
const page = await browser.newPage();
const count = page.listenerCount('request');
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/go`);
expect(pendingXHR.pendingXhrCount()).toEqual(2);
await pendingXHR.waitOnceForAllXhrFinished();
expect(pendingXHR.pendingXhrCount()).toEqual(0);
expect(page.listenerCount('request')).toBe(count)
})
})


})

describe('waitForAllXhrFinished', () => {

it('let a listenner connected', async () => {
await startServerReturning(OK_NO_XHR);
const page = await browser.newPage();
const count = page.listenerCount('request');
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/go`);
await pendingXHR.waitForAllXhrFinished();
expect(page.listenerCount('request')).toBe(count+1)
})
it('returns immediatly if no xhr pending count', async () => {
await startServerReturning(OK_NO_XHR);
const page = await browser.newPage();
Expand All @@ -170,11 +218,25 @@ describe('PendingXHR', () => {
expect(pendingXHR.pendingXhrCount()).toEqual(1);
setTimeout(() => {
request1Resolver([200, '']);
});
}, 0);
await pendingXHR.waitForAllXhrFinished();
expect(pendingXHR.pendingXhrCount()).toEqual(0);
});

it('can be trigerred multiple times', async () => {
await startServerReturning(OK_WITH_1_XHR);
const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://localhost:${port}/go`);
expect(pendingXHR.pendingXhrCount()).toEqual(1);
setTimeout(() => {
request1Resolver([200, '']);
}, 0);
await pendingXHR.waitForAllXhrFinished();
expect(pendingXHR.pendingXhrCount()).toEqual(0);
await pendingXHR.waitForAllXhrFinished();
});

it('works with Promise.race', async () => {
await startServerReturning(OK_WITH_1_XHR);
const page = await browser.newPage();
Expand Down Expand Up @@ -208,7 +270,7 @@ describe('PendingXHR', () => {
expect(pendingXHR.pendingXhrCount()).toEqual(2);
setTimeout(() => {
request1Resolver([200, 'first xhr finished']);
});
}, 0);
setTimeout(() => {
request2Resolver([200, 'second xhr finished']);
}, 200);
Expand All @@ -224,7 +286,7 @@ describe('PendingXHR', () => {
expect(pendingXHR.pendingXhrCount()).toEqual(1);
setTimeout(() => {
request1Resolver([500, 'boom']);
});
}, 0);
await pendingXHR.waitForAllXhrFinished();
expect(pendingXHR.pendingXhrCount()).toEqual(0);
});
Expand Down

0 comments on commit fe6109c

Please sign in to comment.