Skip to content

Commit

Permalink
test: update tests to correctly set symbol id property
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jan 25, 2022
1 parent 6f90b65 commit 018e739
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
13 changes: 8 additions & 5 deletions test/node/object_id_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
const Buffer = require('buffer').Buffer;
const BSON = require('../register-bson');
const BSONTypeError = BSON.BSONTypeError;
const util = require('util');
const ObjectId = BSON.ObjectId;
const util = require('util');
const getSymbolFrom = require('./tools/utils').getSymbolFrom;

describe('ObjectId', function () {
it('should correctly handle objectId timestamps', function (done) {
Expand Down Expand Up @@ -298,6 +299,7 @@ describe('ObjectId', function () {
const oidBytesInAString = 'kaffeeklatch';
const oidString = '6b61666665656b6c61746368';
const oid = new ObjectId(oidString);
const oidKId = getSymbolFrom(oid, 'id');
it('should return false for an undefined otherId', () => {
// otherId === undefined || otherId === null
expect(oid.equals(null)).to.be.false;
Expand Down Expand Up @@ -356,14 +358,15 @@ describe('ObjectId', function () {
});

it('should not rely on toString for otherIds that are instanceof ObjectId', () => {
const equalId = { toString: () => oidString + 'wrong', id: oid.id };
// Note: the method access the symbol prop directly instead of the getter
const equalId = { toString: () => oidString + 'wrong', [oidKId]: oid.id };
Object.setPrototypeOf(equalId, ObjectId.prototype);
expect(oid.toString()).to.not.equal(equalId.toString());
expect(oid.equals(equalId)).to.be.true;
});

it('should use otherId.id Buffer for equality when otherId is instanceof ObjectId', () => {
let equalId = { id: oid.id };
it('should use otherId[kId] Buffer for equality when otherId is instanceof ObjectId', () => {
let equalId = { [oidKId]: oid.id };
Object.setPrototypeOf(equalId, ObjectId.prototype);

const propAccessRecord = [];
Expand All @@ -377,7 +380,7 @@ describe('ObjectId', function () {
expect(oid.equals(equalId)).to.be.true;
// once for the 11th byte shortcut
// once for the total equality
expect(propAccessRecord).to.deep.equal(['id', 'id']);
expect(propAccessRecord).to.deep.equal([oidKId, oidKId]);
});
});
});
16 changes: 16 additions & 0 deletions test/node/tools/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,19 @@ exports.isNode6 = function () {
// eslint-disable-next-line no-undef
return process.version.split('.')[0] === 'v6';
};

const getSymbolFrom = function (target, symbolName, assertExists) {
if (assertExists == null) assertExists = true;

const symbol = Object.getOwnPropertySymbols(target).filter(
s => s.toString() === `Symbol(${symbolName})`
)[0];

if (assertExists && !symbol) {
throw new Error(`Did not find Symbol(${symbolName}) on ${target}`);
}

return symbol;
};

exports.getSymbolFrom = getSymbolFrom;

0 comments on commit 018e739

Please sign in to comment.