3
3
const Buffer = require ( 'buffer' ) . Buffer ;
4
4
const BSON = require ( '../register-bson' ) ;
5
5
const BSONTypeError = BSON . BSONTypeError ;
6
- const util = require ( 'util' ) ;
7
6
const ObjectId = BSON . ObjectId ;
7
+ const util = require ( 'util' ) ;
8
+ const getSymbolFrom = require ( './tools/utils' ) . getSymbolFrom ;
8
9
9
10
describe ( 'ObjectId' , function ( ) {
10
11
it ( 'should correctly handle objectId timestamps' , function ( done ) {
@@ -298,6 +299,7 @@ describe('ObjectId', function () {
298
299
const oidBytesInAString = 'kaffeeklatch' ;
299
300
const oidString = '6b61666665656b6c61746368' ;
300
301
const oid = new ObjectId ( oidString ) ;
302
+ const oidKId = getSymbolFrom ( oid , 'id' ) ;
301
303
it ( 'should return false for an undefined otherId' , ( ) => {
302
304
// otherId === undefined || otherId === null
303
305
expect ( oid . equals ( null ) ) . to . be . false ;
@@ -356,14 +358,15 @@ describe('ObjectId', function () {
356
358
} ) ;
357
359
358
360
it ( 'should not rely on toString for otherIds that are instanceof ObjectId' , ( ) => {
359
- const equalId = { toString : ( ) => oidString + 'wrong' , id : oid . id } ;
361
+ // Note: the method access the symbol prop directly instead of the getter
362
+ const equalId = { toString : ( ) => oidString + 'wrong' , [ oidKId ] : oid . id } ;
360
363
Object . setPrototypeOf ( equalId , ObjectId . prototype ) ;
361
364
expect ( oid . toString ( ) ) . to . not . equal ( equalId . toString ( ) ) ;
362
365
expect ( oid . equals ( equalId ) ) . to . be . true ;
363
366
} ) ;
364
367
365
- it ( 'should use otherId.id Buffer for equality when otherId is instanceof ObjectId' , ( ) => {
366
- let equalId = { id : oid . id } ;
368
+ it ( 'should use otherId[kId] Buffer for equality when otherId is instanceof ObjectId' , ( ) => {
369
+ let equalId = { [ oidKId ] : oid . id } ;
367
370
Object . setPrototypeOf ( equalId , ObjectId . prototype ) ;
368
371
369
372
const propAccessRecord = [ ] ;
@@ -377,7 +380,7 @@ describe('ObjectId', function () {
377
380
expect ( oid . equals ( equalId ) ) . to . be . true ;
378
381
// once for the 11th byte shortcut
379
382
// once for the total equality
380
- expect ( propAccessRecord ) . to . deep . equal ( [ 'id' , 'id' ] ) ;
383
+ expect ( propAccessRecord ) . to . deep . equal ( [ oidKId , oidKId ] ) ;
381
384
} ) ;
382
385
} ) ;
383
386
} ) ;
0 commit comments