Skip to content

Commit 4767b65

Browse files
authored
chore: use async await in extension tests (#446)
1 parent 33fe0fe commit 4767b65

File tree

5 files changed

+447
-548
lines changed

5 files changed

+447
-548
lines changed

src/test/suite/editors/collectionDocumentsProvider.test.ts

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ suite('Collection Documents Provider Test Suite', () => {
4343
sinon.restore();
4444
});
4545

46-
test('provideTextDocumentContent parses uri and return documents in the form of a string from a find call', (done) => {
46+
test('provideTextDocumentContent parses uri and return documents in the form of a string from a find call', async () => {
4747
const mockActiveDataService = {
4848
find: (namespace, filter, options, callback): void => {
4949
assert(
@@ -85,19 +85,15 @@ suite('Collection Documents Provider Test Suite', () => {
8585
`scheme:Results: filename.json?namespace=my-favorite-fruit-is.pineapple&operationId=${operationId}`
8686
);
8787

88-
testCollectionViewProvider
89-
.provideTextDocumentContent(uri)
90-
.then((documents) => {
91-
assert(
92-
documents.includes('Declaration of Independence'),
93-
`Expected provideTextDocumentContent to return documents string, found ${documents}`
94-
);
95-
done();
96-
})
97-
.catch(done);
88+
const documents =
89+
await testCollectionViewProvider.provideTextDocumentContent(uri);
90+
assert(
91+
documents.includes('Declaration of Independence'),
92+
`Expected provideTextDocumentContent to return documents string, found ${documents}`
93+
);
9894
});
9995

100-
test('provideTextDocumentContent returns a ejson.stringify string', (done) => {
96+
test('provideTextDocumentContent returns a ejson.stringify string', async () => {
10197
const mockDocuments = [
10298
{
10399
_id: 'first_id',
@@ -139,19 +135,16 @@ suite('Collection Documents Provider Test Suite', () => {
139135
`scheme:Results: filename.json?namespace=test.test&operationId=${operationId}`
140136
);
141137

142-
testCollectionViewProvider
143-
.provideTextDocumentContent(uri)
144-
.then((documents) => {
145-
assert(
146-
documents === mockDocumentsAsJsonString,
147-
`Expected provideTextDocumentContent to return ejson stringified string, found ${documents}`
148-
);
149-
done();
150-
})
151-
.catch(done);
138+
const documents =
139+
await testCollectionViewProvider.provideTextDocumentContent(uri);
140+
assert.strictEqual(
141+
documents,
142+
mockDocumentsAsJsonString,
143+
`Expected provideTextDocumentContent to return ejson stringified string, found ${documents}`
144+
);
152145
});
153146

154-
test('provideTextDocumentContent sets hasMoreDocumentsToShow to false when there arent more documents', (done) => {
147+
test('provideTextDocumentContent sets hasMoreDocumentsToShow to false when there arent more documents', async () => {
155148
const mockActiveDataService = {
156149
find: (namespace, filter, options, callback): void => {
157150
return callback(null, [{ field: 'Apollo' }, { field: 'Gemini ' }]);
@@ -185,28 +178,21 @@ suite('Collection Documents Provider Test Suite', () => {
185178
`scheme:Results: filename.json?namespace=vostok.mercury&operationId=${operationId}`
186179
);
187180

188-
void testCollectionViewProvider.provideTextDocumentContent(uri).then(() => {
189-
assert(
190-
testQueryStore.operations[operationId].hasMoreDocumentsToShow === false,
191-
'Expected not to have more documents to show.'
192-
);
193-
194-
// Reset and test inverse.
195-
testQueryStore.operations[operationId].currentLimit = 2;
196-
testQueryStore.operations[operationId].hasMoreDocumentsToShow = true;
181+
await testCollectionViewProvider.provideTextDocumentContent(uri);
182+
assert(
183+
testQueryStore.operations[operationId].hasMoreDocumentsToShow === false,
184+
'Expected not to have more documents to show.'
185+
);
197186

198-
testCollectionViewProvider
199-
.provideTextDocumentContent(uri)
200-
.then(() => {
201-
assert(testQueryStore.operations[operationId].hasMoreDocumentsToShow);
187+
// Reset and test inverse.
188+
testQueryStore.operations[operationId].currentLimit = 2;
189+
testQueryStore.operations[operationId].hasMoreDocumentsToShow = true;
202190

203-
done();
204-
})
205-
.catch(done);
206-
});
191+
await testCollectionViewProvider.provideTextDocumentContent(uri);
192+
assert(testQueryStore.operations[operationId].hasMoreDocumentsToShow);
207193
});
208194

209-
test('provideTextDocumentContent shows a status bar item while it is running then hide it', (done) => {
195+
test('provideTextDocumentContent shows a status bar item while it is running then hide it', async () => {
210196
const mockActiveDataService = { find: {} } as DataService;
211197
const mockConnectionController = new ConnectionController(
212198
new StatusView(mockExtensionContext),
@@ -254,12 +240,8 @@ suite('Collection Documents Provider Test Suite', () => {
254240
return callback(null, [{ field: 'aaaaaaaaaaaaaaaaa' }]);
255241
};
256242

257-
testCollectionViewProvider
258-
.provideTextDocumentContent(uri)
259-
.then(() => {
260-
assert(mockHideMessage.called);
261-
})
262-
.then(done, done);
243+
await testCollectionViewProvider.provideTextDocumentContent(uri);
244+
assert(mockHideMessage.called);
263245
});
264246

265247
test('provideTextDocumentContent sets different code lenses for different namespaces from the same connection', async () => {

src/test/suite/explorer/databaseTreeItem.test.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ suite('DatabaseTreeItem Test Suite', () => {
4040
);
4141
});
4242

43-
test('when not expanded it does not show collections', (done) => {
43+
test('when not expanded it does not show collections', async () => {
4444
const testDatabaseTreeItem = new DatabaseTreeItem(
4545
mockDatabaseNames[1],
4646
new DataServiceStub(),
@@ -49,15 +49,12 @@ suite('DatabaseTreeItem Test Suite', () => {
4949
{}
5050
);
5151

52-
testDatabaseTreeItem
53-
.getChildren()
54-
.then((collections) => {
55-
assert(
56-
collections.length === 0,
57-
`Expected no collections to be returned, recieved ${collections.length}`
58-
);
59-
})
60-
.then(done, done);
52+
const collections = await testDatabaseTreeItem.getChildren();
53+
assert.strictEqual(
54+
collections.length,
55+
0,
56+
`Expected no collections to be returned, recieved ${collections.length}`
57+
);
6158
});
6259

6360
test('when expanded shows the collections of a database in tree', async () => {

src/test/suite/explorer/schemaTreeItem.test.ts

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ suite('SchemaTreeItem Test Suite', function () {
119119
);
120120
});
121121

122-
test('it should show a show more item when there are more fields to show', (done) => {
122+
test('it should show a show more item when there are more fields to show', async () => {
123123
const amountOfFieldsExpected = FIELDS_TO_SHOW;
124124
const mockDocWithTwentyFields = {};
125125
for (let i = 0; i < 20; i++) {
@@ -140,26 +140,23 @@ suite('SchemaTreeItem Test Suite', function () {
140140
{}
141141
);
142142

143-
testSchemaTreeItem
144-
.getChildren()
145-
.then((schemaFields) => {
146-
assert(FIELDS_TO_SHOW === 15, 'Expeted FIELDS_TO_SHOW to be 15');
147-
148-
assert(
149-
schemaFields.length === amountOfFieldsExpected + 1,
150-
`Expected ${
151-
amountOfFieldsExpected + 1
152-
} documents to be returned, found ${schemaFields.length}`
153-
);
154-
assert(
155-
schemaFields[amountOfFieldsExpected].label === 'Show more fields...',
156-
`Expected a tree item child with the label "Show more fields..." found ${schemaFields[amountOfFieldsExpected].label}`
157-
);
158-
})
159-
.then(done, done);
143+
const schemaFields = await testSchemaTreeItem.getChildren();
144+
assert.strictEqual(FIELDS_TO_SHOW, 15, 'Expeted FIELDS_TO_SHOW to be 15');
145+
146+
assert.strictEqual(
147+
schemaFields.length,
148+
amountOfFieldsExpected + 1,
149+
`Expected ${amountOfFieldsExpected + 1} documents to be returned, found ${
150+
schemaFields.length
151+
}`
152+
);
153+
assert(
154+
schemaFields[amountOfFieldsExpected].label === 'Show more fields...',
155+
`Expected a tree item child with the label "Show more fields..." found ${schemaFields[amountOfFieldsExpected].label}`
156+
);
160157
});
161158

162-
test('it should show more fields after the show more click handler is called', (done) => {
159+
test('it should show more fields after the show more click handler is called', async () => {
163160
const mockDocWithThirtyFields = {};
164161
for (let i = 0; i < 30; i++) {
165162
mockDocWithThirtyFields[`${i}`] = 'some value';
@@ -181,20 +178,16 @@ suite('SchemaTreeItem Test Suite', function () {
181178

182179
testSchemaTreeItem.onShowMoreClicked();
183180

184-
testSchemaTreeItem
185-
.getChildren()
186-
.then((schemaFields) => {
187-
const amountOfFieldsExpected = 30;
188-
189-
assert(
190-
schemaFields.length === amountOfFieldsExpected,
191-
`Expected ${amountOfFieldsExpected} documents to be returned, found ${schemaFields.length}`
192-
);
193-
})
194-
.then(done, done);
181+
const schemaFields = await testSchemaTreeItem.getChildren();
182+
const amountOfFieldsExpected = 30;
183+
184+
assert(
185+
schemaFields.length === amountOfFieldsExpected,
186+
`Expected ${amountOfFieldsExpected} documents to be returned, found ${schemaFields.length}`
187+
);
195188
});
196189

197-
test('When schema parsing fails it displays an error message', (done) => {
190+
test('When schema parsing fails it displays an error message', async () => {
198191
const testSchemaTreeItem = new SchemaTreeItem(
199192
'favoritePiesIWantToEatRightNow',
200193
TEST_DB_NAME,
@@ -210,23 +203,19 @@ suite('SchemaTreeItem Test Suite', function () {
210203
{}
211204
);
212205

213-
testSchemaTreeItem
214-
.getChildren()
215-
.then(
216-
() => {
217-
assert(false, 'Didnt expect to succeed.');
218-
},
219-
(error) => {
220-
const expectedMessage =
221-
'Unable to parse schema: Unknown input type for `docs`. Must be an array, stream or MongoDB Cursor.';
222-
223-
assert(
224-
error.message === expectedMessage,
225-
`Expected error message to be "${expectedMessage}" found "${error.message}"`
226-
);
227-
}
228-
)
229-
.then(done, done);
206+
try {
207+
await testSchemaTreeItem.getChildren();
208+
assert(false, 'Didnt expect to succeed.');
209+
} catch (error: any) {
210+
const expectedMessage =
211+
'Unable to parse schema: Unknown input type for `docs`. Must be an array, stream or MongoDB Cursor.';
212+
213+
assert.strictEqual(
214+
error.message,
215+
expectedMessage,
216+
`Expected error message to be "${expectedMessage}" found "${error.message}"`
217+
);
218+
}
230219
});
231220

232221
suite('Live Database Tests', () => {

0 commit comments

Comments
 (0)