Skip to content

Commit 0849413

Browse files
author
Grace Chong
committed
test: add further tests for acceptable and invalid inputs for constructor to test for errors
1 parent b44a687 commit 0849413

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

src/objectid.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ export class ObjectId {
6262
try {
6363
this[kId] = ensureBuffer(id.id);
6464
} catch {
65-
throw new BSONTypeError('Argument passed in must have an id that is of type string or Buffer');
65+
throw new BSONTypeError(
66+
'Argument passed in must have an id that is of type string or Buffer'
67+
);
6668
}
6769
}
6870
} else if (id == null || typeof id === 'number') {
@@ -85,9 +87,7 @@ export class ObjectId {
8587
);
8688
}
8789
} else {
88-
throw new TypeError(
89-
'Argument passed in does not match the accepted types'
90-
);
90+
throw new BSONTypeError('Argument passed in does not match the accepted types');
9191
}
9292
// If we are caching the hex string
9393
if (ObjectId.cacheHexString) {

test/node/object_id_tests.js

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('ObjectId', function () {
99
/**
1010
* @ignore
1111
*/
12-
12+
1313
it('should correctly handle objectId timestamps', function (done) {
1414
// var test_number = {id: ObjectI()};
1515
var a = ObjectId.createFromTime(1);
@@ -25,6 +25,11 @@ describe('ObjectId', function () {
2525
done();
2626
});
2727

28+
it('should correctly create ObjectId from ObjectId', function () {
29+
var tmp = new ObjectId();
30+
expect(() => new ObjectId(tmp)).to.not.throw();
31+
});
32+
2833
it('should throw error if empty array is passed in', function () {
2934
expect(() => new ObjectId([])).to.throw(TypeError);
3035
});
@@ -48,18 +53,65 @@ describe('ObjectId', function () {
4853
expect(() => new ObjectId(objectIdLike)).to.throw(TypeError);
4954
});
5055

51-
it('should throw error if object without string or buffer id is passed in', function () {
52-
var objectIdLike = {
56+
it('should throw error if object with non-Buffer non-string id is passed in', function () {
57+
var objectNumId = {
5358
id: 5
5459
};
60+
var objectNullId = {
61+
id: null
62+
};
5563

56-
expect(() => new ObjectId(objectIdLike)).to.throw(TypeError);
64+
expect(() => new ObjectId(objectNumId)).to.throw(TypeError);
65+
expect(() => new ObjectId(objectNullId)).to.throw(TypeError);
66+
});
67+
68+
it('should correctly create ObjectId with objectIdLike properties', function () {
69+
var tmp = new ObjectId();
70+
var objectIdLike = {
71+
id: tmp.id,
72+
toHexString: function () {
73+
return tmp.toHexString();
74+
}
75+
};
76+
77+
expect(() => new ObjectId(objectIdLike)).to.not.throw(TypeError);
5778
});
5879

59-
it('should correctly create ObjectId from number', function () {
80+
it('should correctly create ObjectId from number or null', function () {
6081
expect(() => new ObjectId(42)).to.not.throw();
6182
expect(() => new ObjectId(0x2a)).to.not.throw();
6283
expect(() => new ObjectId(NaN)).to.not.throw();
84+
expect(() => new ObjectId(null)).to.not.throw();
85+
});
86+
87+
it('should correctly create ObjectId with Buffer or string id', function () {
88+
var objectStringId = {
89+
id: 'thisisastringid'
90+
};
91+
var objectBufferId = {
92+
id: Buffer.from('AAAAAAAAAAAAAAAAAAAAAAAA', 'hex')
93+
};
94+
var objectBufferFromArray = {
95+
id: Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
96+
};
97+
98+
expect(() => new ObjectId(objectStringId)).to.not.throw(TypeError);
99+
expect(() => new ObjectId(objectBufferId)).to.not.throw(TypeError);
100+
expect(() => new ObjectId(objectBufferFromArray)).to.not.throw(TypeError);
101+
});
102+
103+
it('should throw error if non-12 byte non-24 hex string passed in', function () {
104+
expect(() => new ObjectId('FFFFFFFFFFFFFFFFFFFFFFFG')).to.throw();
105+
expect(() => new ObjectId('thisismorethan12chars')).to.throw();
106+
expect(() => new ObjectId('101010')).to.throw();
107+
expect(() => new ObjectId('')).to.throw();
108+
});
109+
110+
it('should correctly create ObjectId from 12 byte or 24 hex string', function () {
111+
expect(() => new ObjectId('AAAAAAAAAAAAAAAAAAAAAAAA')).to.not.throw();
112+
expect(() => new ObjectId('FFFFFFFFFFFFFFFFFFFFFFFF')).to.not.throw();
113+
expect(() => new ObjectId('111111111111')).to.not.throw();
114+
expect(() => new ObjectId('abcdefghijkl')).to.not.throw();
63115
});
64116

65117
/**

0 commit comments

Comments
 (0)