From 018e73959cf140a80e925b4d4fccfbe5ea99a4e9 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Tue, 25 Jan 2022 13:51:38 -0500 Subject: [PATCH] test: update tests to correctly set symbol id property --- test/node/object_id_tests.js | 13 ++++++++----- test/node/tools/utils.js | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/test/node/object_id_tests.js b/test/node/object_id_tests.js index 9be0004a..ff974324 100644 --- a/test/node/object_id_tests.js +++ b/test/node/object_id_tests.js @@ -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) { @@ -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; @@ -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 = []; @@ -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]); }); }); }); diff --git a/test/node/tools/utils.js b/test/node/tools/utils.js index ae464833..b628ec4f 100644 --- a/test/node/tools/utils.js +++ b/test/node/tools/utils.js @@ -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;