Skip to content

Commit 63887ba

Browse files
committed
add tests
1 parent 472c390 commit 63887ba

File tree

1 file changed

+62
-26
lines changed

1 file changed

+62
-26
lines changed

test/integration/crud/find_cursor_methods.test.js

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -362,54 +362,90 @@ describe('Find Cursor', function () {
362362
});
363363
});
364364

365-
describe.only('next + Symbol.asyncIterator()', function () {
365+
describe('next + Symbol.asyncIterator()', function () {
366366
let client;
367367
let collection;
368+
let cursor;
368369

369-
before(async function () {
370+
beforeEach(async function () {
370371
client = this.configuration.newClient();
371372
await client.connect();
372373
collection = client.db('next-symbolasynciterator').collection('bar');
373374
await collection.deleteMany({}, { writeConcern: { w: 'majority' } });
374375
await collection.insertMany([{ a: 1 }, { a: 2 }], { writeConcern: { w: 'majority' } });
375376
});
376377

377-
after(async function () {
378+
afterEach(async function () {
379+
await cursor.close();
378380
await client.close();
379381
});
380382

381-
// fails
382-
it('allows combining iteration modes', async function () {
383-
let count = 0;
384-
const cursor = collection.find().map(doc => {
385-
count++;
386-
return doc;
383+
context('when all documents are retrieved in the first batch', function () {
384+
it('allows combining iteration modes', async function () {
385+
let count = 0;
386+
cursor = collection.find().map(doc => {
387+
count++;
388+
return doc;
389+
});
390+
391+
await cursor.next();
392+
for await (const _ of cursor) {
393+
/* empty */
394+
}
395+
396+
expect(count).to.equal(2);
387397
});
388398

389-
await cursor.next();
390-
for await (const _ of cursor) {
391-
/* empty */
392-
}
399+
it('works with next + next() loop', async function () {
400+
let count = 0;
401+
cursor = collection.find().map(doc => {
402+
count++;
403+
return doc;
404+
});
405+
406+
await cursor.next();
407+
408+
let doc;
409+
while ((doc = (await cursor.next()) && doc != null)) {
410+
/** empty */
411+
}
393412

394-
expect(count).to.equal(2);
413+
expect(count).to.equal(2);
414+
});
395415
});
396416

397-
// passes
398-
it('works with next + next() loop', async function () {
399-
let count = 0;
400-
const cursor = collection.find().map(doc => {
401-
count++;
402-
return doc;
417+
context('when there are documents are not retrieved in the first batch', function () {
418+
it('allows combining iteration modes', async function () {
419+
let count = 0;
420+
cursor = collection.find({}, { batchSize: 1 }).map(doc => {
421+
count++;
422+
return doc;
423+
});
424+
425+
await cursor.next();
426+
for await (const _ of cursor) {
427+
/* empty */
428+
}
429+
430+
expect(count).to.equal(2);
403431
});
404432

405-
await cursor.next();
433+
it('works with next + next() loop', async function () {
434+
let count = 0;
435+
cursor = collection.find({}, { batchSize: 1 }).map(doc => {
436+
count++;
437+
return doc;
438+
});
406439

407-
let doc;
408-
while ((doc = (await cursor.next()) && doc != null)) {
409-
/** empty */
410-
}
440+
await cursor.next();
411441

412-
expect(count).to.equal(2);
442+
let doc;
443+
while ((doc = (await cursor.next()) && doc != null)) {
444+
/** empty */
445+
}
446+
447+
expect(count).to.equal(2);
448+
});
413449
});
414450
});
415451
});

0 commit comments

Comments
 (0)