@@ -9,7 +9,7 @@ describe('ObjectId', function () {
9
9
/**
10
10
* @ignore
11
11
*/
12
-
12
+
13
13
it ( 'should correctly handle objectId timestamps' , function ( done ) {
14
14
// var test_number = {id: ObjectI()};
15
15
var a = ObjectId . createFromTime ( 1 ) ;
@@ -25,6 +25,11 @@ describe('ObjectId', function () {
25
25
done ( ) ;
26
26
} ) ;
27
27
28
+ it ( 'should correctly create ObjectId from ObjectId' , function ( ) {
29
+ var tmp = new ObjectId ( ) ;
30
+ expect ( ( ) => new ObjectId ( tmp ) ) . to . not . throw ( ) ;
31
+ } ) ;
32
+
28
33
it ( 'should throw error if empty array is passed in' , function ( ) {
29
34
expect ( ( ) => new ObjectId ( [ ] ) ) . to . throw ( TypeError ) ;
30
35
} ) ;
@@ -48,18 +53,65 @@ describe('ObjectId', function () {
48
53
expect ( ( ) => new ObjectId ( objectIdLike ) ) . to . throw ( TypeError ) ;
49
54
} ) ;
50
55
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 = {
53
58
id : 5
54
59
} ;
60
+ var objectNullId = {
61
+ id : null
62
+ } ;
55
63
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 ) ;
57
78
} ) ;
58
79
59
- it ( 'should correctly create ObjectId from number' , function ( ) {
80
+ it ( 'should correctly create ObjectId from number or null ' , function ( ) {
60
81
expect ( ( ) => new ObjectId ( 42 ) ) . to . not . throw ( ) ;
61
82
expect ( ( ) => new ObjectId ( 0x2a ) ) . to . not . throw ( ) ;
62
83
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 ( ) ;
63
115
} ) ;
64
116
65
117
/**
0 commit comments