Skip to content

Commit 6454498

Browse files
add regression tests and fix bug dropping first value
1 parent 7a8b186 commit 6454498

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/cursor/abstract_cursor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ export function next<T>(
742742
// All cursors must operate within a session, one must be made implicitly if not explicitly provided
743743
cursor[kInit]((err, value) => {
744744
if (err) return callback(err);
745-
if (value) {
745+
if (value != null) {
746746
return callback(undefined, value);
747747
}
748748
return next(cursor, blocking, callback);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { expect } from 'chai';
2+
3+
import { Collection, MongoClient } from '../../../src';
4+
5+
describe('class AbstractCursor', function () {
6+
let client: MongoClient;
7+
8+
let collection: Collection;
9+
beforeEach(async function () {
10+
client = await this.configuration.newClient().connect();
11+
12+
collection = client.db('abstract_cursor_integration').collection('test');
13+
14+
await collection.insertMany(Array.from({ length: 5 }, (_, index) => ({ index })));
15+
});
16+
17+
afterEach(async function () {
18+
await collection.deleteMany({});
19+
await client.close();
20+
});
21+
22+
context('toArray() with custom transforms', function () {
23+
const falseyValues = [0, NaN, '', false];
24+
for (const value of falseyValues) {
25+
it(`supports mapping to falsey value '${value}'`, async function () {
26+
const cursor = collection.find();
27+
cursor.map(() => value);
28+
29+
const result = await cursor.toArray();
30+
31+
const expected = Array.from({ length: 5 }, () => value);
32+
expect(result).to.deep.equal(expected);
33+
});
34+
}
35+
36+
it('does not support mapping to `null`', async function () {
37+
const cursor = collection.find();
38+
cursor.map(() => null);
39+
40+
const result = await cursor.toArray();
41+
42+
expect(result).to.deep.equal([]);
43+
});
44+
});
45+
});

0 commit comments

Comments
 (0)