Skip to content

Commit 1037d9e

Browse files
update unit type tests
1 parent 839d1dc commit 1037d9e

File tree

4 files changed

+9
-31
lines changed

4 files changed

+9
-31
lines changed

src/mongo_types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export type NotAcceptedFields<TSchema, FieldType> = {
219219
/** @public */
220220
export type OnlyFieldsOfType<TSchema, FieldType = any, AssignableType = FieldType> = IsAny<
221221
TSchema[keyof TSchema],
222-
Record<string, FieldType>,
222+
AssignableType extends FieldType ? Record<string, FieldType> : Record<string, AssignableType>,
223223
AcceptedFields<TSchema, FieldType, AssignableType> &
224224
NotAcceptedFields<TSchema, FieldType> &
225225
Record<string, AssignableType>
@@ -283,7 +283,7 @@ export type PullAllOperator<TSchema> = ({
283283
export type UpdateFilter<TSchema> = {
284284
$currentDate?: OnlyFieldsOfType<
285285
TSchema,
286-
true | Date | Timestamp,
286+
Date | Timestamp,
287287
true | { $type: 'date' | 'timestamp' }
288288
>;
289289
$inc?: OnlyFieldsOfType<TSchema, NumericType | undefined>;

test/integration/crud/find_and_modify.test.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -330,35 +330,6 @@ describe('Collection (#findOneAnd...)', function () {
330330
});
331331
});
332332
});
333-
334-
describe('update filter', function () {
335-
context('when $currentDate is provided', function () {
336-
let client;
337-
let db: Db;
338-
let collection: Collection;
339-
340-
beforeEach(async function () {
341-
client = this.configuration.newClient({ w: 1 });
342-
await client.connect();
343-
db = client.db(this.configuration.db);
344-
collection = db.collection('test_coll');
345-
await collection.insertOne({ a: 'c' });
346-
});
347-
348-
afterEach(async function () {
349-
await collection.drop();
350-
await client.close();
351-
});
352-
353-
it(`should support fields with value 'true'`, async function () {
354-
await collection.findOneAndUpdate(
355-
{},
356-
{ $set: { a: 1 }, $currentDate: { lastModified: true } },
357-
{ writeConcern: { w: 1 } }
358-
);
359-
});
360-
});
361-
});
362333
});
363334

364335
describe('#findOneAndReplace', function () {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ expectAssignable<UpdateFilter<Document>>({ $inc: { anyKeyWhatsoever: 2 } });
6060
// But this no longer asserts anything about what the original keys map to
6161
expectNotType<UpdateFilter<Document>>({ $inc: { numberField: '2' } });
6262

63+
64+
expectAssignable<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: { $type: 'timestamp' }} });
65+
expectAssignable<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: { $type: 'date' }} });
66+
expectAssignable<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: true } });
67+
expectNotType<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: 'true' } });
68+
expectNotType<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: { key2: true }} });
6369
// collection.updateX tests
6470
const client = new MongoClient('');
6571
const db = client.db('test');

test/types/helper_types.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ expectType<NotAcceptedFields<{ a: number; b: string; c: string }, number>>(notAc
6868
expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, number>>({ a: 2 });
6969
expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, string>>({ b: 'hello' });
7070
expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, string, boolean>>({ b: true });
71+
expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, string, boolean>>({ b: true });
7172

7273
// test generic schema, essentially we expect nearly no safety here
7374
expectAssignable<OnlyFieldsOfType<Document, NumericType | undefined>>({ someKey: 2 });

0 commit comments

Comments
 (0)