From 6454498ef0ecd419b0acdcac09221280a863443e Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Wed, 19 Oct 2022 12:59:21 -0400 Subject: [PATCH] add regression tests and fix bug dropping first value --- src/cursor/abstract_cursor.ts | 2 +- .../node-specific/abstract_cursor.test.ts | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/integration/node-specific/abstract_cursor.test.ts diff --git a/src/cursor/abstract_cursor.ts b/src/cursor/abstract_cursor.ts index dfa3bf07f4b..9101fbfb551 100644 --- a/src/cursor/abstract_cursor.ts +++ b/src/cursor/abstract_cursor.ts @@ -742,7 +742,7 @@ export function next( // 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); diff --git a/test/integration/node-specific/abstract_cursor.test.ts b/test/integration/node-specific/abstract_cursor.test.ts new file mode 100644 index 00000000000..fddffb1e6c6 --- /dev/null +++ b/test/integration/node-specific/abstract_cursor.test.ts @@ -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([]); + }); + }); +});