@@ -12,10 +12,14 @@ afterAll(async () => {
1212 await connection . close ( ) ;
1313} ) ;
1414
15- class TestClass extends MongoDBCaching { }
15+ class TestClass extends MongoDBCaching < {
16+ test : boolean ;
17+ title : string ;
18+ } > { }
1619
17- const doc = { test : true } ,
18- doc1 = { test : false } ;
20+ const doc = { test : true , title : "test" } ,
21+ doc1 = { test : false , title : "test" } ,
22+ doc2 = { test : true , title : "test2" } ;
1923
2024describe ( "MongoDB-Caching" , ( ) => {
2125 let cacheClass : TestClass ;
@@ -25,7 +29,7 @@ describe("MongoDB-Caching", () => {
2529 beforeAll ( async ( ) => {
2630 cacheClass = new TestClass ( connection . db ( "test" ) . collection ( "test" ) ) ;
2731
28- await cacheClass . collection . insertMany ( [ doc , doc1 ] ) ;
32+ await cacheClass . collection . insertMany ( [ doc , doc1 , doc2 ] ) ;
2933 } ) ;
3034
3135 it ( "should have no context" , ( ) => {
@@ -77,7 +81,7 @@ describe("MongoDB-Caching", () => {
7781
7882 expect ( dbCountDocumentsSpy ) . toHaveBeenCalledTimes ( 1 ) ;
7983
80- expect ( res ) . toBe ( 2 ) ;
84+ expect ( res ) . toBe ( 3 ) ;
8185
8286 dbCountDocumentsSpy . mockRestore ( ) ;
8387 } ) ;
@@ -93,7 +97,7 @@ describe("MongoDB-Caching", () => {
9397
9498 expect ( dbCountDocumentsSpy ) . toHaveBeenCalledTimes ( 1 ) ;
9599
96- expect ( res ) . toBe ( 2 ) ;
100+ expect ( res ) . toBe ( 3 ) ;
97101
98102 dbCountDocumentsSpy . mockRestore ( ) ;
99103 } ) ;
@@ -111,7 +115,7 @@ describe("MongoDB-Caching", () => {
111115 const res = await cacheClass . find ( ) ;
112116
113117 expect ( dbFindSpy ) . toHaveBeenCalledTimes ( 1 ) ;
114- expect ( res ) . toMatchObject ( [ doc , doc1 ] ) ;
118+ expect ( res ) . toMatchObject ( [ doc , doc1 , doc2 ] ) ;
115119
116120 dbFindSpy . mockRestore ( ) ;
117121 } ) ;
@@ -133,8 +137,8 @@ describe("MongoDB-Caching", () => {
133137 { cursor : c => c . skip ( 1 ) . toArray ( ) }
134138 ) ;
135139
136- expect ( res1 ) . toHaveLength ( 1 ) ;
137- expect ( res1 ) . toMatchObject ( [ doc1 ] ) ;
140+ expect ( res1 ) . toHaveLength ( 2 ) ;
141+ expect ( res1 ) . toMatchObject ( [ doc1 , doc2 ] ) ;
138142 expect ( dbFindSpy ) . toHaveBeenCalledTimes ( 2 ) ;
139143
140144 dbFindSpy . mockRestore ( ) ;
@@ -148,7 +152,7 @@ describe("MongoDB-Caching", () => {
148152
149153 expect ( dbFindSpy ) . toHaveBeenCalledTimes ( 1 ) ;
150154
151- expect ( res ) . toMatchObject ( [ doc , doc1 ] ) ;
155+ expect ( res ) . toMatchObject ( [ doc , doc1 , doc2 ] ) ;
152156
153157 dbFindSpy . mockRestore ( ) ;
154158 } ) ;
@@ -197,4 +201,29 @@ describe("MongoDB-Caching", () => {
197201
198202 activeThrottlesSpy . mockClear ( ) ;
199203 } ) ;
204+
205+ it ( "should return the distinct documents" , async ( ) => {
206+ const dbDistinctSpy = jest . spyOn ( cacheClass . collection , "distinct" ) ;
207+
208+ const res = await cacheClass . distinct ( "title" , { } ) ;
209+
210+ expect ( dbDistinctSpy ) . toHaveBeenCalledTimes ( 1 ) ;
211+
212+ expect ( res ) . toStrictEqual ( [ "test" , "test2" ] ) ;
213+
214+ dbDistinctSpy . mockRestore ( ) ;
215+ } ) ;
216+
217+ it ( "should return the cached distinct documents" , async ( ) => {
218+ const dbDistinctSpy = jest . spyOn ( cacheClass . collection , "distinct" ) ;
219+
220+ await cacheClass . distinct ( "title" , { } ) ;
221+ const res = await cacheClass . distinct ( "title" , { } ) ;
222+
223+ expect ( dbDistinctSpy ) . toHaveBeenCalledTimes ( 1 ) ;
224+
225+ expect ( res ) . toStrictEqual ( [ "test" , "test2" ] ) ;
226+
227+ dbDistinctSpy . mockRestore ( ) ;
228+ } ) ;
200229} ) ;
0 commit comments