@@ -57,99 +57,73 @@ describe('class AbstractCursor', function () {
57
57
await client . close ( ) ;
58
58
} ) ;
59
59
60
- describe ( 'tryNext()' , function ( ) {
60
+ context ( `hasNext()` , function ( ) {
61
61
context ( 'when there is a transform on the cursor' , function ( ) {
62
- it ( 'does not transform any documents' , async function ( ) {
62
+ it ( `the transform is NOT called` , async ( ) => {
63
63
const cursor = collection . find ( ) . map ( transformSpy ) ;
64
64
65
- await cursor . hasNext ( ) ;
66
- expect ( transformSpy . called ) . to . be . false ;
65
+ const hasNext = await cursor . hasNext ( ) ;
66
+ expect ( transformSpy ) . not . to . have . been . called ;
67
+ expect ( hasNext ) . to . be . true ;
67
68
} ) ;
68
69
} ) ;
69
70
} ) ;
70
71
71
72
const operations : ReadonlyArray < readonly [ string , ( arg0 : FindCursor ) => Promise < unknown > ] > = [
72
73
[
73
74
'tryNext' ,
74
- ( cursor : FindCursor ) => {
75
- return cursor . tryNext ( ) ;
76
- }
75
+ ( cursor : FindCursor ) => cursor . tryNext ( )
77
76
] ,
78
77
[ 'next' , ( cursor : FindCursor ) => cursor . next ( ) ] ,
79
78
[
80
79
'Symbol.asyncIterator().next' ,
81
80
async ( cursor : FindCursor ) => {
82
81
const iterator = cursor [ Symbol . asyncIterator ] ( ) ;
83
- const doc = await iterator . next ( ) ;
84
- return doc . value ;
82
+ return iterator . next ( ) . then ( ( { value } ) => value ) ;
85
83
}
86
- ]
84
+ ] ,
85
+ [ 'Cursor.stream' , ( cursor : FindCursor ) => {
86
+ const stream = cursor . stream ( ) ;
87
+ return once ( stream , 'data' ) . then ( ( [ doc ] ) => doc )
88
+ } ]
87
89
] as const ;
88
90
89
- context ( 'when there is a transform on the cursor' , function ( ) {
90
- for ( const [ method , func ] of operations ) {
91
- it ( `${ method } () calls the cursor transform when iterated` , async ( ) => {
92
- const cursor = collection . find ( ) . map ( transformSpy ) ;
93
-
94
- const doc = await func ( cursor ) ;
95
- expect ( transformSpy ) . to . have . been . calledOnce ;
96
- expect ( doc . name ) . to . equal ( 'JOHN DOE' ) ;
97
- } ) ;
91
+ for ( const [ method , func ] of operations ) {
92
+ context ( `${ method } ()` , function ( ) {
93
+ context ( 'when there is a transform on the cursor' , function ( ) {
94
+ it ( `the transform is called` , async ( ) => {
95
+ const cursor = collection . find ( ) . map ( transformSpy ) ;
98
96
99
- it ( `when the transform throws, ${ method } () propagates the error to the user` , async ( ) => {
100
- const cursor = collection . find ( ) . map ( ( ) => {
101
- throw new Error ( 'error thrown in transform ') ;
97
+ const doc = await func ( cursor ) ;
98
+ expect ( transformSpy ) . to . have . been . calledOnce ;
99
+ expect ( doc . name ) . to . equal ( 'JOHN DOE ') ;
102
100
} ) ;
103
-
104
- const error = await func ( cursor ) . catch ( e => e ) ;
105
- expect ( error )
106
- . to . be . instanceOf ( Error )
107
- . to . match ( / e r r o r t h r o w n i n t r a n s f o r m / ) ;
108
- expect ( cursor . closed ) . to . be . true ;
109
- } ) ;
110
- }
111
-
112
- it ( 'Cursor.stream() calls the cursor transform when iterated' , async function ( ) {
113
- const cursor = collection . find ( ) . map ( transformSpy ) . stream ( ) ;
114
-
115
- const [ doc ] = await once ( cursor , 'data' ) ;
116
- expect ( transformSpy ) . to . have . been . calledOnce ;
117
- expect ( doc . name ) . to . equal ( 'JOHN DOE' ) ;
118
- } ) ;
119
-
120
- it ( `when the transform throws, Cursor.stream() propagates the error to the user` , async ( ) => {
121
- const cursor = collection
122
- . find ( )
123
- . map ( ( ) => {
124
- throw new Error ( 'error thrown in transform' ) ;
101
+ context ( 'when the transform throws' , function ( ) {
102
+ it ( `the error is propagated to the user` , async ( ) => {
103
+ const cursor = collection . find ( ) . map ( ( ) => {
104
+ throw new Error ( 'error thrown in transform' ) ;
105
+ } ) ;
106
+
107
+ const error = await func ( cursor ) . catch ( e => e ) ;
108
+ expect ( error )
109
+ . to . be . instanceOf ( Error )
110
+ . to . match ( / e r r o r t h r o w n i n t r a n s f o r m / ) ;
111
+ expect ( cursor . closed ) . to . be . true ;
112
+ } ) ;
125
113
} )
126
- . stream ( ) ;
114
+ } ) ;
127
115
128
- const error = await once ( cursor , 'data' ) . catch ( e => e ) ;
129
- expect ( error )
130
- . to . be . instanceOf ( Error )
131
- . to . match ( / e r r o r t h r o w n i n t r a n s f o r m / ) ;
132
- expect ( cursor . _cursor ) . to . have . property ( 'closed' , true ) ;
133
- } ) ;
134
- } ) ;
116
+ context ( 'when there is not a transform on the cursor' , function ( ) {
117
+ it ( `it returns the cursor's documents unmodified` , async ( ) => {
118
+ const cursor = collection . find ( ) ;
135
119
136
- context ( 'when there is not a transform on the cursor' , function ( ) {
137
- for ( const [ method , func ] of operations ) {
138
- it ( `${ method } () returns the documents, unmodified` , async ( ) => {
139
- const cursor = collection . find ( ) ;
140
-
141
- const doc = await func ( cursor ) ;
142
- expect ( doc . name ) . to . equal ( 'john doe' ) ;
120
+ const doc = await func ( cursor ) ;
121
+ expect ( doc . name ) . to . equal ( 'john doe' ) ;
122
+ } ) ;
143
123
} ) ;
144
- }
145
124
146
- it ( 'Cursor.stream() returns the documents, unmodified' , async function ( ) {
147
- const cursor = collection . find ( ) . stream ( ) ;
148
-
149
- const [ doc ] = await once ( cursor , 'data' ) ;
150
- expect ( doc . name ) . to . equal ( 'john doe' ) ;
151
125
} ) ;
152
- } ) ;
126
+ }
153
127
} ) ;
154
128
155
129
describe ( 'custom transforms with falsy values' , function ( ) {
0 commit comments