Skip to content

Commit 9894419

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

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-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,106 @@
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+
46+
context('Symbol.asyncIterator() with custom transforms', function () {
47+
const falseyValues = [0, NaN, '', false];
48+
for (const value of falseyValues) {
49+
it(`supports mapping to falsey value '${value}'`, async function () {
50+
const cursor = collection.find();
51+
cursor.map(() => value);
52+
53+
let count = 0;
54+
55+
for await (const document of cursor) {
56+
expect(document).to.deep.equal(value);
57+
count++;
58+
}
59+
60+
expect(count).to.equal(5);
61+
});
62+
}
63+
64+
it('does not support mapping to `null`', async function () {
65+
const cursor = collection.find();
66+
cursor.map(() => null);
67+
68+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
69+
for await (const document of cursor) {
70+
expect.fail('Expected no documents from cursor, received at least one.');
71+
}
72+
});
73+
});
74+
75+
context('forEach() with custom transforms', function () {
76+
const falseyValues = [0, NaN, '', false];
77+
for (const value of falseyValues) {
78+
it(`supports mapping to falsey value '${value}'`, async function () {
79+
const cursor = collection.find();
80+
cursor.map(() => value);
81+
82+
let count = 0;
83+
84+
function transform(value) {
85+
expect(value).to.deep.equal(value);
86+
count++;
87+
}
88+
89+
await cursor.forEach(transform);
90+
91+
expect(count).to.equal(5);
92+
});
93+
}
94+
95+
it('does not support mapping to `null`', async function () {
96+
const cursor = collection.find();
97+
cursor.map(() => null);
98+
99+
function transform() {
100+
expect.fail('Expected no documents from cursor, received at least one.');
101+
}
102+
103+
await cursor.forEach(transform);
104+
});
105+
});
106+
});

0 commit comments

Comments
 (0)