1
+ import * as SPYABLE_BSON from 'bson' ;
1
2
import { expect } from 'chai' ;
2
3
import * as sinon from 'sinon' ;
3
4
@@ -16,39 +17,56 @@ describe('class MongoDBResponse', () => {
16
17
} ) ;
17
18
18
19
context ( 'utf8 validation' , ( ) => {
19
- afterEach ( ( ) => sinon . restore ( ) ) ;
20
+ let deseriailzeSpy : sinon . SinonSpy ;
21
+ beforeEach ( function ( ) {
22
+ // @ts -expect-error accessing internal property.
23
+ OnDemandDocument . BSON = SPYABLE_BSON ;
24
+
25
+ deseriailzeSpy = sinon . spy ( SPYABLE_BSON , 'deserialize' ) ;
26
+ } ) ;
27
+ afterEach ( function ( ) {
28
+ sinon . restore ( ) ;
29
+ } ) ;
20
30
21
31
context ( 'when enableUtf8Validation is not specified' , ( ) => {
22
32
const options = { enableUtf8Validation : undefined } ;
23
33
it ( 'calls BSON deserialize with writeErrors validation turned off' , ( ) => {
24
34
const res = new MongoDBResponse ( BSON . serialize ( { } ) ) ;
25
- const toObject = sinon . spy ( Object . getPrototypeOf ( Object . getPrototypeOf ( res ) ) , 'toObject' ) ;
26
35
res . toObject ( options ) ;
27
- expect ( toObject ) . to . have . been . calledWith (
28
- sinon . match ( { validation : { utf8 : { writeErrors : false } } } )
29
- ) ;
36
+
37
+ expect ( deseriailzeSpy ) . to . have . been . called ;
38
+
39
+ const [ _buffer , { validation } ] = deseriailzeSpy . getCalls ( ) [ 0 ] . args ;
40
+
41
+ expect ( validation ) . to . deep . equal ( { utf8 : { writeErrors : false } } ) ;
30
42
} ) ;
31
43
} ) ;
32
44
33
45
context ( 'when enableUtf8Validation is true' , ( ) => {
34
46
const options = { enableUtf8Validation : true } ;
35
47
it ( 'calls BSON deserialize with writeErrors validation turned off' , ( ) => {
36
48
const res = new MongoDBResponse ( BSON . serialize ( { } ) ) ;
37
- const toObject = sinon . spy ( Object . getPrototypeOf ( Object . getPrototypeOf ( res ) ) , 'toObject' ) ;
38
49
res . toObject ( options ) ;
39
- expect ( toObject ) . to . have . been . calledWith (
40
- sinon . match ( { validation : { utf8 : { writeErrors : false } } } )
41
- ) ;
50
+
51
+ expect ( deseriailzeSpy ) . to . have . been . called ;
52
+
53
+ const [ _buffer , { validation } ] = deseriailzeSpy . getCalls ( ) [ 0 ] . args ;
54
+
55
+ expect ( validation ) . to . deep . equal ( { utf8 : { writeErrors : false } } ) ;
42
56
} ) ;
43
57
} ) ;
44
58
45
59
context ( 'when enableUtf8Validation is false' , ( ) => {
46
60
const options = { enableUtf8Validation : false } ;
47
61
it ( 'calls BSON deserialize with all validation disabled' , ( ) => {
48
62
const res = new MongoDBResponse ( BSON . serialize ( { } ) ) ;
49
- const toObject = sinon . spy ( Object . getPrototypeOf ( Object . getPrototypeOf ( res ) ) , 'toObject' ) ;
50
63
res . toObject ( options ) ;
51
- expect ( toObject ) . to . have . been . calledWith ( sinon . match ( { validation : { utf8 : false } } ) ) ;
64
+
65
+ expect ( deseriailzeSpy ) . to . have . been . called ;
66
+
67
+ const [ _buffer , { validation } ] = deseriailzeSpy . getCalls ( ) [ 0 ] . args ;
68
+
69
+ expect ( validation ) . to . deep . equal ( { utf8 : false } ) ;
52
70
} ) ;
53
71
} ) ;
54
72
} ) ;
0 commit comments