Skip to content

Commit fe20426

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

1 file changed

Lines changed: 102 additions & 116 deletions

File tree

tests/integration/test.defaults.js

Lines changed: 102 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4,168 +4,152 @@ if (!process.env.LEVEL_ADAPTER &&
44
!process.env.AUTO_COMPACTION &&
55
!process.env.ADAPTERS) {
66
// these tests don't make sense for anything other than default leveldown
7-
var path = require('path');
7+
const path = require('path');
88
const { mkdirSync } = require('fs');
9-
var rimraf = require('rimraf');
9+
const rimraf = require('rimraf');
1010

11-
describe('defaults', function () {
11+
describe('defaults', () => {
1212

13-
beforeEach(function () {
14-
return new PouchDB('mydb').destroy().then(function () {
15-
return new PouchDB('mydb', {db: require('memdown')}).destroy();
16-
});
13+
beforeEach(async () => {
14+
await new PouchDB('mydb').destroy();
15+
await new PouchDB('mydb', {db: require('memdown')}).destroy();
1716
});
1817

19-
afterEach(function (done) {
18+
afterEach(() => {
2019
rimraf.sync('./tmp/_pouch_.');
2120
rimraf.sync('./tmp/path');
22-
done();
2321
});
2422

25-
it('should allow prefixes', function () {
26-
var prefix = './tmp/path/to/db/1/';
27-
var dir = path.join(prefix, '/tmp/');
28-
var dir2 = path.join('./tmp/_pouch_./', prefix);
29-
var dir3 = path.join(dir2, './tmp/_pouch_mydb');
23+
it('should allow prefixes', async () => {
24+
const prefix = './tmp/path/to/db/1/';
25+
const dir = path.join(prefix, '/tmp/');
26+
const dir2 = path.join('./tmp/_pouch_./', prefix);
27+
const dir3 = path.join(dir2, './tmp/_pouch_mydb');
3028
mkdirSync(dir, { recursive:true });
3129
mkdirSync(dir2, { recursive:true });
3230
mkdirSync(dir3, { recursive:true });
3331

34-
var db = new PouchDB('mydb', {prefix});
35-
return db.info().then(function (info1) {
36-
info1.db_name.should.equal('mydb');
37-
return db.destroy();
38-
});
32+
const db = new PouchDB('mydb', {prefix});
33+
const info1 = await db.info();
34+
info1.db_name.should.equal('mydb');
35+
await db.destroy();
3936
});
4037

41-
it('Defaults leaks eventEmitters', function () {
38+
it('Defaults leaks eventEmitters', () => {
4239
PouchDB.defaults({db: require('memdown') });
4340
PouchDB.defaults({db: require('memdown') });
4441
PouchDB.defaults({db: require('memdown') });
4542
PouchDB.defaults({db: require('memdown') });
4643
});
4744

48-
it('should allow us to set a prefix by default', function () {
49-
var prefix = './tmp/path/to/db/2/';
50-
var dir = path.join(prefix, '/tmp/');
51-
var dir2 = path.join('./tmp/_pouch_./', prefix);
52-
var dir3 = path.join(dir2, './tmp/_pouch_mydb');
45+
it('should allow us to set a prefix by default', async () => {
46+
const prefix = './tmp/path/to/db/2/';
47+
const dir = path.join(prefix, '/tmp/');
48+
const dir2 = path.join('./tmp/_pouch_./', prefix);
49+
const dir3 = path.join(dir2, './tmp/_pouch_mydb');
5350
mkdirSync(dir, { recursive:true });
5451
mkdirSync(dir2, { recursive:true });
5552
mkdirSync(dir3, { recursive:true });
5653

57-
var CustomPouch = PouchDB.defaults({
54+
const CustomPouch = PouchDB.defaults({
5855
prefix
5956
});
60-
var db = CustomPouch({name: 'mydb'});
61-
return db.info().then(function (info1) {
62-
info1.db_name.should.equal('mydb');
63-
return db.destroy();
64-
});
57+
const db = CustomPouch({name: 'mydb'});
58+
const info1 = await db.info();
59+
info1.db_name.should.equal('mydb');
60+
await db.destroy();
6561
});
6662

67-
it('should allow us to use memdown', function () {
68-
var opts = { name: 'mydb', db: require('memdown') };
69-
var db = new PouchDB(opts);
70-
return db.put({_id: 'foo'}).then(function () {
71-
var otherDB = new PouchDB('mydb');
72-
return db.info().then(function (info1) {
73-
return otherDB.info().then(function (info2) {
74-
info1.doc_count.should.not.equal(info2.doc_count);
75-
return otherDB.destroy();
76-
}).then(function () {
77-
return db.destroy();
78-
});
79-
});
80-
});
63+
it('should allow us to use memdown', async () => {
64+
const opts = { name: 'mydb', db: require('memdown') };
65+
const db = new PouchDB(opts);
66+
await db.put({_id: 'foo'});
67+
const otherDB = new PouchDB('mydb');
68+
const info1 = await db.info();
69+
const info2 = await otherDB.info();
70+
info1.doc_count.should.not.equal(info2.doc_count);
71+
await otherDB.destroy();
72+
await db.destroy();
8173
});
8274

83-
it('should allow us to destroy memdown', function () {
84-
var opts = {db: require('memdown') };
85-
var db = new PouchDB('mydb', opts);
86-
return db.put({_id: 'foo'}).then(function () {
87-
var otherDB = new PouchDB('mydb', opts);
88-
return db.info().then(function (info1) {
89-
return otherDB.info().then(function (info2) {
90-
info1.doc_count.should.equal(info2.doc_count);
91-
return otherDB.destroy();
92-
}).then(function () {
93-
var db3 = new PouchDB('mydb', opts);
94-
return db3.info().then(function (info) {
95-
info.doc_count.should.equal(0);
96-
return db3.destroy();
97-
});
98-
});
99-
});
100-
});
75+
it('should allow us to destroy memdown', async () => {
76+
const opts = {db: require('memdown') };
77+
const db = new PouchDB('mydb', opts);
78+
await db.put({_id: 'foo'});
79+
const otherDB = new PouchDB('mydb', opts);
80+
const info1 = await db.info();
81+
const info2 = await otherDB.info();
82+
info1.doc_count.should.equal(info2.doc_count);
83+
await otherDB.destroy();
84+
const db3 = new PouchDB('mydb', opts);
85+
const info = await db3.info();
86+
info.doc_count.should.equal(0);
87+
await db3.destroy();
10188
});
10289

103-
it('should allow us to use memdown by default', function () {
104-
var CustomPouch = PouchDB.defaults({db: require('memdown')});
105-
var db = new CustomPouch('mydb');
106-
return db.put({_id: 'foo'}).then(function () {
107-
var otherDB = new PouchDB('mydb');
108-
return db.info().then(function (info1) {
109-
return otherDB.info().then(function (info2) {
110-
info1.doc_count.should.not.equal(info2.doc_count);
111-
return otherDB.destroy();
112-
}).then(function () {
113-
return db.destroy();
114-
});
115-
});
116-
});
90+
it('should allow us to use memdown by default', async () => {
91+
const CustomPouch = PouchDB.defaults({db: require('memdown')});
92+
const db = new CustomPouch('mydb');
93+
await db.put({_id: 'foo'});
94+
const otherDB = new PouchDB('mydb');
95+
const info1 = await db.info();
96+
const info2 = await otherDB.info();
97+
info1.doc_count.should.not.equal(info2.doc_count);
98+
await otherDB.destroy();
99+
await db.destroy();
117100
});
118101

119102

120-
it('should inform us when using memdown', function () {
121-
var opts = { name: 'mydb', db: require('memdown') };
122-
var db = new PouchDB(opts);
123-
return db.info().then(function (info) {
124-
info.backend_adapter.should.equal('MemDOWN');
125-
});
103+
it('should inform us when using memdown', async () => {
104+
const opts = { name: 'mydb', db: require('memdown') };
105+
const db = new PouchDB(opts);
106+
const info = await db.info();
107+
info.backend_adapter.should.equal('MemDOWN');
126108
});
127109

128-
it('constructor emits destroyed when using defaults', function () {
129-
var CustomPouch = PouchDB.defaults({db: require('memdown')});
110+
it('constructor emits destroyed when using defaults', async () => {
111+
const CustomPouch = PouchDB.defaults({db: require('memdown')});
130112

131-
var db = new CustomPouch('mydb');
132-
return new Promise(function (resolve) {
133-
CustomPouch.once('destroyed', function (name) {
113+
const db = new CustomPouch('mydb');
114+
await new Promise((resolve) => {
115+
CustomPouch.once('destroyed', (name) => {
134116
name.should.equal('mydb');
135117
resolve();
136118
});
137119
db.destroy();
138120
});
139121
});
140122

141-
it('db emits destroyed when using defaults', function () {
142-
var CustomPouch = PouchDB.defaults({db: require('memdown')});
123+
it('db emits destroyed when using defaults', async () => {
124+
const CustomPouch = PouchDB.defaults({db: require('memdown')});
143125

144-
var db = new CustomPouch('mydb');
145-
return new Promise(function (resolve) {
146-
db.once('destroyed', resolve);
147-
db.destroy();
148-
});
126+
const db = new CustomPouch('mydb');
127+
const destroyedPromise = new Promise((resolve) => db.once('destroyed', resolve));
128+
db.destroy();
129+
await destroyedPromise;
149130
});
150131

151-
it('constructor emits creation event', function (done) {
152-
var CustomPouch = PouchDB.defaults({db: require('memdown')});
132+
it('constructor emits creation event', async () => {
133+
const CustomPouch = PouchDB.defaults({db: require('memdown')});
153134

154-
CustomPouch.once('created', function (name) {
155-
name.should.equal('mydb', 'should be same thing');
156-
done();
135+
const eventPromise = new Promise((resolve) => {
136+
CustomPouch.once('created', (name) => {
137+
name.should.equal('mydb', 'should be same thing');
138+
resolve();
139+
});
157140
});
158141
new PouchDB('mydb');
142+
await eventPromise;
159143
});
160144

161145
// somewhat odd behavior (CustomPouch constructor always mirrors PouchDB),
162146
// but better to test it explicitly
163-
it('PouchDB emits destroyed when using defaults', function () {
164-
var CustomPouch = PouchDB.defaults({db: require('memdown')});
147+
it('PouchDB emits destroyed when using defaults', async () => {
148+
const CustomPouch = PouchDB.defaults({db: require('memdown')});
165149

166-
var db = new CustomPouch('mydb');
167-
return new Promise(function (resolve) {
168-
PouchDB.once('destroyed', function (name) {
150+
const db = new CustomPouch('mydb');
151+
await new Promise((resolve) => {
152+
PouchDB.once('destroyed', (name) => {
169153
name.should.equal('mydb');
170154
resolve();
171155
});
@@ -175,25 +159,27 @@ if (!process.env.LEVEL_ADAPTER &&
175159

176160
// somewhat odd behavior (CustomPouch constructor always mirrors PouchDB),
177161
// but better to test it explicitly
178-
it('PouchDB emits created when using defaults', function (done) {
179-
var CustomPouch = PouchDB.defaults({db: require('memdown')});
162+
it('PouchDB emits created when using defaults', async () => {
163+
const CustomPouch = PouchDB.defaults({db: require('memdown')});
180164

181-
PouchDB.once('created', function (name) {
182-
name.should.equal('mydb', 'should be same thing');
183-
done();
165+
const eventPromise = new Promise((resolve) => {
166+
PouchDB.once('created', (name) => {
167+
name.should.equal('mydb', 'should be same thing');
168+
resolve();
169+
});
184170
});
185171
new CustomPouch('mydb');
172+
await eventPromise;
186173
});
187174

188-
it('should be transitive (#5922)', function () {
189-
var CustomPouch = PouchDB
175+
it('should be transitive (#5922)', async () => {
176+
const CustomPouch = PouchDB
190177
.defaults({db: require('memdown')})
191178
.defaults({});
192179

193-
var db = new CustomPouch('mydb');
194-
return db.info().then(function (info) {
195-
info.backend_adapter.should.equal('MemDOWN');
196-
});
180+
const db = new CustomPouch('mydb');
181+
const info = await db.info();
182+
info.backend_adapter.should.equal('MemDOWN');
197183
});
198184
});
199185
}

0 commit comments

Comments
 (0)