|
| 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