Skip to content

Commit ef582c8

Browse files
committed
Repalccing zombie with JSDOM
1 parent 0e42127 commit ef582c8

8 files changed

Lines changed: 410 additions & 911 deletions

File tree

components/pryv/test/Browser.AuthController.test.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @license
33
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
44
*/
5-
/* global describe, it, before, after, expect, testData, Browser */
5+
/* global describe, it, before, after, expect, testData, JSDOM */
66
/* eslint-disable no-unused-expressions */
77

88
const utils = require('../src/utils.js');
@@ -13,20 +13,21 @@ describe('Browser.LoginButton', function () {
1313
this.timeout(15000);
1414

1515
let auth;
16-
let removeZombie = false;
16+
let cleanupDom = false;
1717
before(async function () {
1818
if (typeof document !== 'undefined') return; // in browser
19-
removeZombie = true;
20-
const browser = new Browser();
21-
browser.visit('./?pryvServiceInfoUrl=https://zou.zou/service/info');
22-
global.document = browser.document;
23-
global.window = browser.window;
24-
global.location = browser.location;
19+
cleanupDom = true;
20+
const dom = new JSDOM('<!DOCTYPE html>', {
21+
url: 'http://localhost/?pryvServiceInfoUrl=https://zou.zou/service/info'
22+
});
23+
global.document = dom.window.document;
24+
global.window = dom.window;
25+
global.location = dom.window.location;
2526
global.navigator = { userAgent: 'Safari' };
2627
});
2728

2829
after(async function () {
29-
if (!removeZombie) return; // in browser
30+
if (!cleanupDom) return; // in browser
3031
delete global.document;
3132
delete global.window;
3233
delete global.location;

components/pryv/test/Browser.test.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @license
33
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
44
*/
5-
/* global describe, it, before, after, expect, Browser, pryv, testData */
5+
/* global describe, it, before, after, expect, JSDOM, pryv, testData */
66
/* eslint-disable no-unused-expressions */
77

88
function genSettings () {
@@ -26,21 +26,22 @@ describe('Browser', function () {
2626
await testData.prepare();
2727
});
2828

29-
let removeZombie = false;
29+
let cleanupDom = false;
3030

3131
before(async () => {
3232
if (typeof document !== 'undefined') return; // in browser
33-
removeZombie = true;
34-
const browser = new Browser();
35-
browser.visit('./?pryvServiceInfoUrl=https://zou.zou/service/info');
36-
global.document = browser.document;
37-
global.window = browser.window;
38-
global.location = browser.location;
33+
cleanupDom = true;
34+
const dom = new JSDOM('<!DOCTYPE html>', {
35+
url: 'http://localhost/?pryvServiceInfoUrl=https://zou.zou/service/info'
36+
});
37+
global.document = dom.window.document;
38+
global.window = dom.window;
39+
global.location = dom.window.location;
3940
global.navigator = { userAgent: 'Safari' };
4041
});
4142

4243
after(async () => {
43-
if (!removeZombie) return; // in browser
44+
if (!cleanupDom) return; // in browser
4445
delete global.document;
4546
delete global.window;
4647
delete global.location;

components/pryv/test/Connection.test.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @license
33
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
44
*/
5-
/* global describe, it, before, after, beforeEach, afterEach, expect, Browser, pryv, Blob, FormData */
5+
/* global describe, it, before, after, beforeEach, afterEach, expect, JSDOM, pryv, Blob, FormData */
66
/* eslint-disable no-unused-expressions */
77

88
// URL and URLSearchParams are native in Node.js and browsers
@@ -382,35 +382,19 @@ describe('Connection', () => {
382382

383383
if (typeof window === 'undefined') {
384384
describe('Browser mock', function () {
385-
const isNotAvailable = {
386-
URL: global.URL == null,
387-
URLSearchParams: global.URLSearchParams == null,
388-
fetch: global.fetch == null
389-
};
390385
beforeEach(function () {
391-
const browser = new Browser();
392-
browser.visit('./');
393-
global.document = browser.document;
394-
global.window = browser.window;
395-
global.location = browser.location;
396-
function fetch (...args) {
397-
return browser.fetch(...args);
398-
}
399-
if (isNotAvailable.fetch) global.fetch = fetch;
400-
if (isNotAvailable.URL) global.URL = URL;
401-
if (isNotAvailable.URLSearchParams) global.URLSearchParams = URLSearchParams;
386+
const dom = new JSDOM('<!DOCTYPE html>', { url: 'http://localhost/' });
387+
global.document = dom.window.document;
388+
global.window = dom.window;
389+
global.location = dom.window.location;
402390
});
403391

404392
afterEach(function () {
405393
delete global.document;
406394
delete global.window;
407395
delete global.location;
408-
if (isNotAvailable.fetch) delete global.fetch;
409-
if (isNotAvailable.URL) delete global.URL;
410-
if (isNotAvailable.URLSearchParams) delete global.URLSearchParams;
411396
});
412397

413-
// HACK: skip until a solution is found to Zombie's `fetch()` not accepting URLs
414398
it(' with fetch', async () => {
415399
const queryParams = { fromTime: 0, toTime: now, limit: 10000 };
416400
let eventsCount = 0;

components/pryv/test/ServiceAssets.test.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* @license
33
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
44
*/
5-
/* global describe, it, before, after, expect, Browser, pryv, testData */
5+
/* global describe, it, before, after, expect, JSDOM, pryv, testData */
66

77
describe('ServiceAssets', function () {
8-
let removeZombie = false;
8+
let cleanupDom = false;
99

1010
before(async function () {
1111
this.timeout(15000);
@@ -14,16 +14,15 @@ describe('ServiceAssets', function () {
1414

1515
before(async () => {
1616
if (typeof document !== 'undefined') return; // in browser
17-
removeZombie = true;
18-
const browser = new Browser();
19-
browser.visit('./');
20-
global.document = browser.document;
21-
global.window = browser.window;
22-
global.location = browser.location;
17+
cleanupDom = true;
18+
const dom = new JSDOM('<!DOCTYPE html>', { url: 'http://localhost/' });
19+
global.document = dom.window.document;
20+
global.window = dom.window;
21+
global.location = dom.window.location;
2322
});
2423

2524
after(async () => {
26-
if (!removeZombie) return; // in browser
25+
if (!cleanupDom) return; // in browser
2726
delete global.document;
2827
delete global.window;
2928
delete global.location;

0 commit comments

Comments
 (0)