Skip to content

Commit

Permalink
add regression tests and fix bug dropping first value
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Oct 19, 2022
1 parent 7a8b186 commit 6454498
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ export function next<T>(
// All cursors must operate within a session, one must be made implicitly if not explicitly provided
cursor[kInit]((err, value) => {
if (err) return callback(err);
if (value) {
if (value != null) {
return callback(undefined, value);
}
return next(cursor, blocking, callback);
Expand Down
45 changes: 45 additions & 0 deletions test/integration/node-specific/abstract_cursor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect } from 'chai';

import { Collection, MongoClient } from '../../../src';

describe('class AbstractCursor', function () {
let client: MongoClient;

let collection: Collection;
beforeEach(async function () {
client = await this.configuration.newClient().connect();

collection = client.db('abstract_cursor_integration').collection('test');

await collection.insertMany(Array.from({ length: 5 }, (_, index) => ({ index })));
});

afterEach(async function () {
await collection.deleteMany({});
await client.close();
});

context('toArray() with custom transforms', function () {
const falseyValues = [0, NaN, '', false];
for (const value of falseyValues) {
it(`supports mapping to falsey value '${value}'`, async function () {
const cursor = collection.find();
cursor.map(() => value);

const result = await cursor.toArray();

const expected = Array.from({ length: 5 }, () => value);
expect(result).to.deep.equal(expected);
});
}

it('does not support mapping to `null`', async function () {
const cursor = collection.find();
cursor.map(() => null);

const result = await cursor.toArray();

expect(result).to.deep.equal([]);
});
});
});

0 comments on commit 6454498

Please sign in to comment.