Skip to content

Commit 1ad018c

Browse files
committed
test: update suggestions
1 parent c7f81a6 commit 1ad018c

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

test/integration/crud/find_and_modify.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ describe('Collection (#findOneAnd...)', function () {
3030
});
3131
});
3232

33+
context('when passing includeResultMetadata: false', function () {
34+
let client;
35+
let collection;
36+
37+
beforeEach(async function () {
38+
client = this.configuration.newClient({}, { maxPoolSize: 1 });
39+
collection = client.db('test').collection('findAndModifyTest');
40+
await collection.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } });
41+
});
42+
43+
afterEach(async function () {
44+
await collection.drop();
45+
await client?.close();
46+
});
47+
48+
it('returns the deleted document', async function () {
49+
const result = await collection.findOneAndDelete(
50+
{ a: 1 },
51+
{ includeResultMetadata: false }
52+
);
53+
expect(result.b).to.equal(1);
54+
});
55+
});
56+
3357
context('when passing an object id filter', function () {
3458
let client;
3559
let collection;
@@ -140,6 +164,31 @@ describe('Collection (#findOneAnd...)', function () {
140164
});
141165
});
142166

167+
context('when passing includeResultMetadata: false', function () {
168+
let client;
169+
let collection;
170+
171+
beforeEach(async function () {
172+
client = this.configuration.newClient({}, { maxPoolSize: 1 });
173+
collection = client.db('test').collection('findAndModifyTest');
174+
await collection.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } });
175+
});
176+
177+
afterEach(async function () {
178+
await collection.drop();
179+
await client?.close();
180+
});
181+
182+
it('returns the modified document', async function () {
183+
const result = await collection.findOneAndUpdate(
184+
{ a: 1 },
185+
{ $set: { a: 1 } },
186+
{ includeResultMetadata: false }
187+
);
188+
expect(result.b).to.equal(1);
189+
});
190+
});
191+
143192
context('when passing an object id filter', function () {
144193
let client;
145194
let collection;
@@ -280,6 +329,31 @@ describe('Collection (#findOneAnd...)', function () {
280329
});
281330
});
282331

332+
context('when passing includeResultMetadata: false', function () {
333+
let client;
334+
let collection;
335+
336+
beforeEach(async function () {
337+
client = this.configuration.newClient({}, { maxPoolSize: 1 });
338+
collection = client.db('test').collection('findAndModifyTest');
339+
await collection.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } });
340+
});
341+
342+
afterEach(async function () {
343+
await collection.drop();
344+
await client?.close();
345+
});
346+
347+
it('returns the replaced document', async function () {
348+
const result = await collection.findOneAndReplace(
349+
{ a: 1 },
350+
{ a: 1 },
351+
{ includeResultMetadata: false }
352+
);
353+
expect(result.b).to.equal(1);
354+
});
355+
});
356+
283357
context('when passing an object id filter', function () {
284358
let client;
285359
let collection;

test/types/community/collection/findX.test-d.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ const fooObjWithArray: FooWithArray = {
288288
const fooFilterWithArray: Filter<FooWithArray> = fooObjWithArray;
289289

290290
declare const coll: Collection<{ a: number; b: string }>;
291+
// Passing no options will return the modify result.
291292
expectType<WithId<{ a: number; b: string }> | null>((await coll.findOneAndDelete({ a: 3 })).value);
292293
expectType<WithId<{ a: number; b: string }> | null>(
293294
(await coll.findOneAndReplace({ a: 3 }, { a: 5, b: 'new string' })).value
@@ -305,6 +306,45 @@ expectType<WithId<{ a: number; b: string }> | null>(
305306
).value
306307
);
307308

309+
// Passing empty options will return the modify result.
310+
expectType<WithId<{ a: number; b: string }> | null>(
311+
(await coll.findOneAndDelete({ a: 3 }, {})).value
312+
);
313+
expectType<WithId<{ a: number; b: string }> | null>(
314+
(await coll.findOneAndReplace({ a: 3 }, { a: 5, b: 'new string' }, {})).value
315+
);
316+
expectType<WithId<{ a: number; b: string }> | null>(
317+
(
318+
await coll.findOneAndUpdate(
319+
{ a: 3 },
320+
{
321+
$set: {
322+
a: 5
323+
}
324+
},
325+
{}
326+
)
327+
).value
328+
);
329+
330+
// Including { includeResultMetadata: true } option will return the
331+
// modify result.
332+
expectType<WithId<{ a: number; b: string }> | null>(
333+
(await coll.findOneAndDelete({ a: 3 }, { includeResultMetadata: true })).value
334+
);
335+
expectType<WithId<{ a: number; b: string }> | null>(
336+
(
337+
await coll.findOneAndReplace(
338+
{ a: 3 },
339+
{ a: 5, b: 'new string' },
340+
{ includeResultMetadata: true }
341+
)
342+
).value
343+
);
344+
expectType<WithId<{ a: number; b: string }> | null>(
345+
(await coll.findOneAndUpdate({ a: 3 }, { $set: { a: 5 } }, { includeResultMetadata: true })).value
346+
);
347+
308348
// Including { includeResultMetadata: false } option will change the return type
309349
// to the modified document or null.
310350
expectType<WithId<{ a: number; b: string }> | null>(

0 commit comments

Comments
 (0)