Skip to content

Commit 90b5a5d

Browse files
committed
refactor: use async/await, const and arrow function
1 parent fe20426 commit 90b5a5d

1 file changed

Lines changed: 70 additions & 61 deletions

File tree

tests/integration/test.events.js

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,126 @@
11
'use strict';
22

3-
var adapters = ['local', 'http'];
3+
const adapters = ['local', 'http'];
44

5-
adapters.forEach(function (adapter) {
6-
describe('test.events.js-' + adapter, function () {
5+
adapters.forEach((adapter) => {
6+
describe(`test.events.js-${adapter}`, () => {
77

8-
var dbs = {};
8+
const dbs = {};
99

10-
beforeEach(function () {
10+
beforeEach(() => {
1111
dbs.name = testUtils.adapterUrl(adapter, 'testdb');
1212
});
1313

14-
afterEach(function (done) {
15-
testUtils.cleanup([dbs.name], done);
14+
afterEach(async () => {
15+
await new Promise((resolve, reject) => testUtils.cleanup([dbs.name], (err) => {
16+
return err ? reject(err) : resolve();
17+
}));
1618
});
1719

18-
it('PouchDB emits creation event', function (done) {
19-
PouchDB.once('created', function (name) {
20-
name.should.equal(dbs.name, 'should be same thing');
21-
done();
20+
it('PouchDB emits creation event', async () => {
21+
const eventPromise = new Promise((resolve) => {
22+
PouchDB.once('created', (name) => {
23+
name.should.equal(dbs.name, 'should be same thing');
24+
resolve();
25+
});
2226
});
2327
new PouchDB(dbs.name);
28+
await eventPromise;
2429
});
2530

26-
it('PouchDB emits destruction event', function (done) {
27-
var db = new PouchDB(dbs.name);
28-
db.once('destroyed', done);
31+
it('PouchDB emits destruction event', async () => {
32+
const db = new PouchDB(dbs.name);
33+
const destroyedPromise = new Promise((resolve) => db.once('destroyed', resolve));
2934
db.destroy();
35+
await destroyedPromise;
3036
});
3137

32-
it('PouchDB emits destruction event on PouchDB object', function (done) {
33-
PouchDB.once('destroyed', function (name) {
34-
name.should.equal(dbs.name, 'should have the same name');
35-
done();
38+
it('PouchDB emits destruction event on PouchDB object', async () => {
39+
const eventPromise = new Promise((resolve) => {
40+
PouchDB.once('destroyed', (name) => {
41+
name.should.equal(dbs.name, 'should have the same name');
42+
resolve();
43+
});
3644
});
3745
new PouchDB(dbs.name).destroy();
46+
await eventPromise;
3847
});
3948

40-
it('PouchDB emits destroyed when using {name: foo}', function () {
41-
var db = new PouchDB({name: 'testdb'});
42-
return new Promise(function (resolve) {
43-
PouchDB.once('destroyed', function (name) {
49+
it('PouchDB emits destroyed when using {name: foo}', async () => {
50+
const db = new PouchDB({name: 'testdb'});
51+
await new Promise((resolve) => {
52+
PouchDB.once('destroyed', (name) => {
4453
name.should.equal('testdb');
4554
resolve();
4655
});
4756
db.destroy();
4857
});
4958
});
5059

51-
it('db emits destroyed on all DBs', function () {
52-
var db1 = new PouchDB('testdb');
53-
var db2 = new PouchDB('testdb');
60+
it('db emits destroyed on all DBs', async () => {
61+
const db1 = new PouchDB('testdb');
62+
const db2 = new PouchDB('testdb');
5463

55-
return new Promise(function (resolve) {
56-
var called = 0;
57-
function checkDone() {
64+
await new Promise((resolve) => {
65+
let called = 0;
66+
const checkDone = () => {
5867
if (++called === 2) {
5968
resolve();
6069
}
61-
}
70+
};
6271
db1.once('destroyed', checkDone);
6372
db2.once('destroyed', checkDone);
6473
db1.destroy();
6574
});
6675
});
6776

68-
it('3900 db emits destroyed event', function () {
69-
var db = new PouchDB('testdb');
70-
return new Promise(function (resolve) {
71-
db.once('destroyed', function () {
72-
resolve();
73-
});
77+
it('3900 db emits destroyed event', async () => {
78+
const db = new PouchDB('testdb');
79+
await new Promise((resolve) => {
80+
db.once('destroyed', resolve);
7481
db.destroy();
7582
});
7683
});
7784

78-
it('3900 db emits destroyed event 2', function () {
79-
var db = new PouchDB('testdb');
80-
return new Promise(function (resolve) {
81-
db.once('destroyed', function () {
82-
resolve();
83-
});
85+
it('3900 db emits destroyed event 2', async () => {
86+
const db = new PouchDB('testdb');
87+
await new Promise((resolve) => {
88+
db.once('destroyed', resolve);
8489
db.destroy();
8590
});
8691
});
8792

88-
it('emit creation event', function (done) {
89-
var db = new PouchDB(dbs.name).on('created', function (newDB) {
90-
db.should.equal(newDB, 'should be same thing');
91-
done();
93+
it('emit creation event', async () => {
94+
await new Promise((resolve) => {
95+
const db = new PouchDB(dbs.name).on('created', (newDB) => {
96+
db.should.equal(newDB, 'should be same thing');
97+
resolve();
98+
});
9299
});
93100
});
94101

95-
it('#4168 multiple constructor calls don\'t leak listeners', function () {
96-
for (var i = 0; i < 50; i++) {
102+
it('#4168 multiple constructor calls don\'t leak listeners', () => {
103+
for (let i = 0; i < 50; i++) {
97104
new PouchDB(dbs.name);
98105
}
99106
});
100107

101-
it('4922 Destroyed is not called twice', function (done) {
102-
var count = 0;
103-
function destroyed() {
104-
count++;
105-
if (count === 1) {
106-
setTimeout(function () {
107-
count.should.equal(1);
108-
PouchDB.removeListener('destroyed', destroyed);
109-
done();
110-
}, 50);
111-
}
112-
}
113-
PouchDB.on('destroyed', destroyed);
114-
new PouchDB(dbs.name).destroy();
108+
it('4922 Destroyed is not called twice', async () => {
109+
let count = 0;
110+
await new Promise((resolve) => {
111+
const destroyed = () => {
112+
count++;
113+
if (count === 1) {
114+
setTimeout(() => {
115+
count.should.equal(1);
116+
PouchDB.removeListener('destroyed', destroyed);
117+
resolve();
118+
}, 50);
119+
}
120+
};
121+
PouchDB.on('destroyed', destroyed);
122+
new PouchDB(dbs.name).destroy();
123+
});
115124
});
116125

117126
});

0 commit comments

Comments
 (0)