Skip to content

Commit e1fbbb4

Browse files
authored
(#8651) - restructure async handling of test.viewadapter
Re-instates async test handling using done() which was removed in 25db22f, and adds more direct handling of errors. Co-authored-by: alxndrsn <alxndrsn>
1 parent 080b8a9 commit e1fbbb4

File tree

1 file changed

+89
-64
lines changed

1 file changed

+89
-64
lines changed

tests/integration/test.viewadapter.js

Lines changed: 89 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -48,85 +48,110 @@ viewAdapters.forEach(viewAdapter => {
4848
dbs.name = testUtils.adapterUrl('local', 'testdb');
4949
});
5050

51-
it('Create pouch with separate view adapters', async function () {
51+
it('Create pouch with separate view adapters', function (done) {
5252
const db = new PouchDB(dbs.name, {view_adapter: viewAdapter});
5353

5454
if (db.adapter === viewAdapter) {
55-
return;
55+
return done();
5656
}
5757

5858
if (db.adapter !== 'leveldb' && db.adapter !== 'idb') {
59-
return;
59+
return done();
6060
}
6161

62-
await db.bulkDocs(docs);
63-
await db.query('index', { key: 'abc', include_docs: true });
64-
65-
if (testUtils.isNode()) {
66-
const dbs = getDbNamesFromLevelDBFolder(db.name);
67-
dbs.length.should.equal(1); // only one db created on disk, no dependent db created
68-
} else {
69-
const { viewDbName, docDbName } = getDBNames(localStorage);
70-
// check indexedDB for saved views
71-
// need to add '_pouch_' because views are saved in memory
72-
const viewRequest = indexedDB.open('_pouch_' + viewDbName, 1);
73-
viewRequest.onupgradeneeded = function (event) {
74-
// The version of the view database created is 1 which shows that this
75-
// database was newly created in IndexedDB and did not exist there
76-
// before. So the view database was created in the database specified in
77-
// the view_adapter and not in the default `idb`adapter.
78-
event.oldVersion.should.equal(0);
79-
event.newVersion.should.equal(1);
80-
};
81-
82-
viewRequest.onsuccess = function () {
83-
// Nothing is saved here
84-
viewRequest.result.objectStoreNames.length.should.equal(0);
85-
viewRequest.result.version.should.equal(1);
86-
};
87-
88-
// check indexedDB for saved docs
89-
const docRequest = indexedDB.open(docDbName, 5);
90-
docRequest.onsuccess = function () {
91-
// something is saved here
92-
docRequest.result.objectStoreNames.length.should.equal(7);
93-
};
94-
}
62+
db.bulkDocs(docs).then(function () {
63+
db.query('index', {
64+
key: 'abc',
65+
include_docs: true
66+
}).then(function () {
67+
68+
if (testUtils.isNode()) {
69+
const dbs = getDbNamesFromLevelDBFolder(db.name);
70+
dbs.length.should.equal(1); // only one db created on disk, no dependent db created
71+
done();
72+
} else {
73+
const { viewDbName, docDbName } = getDBNames(localStorage);
74+
// check indexedDB for saved views
75+
// need to add '_pouch_' because views are saved in memory
76+
const viewRequest = indexedDB.open('_pouch_' + viewDbName, 1);
77+
viewRequest.onupgradeneeded = function (event) {
78+
// The version of the view database created is 1 which shows that this
79+
// database was newly created in IndexedDB and did not exist there
80+
// before. So the view database was created in the database specified in
81+
// the view_adapter and not in the default `idb`adapter.
82+
event.oldVersion.should.equal(0);
83+
event.newVersion.should.equal(1);
84+
};
85+
viewRequest.onerror = function () {
86+
done(new Error('viewRequest.open() failed'));
87+
};
88+
viewRequest.onsuccess = function () {
89+
// Nothing is saved here
90+
viewRequest.result.objectStoreNames.length.should.equal(0);
91+
viewRequest.result.version.should.equal(1);
92+
93+
// check indexedDB for saved docs
94+
const docRequest = indexedDB.open(docDbName, 5);
95+
docRequest.onerror = function () {
96+
done(new Error('docRequest.open() failed'));
97+
};
98+
docRequest.onsuccess = function () {
99+
// something is saved here
100+
docRequest.result.objectStoreNames.length.should.equal(7);
101+
done();
102+
};
103+
};
104+
}
105+
}).catch(done);
106+
}).catch(done);
95107
});
96108

97-
it('Create pouch with no view adapters', async function () {
109+
it('Create pouch with no view adapters', function (done) {
98110
const db = new PouchDB(dbs.name);
99111

100112
if (db.adapter !== 'leveldb' && db.adapter !== 'idb') {
101-
return;
113+
return done();
102114
}
103115

104-
await db.bulkDocs(docs);
105-
await db.query('index', { key: 'abc', include_docs: true });
106-
107-
if (testUtils.isNode()) {
108-
const dbs = getDbNamesFromLevelDBFolder(db.name);
109-
const expectedLength = db.adapter === 'memory' ? 0 : 2;
110-
dbs.length.should.equal(expectedLength);
111-
} else {
112-
const { viewDbName, docDbName } = getDBNames(localStorage);
113-
114-
// check indexedDB for saved views
115-
const viewRequest = indexedDB.open(viewDbName, 5);
116-
viewRequest.onsuccess = function () {
117-
// Something is saved here
118-
// This shows that without a view_adapter specified
119-
// the view query data is stored in the default adapter database.
120-
viewRequest.result.objectStoreNames.length.should.equal(7);
121-
};
122-
123-
// check indexedDB for saved docs
124-
const docRequest = indexedDB.open(docDbName, 5);
125-
docRequest.onsuccess = function () {
126-
// something is saved here
127-
docRequest.result.objectStoreNames.length.should.equal(7);
128-
};
129-
}
116+
db.bulkDocs(docs).then(function () {
117+
db.query('index', {
118+
key: 'abc',
119+
include_docs: true
120+
}).then(function () {
121+
122+
if (testUtils.isNode()) {
123+
const dbs = getDbNamesFromLevelDBFolder(db.name);
124+
const expectedLength = db.adapter === 'memory' ? 0 : 2;
125+
dbs.length.should.equal(expectedLength);
126+
done();
127+
} else {
128+
const { viewDbName, docDbName } = getDBNames(localStorage);
129+
130+
// check indexedDB for saved views
131+
const viewRequest = indexedDB.open(viewDbName, 5);
132+
viewRequest.onerror = function () {
133+
done(new Error('viewRequest.open() failed'));
134+
};
135+
viewRequest.onsuccess = function () {
136+
// Something is saved here
137+
// This shows that without a view_adapter specified
138+
// the view query data is stored in the default adapter database.
139+
viewRequest.result.objectStoreNames.length.should.equal(7);
140+
141+
// check indexedDB for saved docs
142+
const docRequest = indexedDB.open(docDbName, 5);
143+
docRequest.onerror = function () {
144+
done(new Error('docRequest.open() failed'));
145+
};
146+
docRequest.onsuccess = function () {
147+
// something is saved here
148+
docRequest.result.objectStoreNames.length.should.equal(7);
149+
done();
150+
};
151+
};
152+
}
153+
}).catch(done);
154+
}).catch(done);
130155
});
131156
});
132157
});

0 commit comments

Comments
 (0)