Skip to content

Commit 018e739

Browse files
committed
test: update tests to correctly set symbol id property
1 parent 6f90b65 commit 018e739

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

test/node/object_id_tests.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
const Buffer = require('buffer').Buffer;
44
const BSON = require('../register-bson');
55
const BSONTypeError = BSON.BSONTypeError;
6-
const util = require('util');
76
const ObjectId = BSON.ObjectId;
7+
const util = require('util');
8+
const getSymbolFrom = require('./tools/utils').getSymbolFrom;
89

910
describe('ObjectId', function () {
1011
it('should correctly handle objectId timestamps', function (done) {
@@ -298,6 +299,7 @@ describe('ObjectId', function () {
298299
const oidBytesInAString = 'kaffeeklatch';
299300
const oidString = '6b61666665656b6c61746368';
300301
const oid = new ObjectId(oidString);
302+
const oidKId = getSymbolFrom(oid, 'id');
301303
it('should return false for an undefined otherId', () => {
302304
// otherId === undefined || otherId === null
303305
expect(oid.equals(null)).to.be.false;
@@ -356,14 +358,15 @@ describe('ObjectId', function () {
356358
});
357359

358360
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 };
360363
Object.setPrototypeOf(equalId, ObjectId.prototype);
361364
expect(oid.toString()).to.not.equal(equalId.toString());
362365
expect(oid.equals(equalId)).to.be.true;
363366
});
364367

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 };
367370
Object.setPrototypeOf(equalId, ObjectId.prototype);
368371

369372
const propAccessRecord = [];
@@ -377,7 +380,7 @@ describe('ObjectId', function () {
377380
expect(oid.equals(equalId)).to.be.true;
378381
// once for the 11th byte shortcut
379382
// once for the total equality
380-
expect(propAccessRecord).to.deep.equal(['id', 'id']);
383+
expect(propAccessRecord).to.deep.equal([oidKId, oidKId]);
381384
});
382385
});
383386
});

test/node/tools/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,19 @@ exports.isNode6 = function () {
156156
// eslint-disable-next-line no-undef
157157
return process.version.split('.')[0] === 'v6';
158158
};
159+
160+
const getSymbolFrom = function (target, symbolName, assertExists) {
161+
if (assertExists == null) assertExists = true;
162+
163+
const symbol = Object.getOwnPropertySymbols(target).filter(
164+
s => s.toString() === `Symbol(${symbolName})`
165+
)[0];
166+
167+
if (assertExists && !symbol) {
168+
throw new Error(`Did not find Symbol(${symbolName}) on ${target}`);
169+
}
170+
171+
return symbol;
172+
};
173+
174+
exports.getSymbolFrom = getSymbolFrom;

0 commit comments

Comments
 (0)