Skip to content

Commit 5d2955a

Browse files
committed
chore: Add test suite for multi-tab behaviour
1 parent 39b9f96 commit 5d2955a

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"ArrayBuffer": "readonly",
2222
"FileReaderSync": "readonly",
2323
"emit": "readonly",
24-
"PouchDB": "readonly"
24+
"PouchDB": "readonly",
25+
"__pouch__": "readonly"
2526
},
2627

2728
"rules": {

tests/multitab/concurrency.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const { assert } = require('chai');
4+
const UserAgent = require('./user_agent');
5+
6+
describe('multi-tab concurrency', () => {
7+
let agent, res;
8+
9+
beforeEach(async () => {
10+
agent = await UserAgent.start();
11+
});
12+
13+
afterEach(async () => {
14+
await agent.stop();
15+
});
16+
17+
async function checkInfo({ doc_count }) {
18+
let info1 = await agent.eval(1, () => __pouch__.info());
19+
assert.equal(info1.doc_count, doc_count);
20+
21+
let info2 = await agent.eval(2, () => __pouch__.info());
22+
assert.deepEqual(info1, info2);
23+
}
24+
25+
it('creates docs concurrently in two tabs', async () => {
26+
res = await agent.eval(1, () => __pouch__.put({ _id: 'doc-1' }));
27+
assert(res.ok);
28+
await checkInfo({ doc_count: 1 });
29+
30+
res = await agent.eval(2, () => __pouch__.put({ _id: 'doc-2' }));
31+
assert(res.ok);
32+
await checkInfo({ doc_count: 2 });
33+
34+
res = await agent.eval(1, () => __pouch__.put({ _id: 'doc-3' }));
35+
assert(res.ok);
36+
await checkInfo({ doc_count: 3 }); // fails on indexeddb; pages have different info
37+
38+
res = await agent.eval(2, () => __pouch__.put({ _id: 'doc-4' }));
39+
assert(res.ok); // fails on indexeddb
40+
await checkInfo({ doc_count: 4 });
41+
});
42+
});

tests/multitab/shell.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Playwright shell</title>
5+
<script src="/packages/node_modules/pouchdb/dist/pouchdb.min.js"></script>
6+
<script src="/packages/node_modules/pouchdb/dist/pouchdb.indexeddb.min.js"></script>
7+
</head>
8+
<body></body>
9+
</html>

tests/multitab/user_agent.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const playwright = require('playwright');
4+
5+
const ADAPTERS = process.env.ADAPTERS || 'indexeddb';
6+
const CLIENT = process.env.CLIENT || 'firefox';
7+
const SHELL_URL = 'http://127.0.0.1:8000/tests/multitab/shell.html';
8+
9+
class UserAgent {
10+
static async start() {
11+
let browser = await playwright[CLIENT].launch();
12+
let context = await browser.newContext();
13+
return new UserAgent(ADAPTERS, browser, context);
14+
}
15+
16+
constructor(adapter, browser, context) {
17+
this._adapter = adapter;
18+
this._browser = browser;
19+
this._context = context;
20+
this._pages = new Map();
21+
}
22+
23+
async stop() {
24+
await this._browser.close();
25+
}
26+
27+
async eval(pageId, fn) {
28+
let page = await this._getPage(pageId);
29+
return page.evaluate(fn);
30+
}
31+
32+
_getPage(id) {
33+
if (!this._pages.has(id)) {
34+
this._pages.set(id, this._setupPage());
35+
}
36+
return this._pages.get(id);
37+
}
38+
39+
async _setupPage() {
40+
let page = await this._context.newPage();
41+
await page.goto(SHELL_URL);
42+
43+
if (this._adapter === 'idb') {
44+
await page.evaluate(() => window.__pouch__ = new PouchDB('testdb', { adapter: 'idb' }));
45+
} else if (this._adapter === 'indexeddb') {
46+
await page.evaluate(() => window.__pouch__ = new PouchDB('testdb', { adapter: 'indexeddb' }));
47+
}
48+
49+
return page;
50+
}
51+
}
52+
53+
module.exports = UserAgent;

0 commit comments

Comments
 (0)