@@ -11,6 +11,8 @@ const { expect } = require('chai');
1111
1212const iLoveJs = 'mongodb://iLoveJavascript' ;
1313
14+ const currentLegacyVersion = require ( '../../../package.json' ) . version ;
15+
1416describe ( 'legacy_wrappers/mongo_client.js' , ( ) => {
1517 let client ;
1618 beforeEach ( async function ( ) {
@@ -22,6 +24,92 @@ describe('legacy_wrappers/mongo_client.js', () => {
2224 await client . close ( ) ;
2325 } ) ;
2426
27+ describe ( 'calling the constructor with invalid types' , ( ) => {
28+ it ( 'should not throw when passing a non-object type as the options' , ( ) => {
29+ // The driver ignores non-object types in the options arg position
30+ // so this confirms our logic for adding metadata or any other handling
31+ // does not introduce an error for non-object types
32+ expect ( ( ) => {
33+ new LegacyMongoClient ( iLoveJs , true ) ;
34+ } ) . to . not . throw ( ) ;
35+ } ) ;
36+ } ) ;
37+
38+ describe ( 'setting client metadata' , ( ) => {
39+ it ( 'does not mutate the input options' , ( ) => {
40+ expect ( ( ) => {
41+ new LegacyMongoClient ( iLoveJs , Object . freeze ( { } ) ) ;
42+ } ) . to . not . throw ( ) ;
43+ } ) ;
44+
45+ it ( 'does not mutate the input options.driverInfo' , ( ) => {
46+ expect ( ( ) => {
47+ new LegacyMongoClient ( iLoveJs , Object . freeze ( { driverInfo : Object . freeze ( { } ) } ) ) ;
48+ } ) . to . not . throw ( ) ;
49+ } ) ;
50+
51+ describe ( 'when driverInfo.name is provided' , ( ) => {
52+ const client = new LegacyMongoClient ( iLoveJs , { driverInfo : { name : 'mongoose' } } ) ;
53+
54+ it ( 'should prepend mongodb-legacy to user passed driverInfo.name' , ( ) =>
55+ expect ( client . options . metadata ) . to . have . nested . property (
56+ 'driver.name' ,
57+ 'nodejs|mongodb-legacy|mongoose'
58+ ) ) ;
59+
60+ it ( 'should include version in package.json in client metadata' , ( ) =>
61+ expect ( client . options . metadata )
62+ . to . have . property ( 'version' )
63+ . that . includes ( currentLegacyVersion ) ) ;
64+ } ) ;
65+
66+ describe ( 'when driverInfo.name is provided and driverInfo.version is provided' , ( ) => {
67+ const client = new LegacyMongoClient ( iLoveJs , {
68+ driverInfo : { name : 'mongoose' , version : '99.99.99' }
69+ } ) ;
70+
71+ it ( 'should prepend mongodb-legacy to user passed driverInfo.name' , ( ) =>
72+ expect ( client . options . metadata )
73+ . to . have . nested . property ( 'driver.name' )
74+ . that . equals ( 'nodejs|mongodb-legacy|mongoose' ) ) ;
75+
76+ it ( 'should prepend version in package.json to user driverInfo.version' , ( ) =>
77+ expect ( client . options . metadata )
78+ . to . have . property ( 'version' )
79+ . that . includes ( `${ currentLegacyVersion } |99.99.99` ) ) ;
80+ } ) ;
81+
82+ describe ( 'when driverInfo.version is provided' , ( ) => {
83+ const client = new LegacyMongoClient ( iLoveJs , {
84+ driverInfo : { version : '99.99.99' }
85+ } ) ;
86+
87+ it ( 'should include mongodb-legacy in client metadata' , ( ) =>
88+ expect ( client . options . metadata )
89+ . to . have . nested . property ( 'driver.name' )
90+ . that . equals ( 'nodejs|mongodb-legacy' ) ) ;
91+
92+ it ( 'should prepend version in package.json to user driverInfo.version' , ( ) =>
93+ expect ( client . options . metadata )
94+ . to . have . property ( 'version' )
95+ . that . includes ( `${ currentLegacyVersion } |99.99.99` ) ) ;
96+ } ) ;
97+
98+ describe ( 'when driverInfo is not provided' , ( ) => {
99+ const client = new LegacyMongoClient ( iLoveJs ) ;
100+
101+ it ( 'should include mongodb-legacy in client metadata' , ( ) =>
102+ expect ( client . options . metadata )
103+ . to . have . nested . property ( 'driver.name' )
104+ . that . equals ( 'nodejs|mongodb-legacy' ) ) ;
105+
106+ it ( 'should include version in package.json in client metadata' , ( ) =>
107+ expect ( client . options . metadata )
108+ . to . have . property ( 'version' )
109+ . that . includes ( currentLegacyVersion ) ) ;
110+ } ) ;
111+ } ) ;
112+
25113 it ( 'calling MongoClient.connect() returns promise' , async ( ) => {
26114 sinon . stub ( mongodbDriver . MongoClient . prototype , 'connect' ) . returns ( Promise . resolve ( 2 ) ) ;
27115 expect ( client ) . to . have . property ( 'connect' ) . that . is . a ( 'function' ) ;
0 commit comments