@@ -5,64 +5,32 @@ const { BSONRegExp } = require('../../src/index');
5
5
6
6
describe ( 'BSONRegExp' , ( ) => {
7
7
describe ( 'bsonRegExp option' , ( ) => {
8
- it ( 'should respond with BSONRegExp class with option passed to db' , async function ( ) {
9
- let client ;
10
- try {
11
- // create and connect to client
12
- client = this . configuration . newClient ( ) ;
13
- await client . connect ( ) ;
14
-
15
- const db = client . db ( 'a' , { bsonRegExp : true } ) ;
16
- const collection = db . collection ( 'b' ) ;
17
-
18
- await collection . insertOne ( { regex : new BSONRegExp ( 'abc' , 'imx' ) } ) ;
19
- const res = await collection . findOne ( { regex : new BSONRegExp ( 'abc' , 'imx' ) } ) ;
20
-
21
- expect ( res ) . has . property ( 'regex' ) . that . is . instanceOf ( BSONRegExp ) ;
22
- } finally {
23
- await client . close ( ) ;
24
- }
25
- } ) ;
26
-
27
- it ( 'should respond with BSONRegExp class with option passed to collection' , async function ( ) {
28
- let client ;
29
- try {
30
- // create and connect to client
31
- client = this . configuration . newClient ( ) ; // bsonRegex
32
- await client . connect ( ) ;
33
-
34
- const db = client . db ( 'a' ) ;
35
- const collection = db . collection ( 'b' , { bsonRegExp : true } ) ;
36
-
37
- await collection . insertOne ( { regex : new BSONRegExp ( 'abc' , 'imx' ) } ) ;
38
- const res = await collection . findOne ( { regex : new BSONRegExp ( 'abc' , 'imx' ) } ) ;
39
-
40
- expect ( res ) . has . property ( 'regex' ) . that . is . instanceOf ( BSONRegExp ) ;
41
- } finally {
42
- await client . close ( ) ;
43
- }
44
- } ) ;
45
-
46
- it ( 'should respond with BSONRegExp class with option passed to operation' , async function ( ) {
47
- let client ;
48
- try {
49
- // create and connect to client
50
- client = this . configuration . newClient ( ) ; // bsonRegex
51
- await client . connect ( ) ;
52
-
53
- const db = client . db ( 'a' ) ;
54
- const collection = db . collection ( 'b' ) ;
55
-
56
- await collection . insertOne ( { regex : new BSONRegExp ( 'abc' , 'imx' ) } ) ;
57
- const res = await collection . findOne (
58
- { regex : new BSONRegExp ( 'abc' , 'imx' ) } ,
59
- { bsonRegExp : true }
60
- ) ;
61
-
62
- expect ( res ) . has . property ( 'regex' ) . that . is . instanceOf ( BSONRegExp ) ;
63
- } finally {
64
- await client . close ( ) ;
65
- }
66
- } ) ;
8
+ // define client and option for tests to use
9
+ let client ;
10
+ const option = { bsonRegExp : true } ;
11
+ for ( const passOptionTo of [ 'client' , 'db' , 'collection' , 'operation' ] ) {
12
+ it ( `should respond with BSONRegExp class with option passed to ${ passOptionTo } ` , async function ( ) {
13
+ try {
14
+ client = this . configuration . newClient ( passOptionTo === 'client' ? option : undefined ) ;
15
+ await client . connect ( ) ;
16
+
17
+ const db = client . db ( 'bson_regex_db' , passOptionTo === 'db' ? option : undefined ) ;
18
+ const collection = db . collection (
19
+ 'bson_regex_coll' ,
20
+ passOptionTo === 'collection' ? option : undefined
21
+ ) ;
22
+
23
+ await collection . insertOne ( { regex : new BSONRegExp ( 'abc' , 'imx' ) } ) ;
24
+ const res = await collection . findOne (
25
+ { regex : new BSONRegExp ( 'abc' , 'imx' ) } ,
26
+ passOptionTo === 'operation' ? option : undefined
27
+ ) ;
28
+
29
+ expect ( res ) . has . property ( 'regex' ) . that . is . instanceOf ( BSONRegExp ) ;
30
+ } finally {
31
+ await client . close ( ) ;
32
+ }
33
+ } ) ;
34
+ }
67
35
} ) ;
68
36
} ) ;
0 commit comments