Skip to content

Commit 6d1933b

Browse files
committed
refactor: use asyn/await syntax, arrow functions and const
1 parent 19dd0b2 commit 6d1933b

2 files changed

Lines changed: 83 additions & 120 deletions

File tree

Lines changed: 57 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22

3-
describe("test.issue7810.js", function () {
4-
var adapter = testUtils.adapterType();
5-
var dbs = {};
3+
describe("test.issue7810.js", () => {
4+
const adapter = testUtils.adapterType();
5+
const dbs = {};
66

77
const docData = {
88
_id: "foobar",
@@ -12,16 +12,13 @@ describe("test.issue7810.js", function () {
1212
indexedPairTwo: 'bar'
1313
};
1414

15-
function findInDbs(query) {
16-
return dbs.withIndex.find(query).then((withIndexResults) =>
17-
dbs.withoutIndex.find(query).then((withoutIndexResults) => ({
18-
withIndexResults,
19-
withoutIndexResults,
20-
}))
21-
);
22-
}
15+
const findInDbs = async (query) => {
16+
const withIndexResults = await dbs.withIndex.find(query);
17+
const withoutIndexResults = await dbs.withoutIndex.find(query);
18+
return { withIndexResults, withoutIndexResults };
19+
};
2320

24-
function createIndicesAndPutData() {
21+
const createIndicesAndPutData = () => {
2522
return Promise.all([
2623
dbs.withIndex.createIndex({
2724
index: {
@@ -39,9 +36,9 @@ describe("test.issue7810.js", function () {
3936
dbs.withIndex.put(docData),
4037
dbs.withoutIndex.put(docData),
4138
]);
42-
}
39+
};
4340

44-
function assertWithAndWithoutLengthOf(results, docLen) {
41+
const assertWithAndWithoutLengthOf = (results, docLen) => {
4542
const { withIndexResults, withoutIndexResults } = results;
4643
const withIndexDocs = withIndexResults.docs.length;
4744
const withoutIndexDocs = withoutIndexResults.docs.length;
@@ -53,148 +50,121 @@ describe("test.issue7810.js", function () {
5350
const suffix = docLen === 1 ? '' : 's';
5451
withIndexDocs.should.equal(docLen, `indexed should return ${docLen} doc${suffix}`);
5552
withoutIndexDocs.should.equal(docLen, `non-indexed should return ${docLen} doc${suffix}`);
56-
}
53+
};
5754

58-
beforeEach(function () {
55+
beforeEach(async () => {
5956
dbs.withIndexName = testUtils.adapterUrl(adapter, "with_index");
6057
dbs.withoutIndexName = testUtils.adapterUrl(adapter, "without_index");
6158
dbs.withIndex = new PouchDB(dbs.withIndexName);
6259
dbs.withoutIndex = new PouchDB(dbs.withoutIndexName);
6360

64-
return createIndicesAndPutData();
61+
await createIndicesAndPutData();
6562
});
6663

67-
afterEach(function (done) {
64+
afterEach((done) => {
6865
testUtils.cleanup([dbs.withIndexName, dbs.withoutIndexName], done);
6966
});
7067

71-
it("Testing issue #7810 with selector {} - should return 1 doc", function () {
72-
var query = {
68+
it("Testing issue #7810 with selector {} - should return 1 doc", async () => {
69+
const query = {
7370
selector: {},
7471
limit: 1,
7572
};
76-
return findInDbs(query).then(
77-
(results) => {
78-
assertWithAndWithoutLengthOf(results, 1);
79-
}
80-
);
73+
const results = await findInDbs(query);
74+
assertWithAndWithoutLengthOf(results, 1);
8175
});
8276

83-
it("Testing issue #7810 with selector { _id: {} } - should return 0 docs", function () {
84-
var query = {
77+
it("Testing issue #7810 with selector { _id: {} } - should return 0 docs", async () => {
78+
const query = {
8579
selector: {
8680
_id: {},
8781
},
8882
limit: 1,
8983
};
90-
return findInDbs(query).then(
91-
(results) => {
92-
assertWithAndWithoutLengthOf(results, 0);
93-
}
94-
);
84+
const results = await findInDbs(query);
85+
assertWithAndWithoutLengthOf(results, 0);
9586
});
9687

97-
it("Testing issue #7810 with selector { indexedField: {} } - should return 0 docs", function () {
98-
var query = {
88+
it("Testing issue #7810 with selector { indexedField: {} } - should return 0 docs", async () => {
89+
const query = {
9990
selector: {
10091
indexedField: {},
10192
},
10293
limit: 1,
10394
};
104-
return findInDbs(query).then(
105-
(results) => {
106-
assertWithAndWithoutLengthOf(results, 0);
107-
}
108-
);
95+
const results = await findInDbs(query);
96+
assertWithAndWithoutLengthOf(results, 0);
10997
});
11098

111-
it("Testing issue #7810 with selector { _id: 'foobar'} - should return 1 doc", function () {
112-
var query = {
99+
it("Testing issue #7810 with selector { _id: 'foobar'} - should return 1 doc", async () => {
100+
const query = {
113101
selector: {
114102
_id: "foobar",
115103
},
116104
limit: 1,
117105
};
118-
return findInDbs(query).then(
119-
(results) => {
120-
assertWithAndWithoutLengthOf(results, 1);
121-
}
122-
);
106+
const results = await findInDbs(query);
107+
assertWithAndWithoutLengthOf(results, 1);
123108
});
124109

125-
it("Testing issue #7810 with selector { indexedField: 'foobaz' } - should return 1 doc", function () {
126-
var query = {
110+
it("Testing issue #7810 with selector { indexedField: 'foobaz' } - should return 1 doc", async () => {
111+
const query = {
127112
selector: {
128113
indexedField: "foobaz",
129114
},
130115
limit: 1,
131116
};
132-
return findInDbs(query).then(
133-
(results) => {
134-
assertWithAndWithoutLengthOf(results, 1);
135-
}
136-
);
117+
const results = await findInDbs(query);
118+
assertWithAndWithoutLengthOf(results, 1);
137119
});
138120

139-
it("Testing issue #7810 with selector { numericField: 1337} - should return 1 doc", function () {
140-
var query = {
121+
it("Testing issue #7810 with selector { numericField: 1337} - should return 1 doc", async () => {
122+
const query = {
141123
selector: {
142124
numericField: 1337,
143125
},
144126
limit: 1,
145127
};
146-
return findInDbs(query).then(
147-
(results) => {
148-
assertWithAndWithoutLengthOf(results, 1);
149-
}
150-
);
128+
const results = await findInDbs(query);
129+
assertWithAndWithoutLengthOf(results, 1);
151130
});
152131

153-
it("Testing issue #7810 with selector { numericField: 404 } - should return 0 docs", function () {
154-
var query = {
132+
it("Testing issue #7810 with selector { numericField: 404 } - should return 0 docs", async () => {
133+
const query = {
155134
selector: {
156135
numericField: 404,
157136
},
158137
limit: 1,
159138
};
160-
return findInDbs(query).then(
161-
(results) => {
162-
assertWithAndWithoutLengthOf(results, 0);
163-
}
164-
);
139+
const results = await findInDbs(query);
140+
assertWithAndWithoutLengthOf(results, 0);
165141
});
166142

167143

168-
it("Testing issue #7810 with selector { indexedPairOne: 'foo' } - should return 1 docs", function () {
169-
var query = {
144+
it("Testing issue #7810 with selector { indexedPairOne: 'foo' } - should return 1 docs", async () => {
145+
const query = {
170146
selector: {
171147
indexedPairOne: 'foo',
172148
},
173149
limit: 1,
174150
};
175-
return findInDbs(query).then(
176-
(results) => {
177-
assertWithAndWithoutLengthOf(results, 1);
178-
}
179-
);
151+
const results = await findInDbs(query);
152+
assertWithAndWithoutLengthOf(results, 1);
180153
});
181154

182-
it("Testing issue #7810 with selector { indexedPairOne: 'baz' } - should return 0 docs", function () {
183-
var query = {
155+
it("Testing issue #7810 with selector { indexedPairOne: 'baz' } - should return 0 docs", async () => {
156+
const query = {
184157
selector: {
185158
indexedPairOne: 'baz'
186159
},
187160
limit: 1,
188161
};
189-
return findInDbs(query).then(
190-
(results) => {
191-
assertWithAndWithoutLengthOf(results, 0);
192-
}
193-
);
162+
const results = await findInDbs(query);
163+
assertWithAndWithoutLengthOf(results, 0);
194164
});
195165

196-
it("Testing issue #7810 with selector {} - should return 1 out of 2 docs", function () {
197-
var query = {
166+
it("Testing issue #7810 with selector {} - should return 1 out of 2 docs", async () => {
167+
const query = {
198168
selector: {},
199169
limit: 1,
200170
};
@@ -205,15 +175,11 @@ describe("test.issue7810.js", function () {
205175
indexedPairOne: 'bob',
206176
indexedPairTwo: 'david'
207177
};
208-
return Promise.all([
178+
await Promise.all([
209179
dbs.withIndex.put(otherDoc),
210180
dbs.withoutIndex.put(otherDoc)
211-
]).then(function () {
212-
return findInDbs(query).then(
213-
(results) => {
214-
assertWithAndWithoutLengthOf(results, 1);
215-
}
216-
);
217-
});
181+
]);
182+
const results = await findInDbs(query);
183+
assertWithAndWithoutLengthOf(results, 1);
218184
});
219185
});
Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
"use strict";
22

3-
describe("test.issue8389.js", function () {
4-
var adapter = testUtils.adapterType();
5-
var db = null;
6-
var dbName = null;
3+
describe("test.issue8389.js", () => {
4+
const adapter = testUtils.adapterType();
5+
let db = null;
6+
let dbName = null;
77

88
const docData = {
99
_id: "foobar",
1010
indexedField: "foobaz",
1111
};
1212

13-
function createIndicesAndPutData() {
13+
const createIndicesAndPutData = () => {
1414
return Promise.all([
1515
db.createIndex({
1616
index: {
@@ -19,28 +19,27 @@ describe("test.issue8389.js", function () {
1919
}),
2020
db.put(docData),
2121
]);
22-
}
22+
};
2323

24-
function assertLengthOf(query, docLen) {
25-
return db.find(query).then((results) => {
26-
const suffix = docLen === 1 ? '' : 's';
27-
results.docs.length.should.equal(docLen, `find should return ${docLen} doc${suffix}`);
28-
});
29-
}
24+
const assertLengthOf = async (query, docLen) => {
25+
const results = await db.find(query);
26+
const suffix = docLen === 1 ? '' : 's';
27+
results.docs.length.should.equal(docLen, `find should return ${docLen} doc${suffix}`);
28+
};
3029

31-
beforeEach(function () {
30+
beforeEach(async () => {
3231
dbName = testUtils.adapterUrl(adapter, "issue8389");
3332
db = new PouchDB(dbName);
3433

35-
return createIndicesAndPutData();
34+
await createIndicesAndPutData();
3635
});
3736

38-
afterEach(function (done) {
37+
afterEach((done) => {
3938
testUtils.cleanup([dbName], done);
4039
});
4140

42-
it("Testing issue #8389 _id should work in find index: 0 with nonmatching query", function () {
43-
var query = {
41+
it("Testing issue #8389 _id should work in find index: 0 with nonmatching query", () => {
42+
const query = {
4443
selector: {
4544
indexedField: 'bar',
4645
_id: 'bar',
@@ -49,8 +48,8 @@ describe("test.issue8389.js", function () {
4948
return assertLengthOf(query, 0);
5049
});
5150

52-
it("Testing issue #8389 _id should work in find index: 1 with matching query", function () {
53-
var query = {
51+
it("Testing issue #8389 _id should work in find index: 1 with matching query", () => {
52+
const query = {
5453
selector: {
5554
indexedField: 'foobaz',
5655
_id: 'foobar',
@@ -59,8 +58,8 @@ describe("test.issue8389.js", function () {
5958
return assertLengthOf(query, 1);
6059
});
6160

62-
it("Testing issue #8389 _id should work in find index: 1/2 with multiple docs", function () {
63-
var query = {
61+
it("Testing issue #8389 _id should work in find index: 1/2 with multiple docs", async () => {
62+
const query = {
6463
selector: {
6564
indexedField: 'foobaz',
6665
_id: 'foobar',
@@ -70,13 +69,12 @@ describe("test.issue8389.js", function () {
7069
_id: "charlie",
7170
indexedField: "foobaz",
7271
};
73-
return db.put(otherDoc).then(function () {
74-
return assertLengthOf(query, 1);
75-
});
72+
await db.put(otherDoc);
73+
await assertLengthOf(query, 1);
7674
});
7775

78-
it("Testing issue #8389 _id should work in find index: 2/2 with multiple docs", function () {
79-
var query = {
76+
it("Testing issue #8389 _id should work in find index: 2/2 with multiple docs", async () => {
77+
const query = {
8078
selector: {
8179
indexedField: 'foobaz',
8280
_id: {
@@ -88,8 +86,7 @@ describe("test.issue8389.js", function () {
8886
_id: "charlie",
8987
indexedField: "foobaz",
9088
};
91-
return db.put(otherDoc).then(function () {
92-
return assertLengthOf(query, 2);
93-
});
89+
await db.put(otherDoc);
90+
await assertLengthOf(query, 2);
9491
});
9592
});

0 commit comments

Comments
 (0)