Skip to content

Commit e90e3ff

Browse files
fix unit tests
1 parent 45e19b6 commit e90e3ff

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/cmap/wire_protocol/on_demand/document.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ type CachedBSONElement = { element: BSONElement; value: any | undefined };
4646

4747
/** @internal */
4848
export class OnDemandDocument {
49+
/**
50+
* @internal
51+
*
52+
* Used for testing purposes.
53+
*/
54+
private static BSON: typeof BSON = BSON;
55+
4956
/**
5057
* Maps JS strings to elements and jsValues for speeding up subsequent lookups.
5158
* - If `false` then name does not exist in the BSON document
@@ -337,7 +344,7 @@ export class OnDemandDocument {
337344
index: this.offset,
338345
allowObjectSmallerThanBufferSize: true
339346
};
340-
return BSON.deserialize(this.bson, exactBSONOptions);
347+
return OnDemandDocument.BSON.deserialize(this.bson, exactBSONOptions);
341348
}
342349

343350
private parseBsonSerializationOptions(options?: { enableUtf8Validation?: boolean }): {

test/unit/cmap/wire_protocol/responses.test.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as SPYABLE_BSON from 'bson';
12
import { expect } from 'chai';
23
import * as sinon from 'sinon';
34

@@ -16,39 +17,56 @@ describe('class MongoDBResponse', () => {
1617
});
1718

1819
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+
});
2030

2131
context('when enableUtf8Validation is not specified', () => {
2232
const options = { enableUtf8Validation: undefined };
2333
it('calls BSON deserialize with writeErrors validation turned off', () => {
2434
const res = new MongoDBResponse(BSON.serialize({}));
25-
const toObject = sinon.spy(Object.getPrototypeOf(Object.getPrototypeOf(res)), 'toObject');
2635
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 } });
3042
});
3143
});
3244

3345
context('when enableUtf8Validation is true', () => {
3446
const options = { enableUtf8Validation: true };
3547
it('calls BSON deserialize with writeErrors validation turned off', () => {
3648
const res = new MongoDBResponse(BSON.serialize({}));
37-
const toObject = sinon.spy(Object.getPrototypeOf(Object.getPrototypeOf(res)), 'toObject');
3849
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 } });
4256
});
4357
});
4458

4559
context('when enableUtf8Validation is false', () => {
4660
const options = { enableUtf8Validation: false };
4761
it('calls BSON deserialize with all validation disabled', () => {
4862
const res = new MongoDBResponse(BSON.serialize({}));
49-
const toObject = sinon.spy(Object.getPrototypeOf(Object.getPrototypeOf(res)), 'toObject');
5063
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 });
5270
});
5371
});
5472
});

0 commit comments

Comments
 (0)