1
1
import { of } from '../../thingies' ;
2
- import type { CrudApi } from '../types' ;
2
+ import type { CrudApi , CrudCollectionEntry } from '../types' ;
3
3
4
4
export type Setup = ( ) => {
5
5
crud : CrudApi ;
@@ -271,12 +271,59 @@ export const testCrudfs = (setup: Setup) => {
271
271
} ) ;
272
272
} ) ;
273
273
274
+ describe ( '.scan()' , ( ) => {
275
+ test ( 'throws if the collection is not valid' , async ( ) => {
276
+ const { crud } = setup ( ) ;
277
+ try {
278
+ const iterable = crud . scan ( [ './..' , 'foo' ] ) ;
279
+ await iterable . next ( ) ;
280
+ throw 'should not reach here' ;
281
+ } catch ( err ) {
282
+ expect ( err ) . toBeInstanceOf ( TypeError ) ;
283
+ expect ( ( < any > err ) . message ) . toBe ( "Failed to execute 'scan' on 'crudfs': Name is not allowed." ) ;
284
+ }
285
+ } ) ;
286
+
287
+ test ( 'can retrieve a list of resources and collections at root' , async ( ) => {
288
+ const { crud } = setup ( ) ;
289
+ await crud . put ( [ 'foo' ] , 'bar' , b ( '1' ) ) ;
290
+ await crud . put ( [ ] , 'baz' , b ( '1' ) ) ;
291
+ await crud . put ( [ ] , 'qux' , b ( '2' ) ) ;
292
+ const list : CrudCollectionEntry [ ] = [ ] ;
293
+ for await ( const entry of crud . scan ( [ ] ) ) list . push ( entry ) ;
294
+ expect ( list . length ) . toBe ( 3 ) ;
295
+ expect ( list . find ( x => x . id === 'baz' ) ) . toMatchObject ( {
296
+ type : 'resource' ,
297
+ id : 'baz' ,
298
+ } ) ;
299
+ expect ( list . find ( x => x . id === 'qux' ) ) . toMatchObject ( {
300
+ type : 'resource' ,
301
+ id : 'qux' ,
302
+ } ) ;
303
+ expect ( list . find ( x => x . id === 'foo' ) ) . toMatchObject ( {
304
+ type : 'collection' ,
305
+ id : 'foo' ,
306
+ } ) ;
307
+ } ) ;
308
+
309
+ test ( 'throws when trying to list a non-existing collection' , async ( ) => {
310
+ const { crud } = setup ( ) ;
311
+ await crud . put ( [ 'foo' ] , 'bar' , b ( '1' ) ) ;
312
+ await crud . put ( [ ] , 'baz' , b ( '1' ) ) ;
313
+ await crud . put ( [ ] , 'qux' , b ( '2' ) ) ;
314
+ const iterator = crud . scan ( [ 'gg' ] ) ;
315
+ const [ , err ] = await of ( iterator . next ( ) ) ;
316
+ expect ( err ) . toBeInstanceOf ( DOMException ) ;
317
+ expect ( ( < any > err ) . name ) . toBe ( 'CollectionNotFound' ) ;
318
+ } ) ;
319
+ } ) ;
320
+
274
321
describe ( '.list()' , ( ) => {
275
322
test ( 'throws if the collection is not valid' , async ( ) => {
276
323
const { crud } = setup ( ) ;
277
324
const [ , err ] = await of ( crud . list ( [ './..' , 'foo' ] ) ) ;
278
325
expect ( err ) . toBeInstanceOf ( TypeError ) ;
279
- expect ( ( < any > err ) . message ) . toBe ( "Failed to execute 'drop ' on 'crudfs': Name is not allowed." ) ;
326
+ expect ( ( < any > err ) . message ) . toBe ( "Failed to execute 'scan ' on 'crudfs': Name is not allowed." ) ;
280
327
} ) ;
281
328
282
329
test ( 'can retrieve a list of resources and collections at root' , async ( ) => {
@@ -300,7 +347,7 @@ export const testCrudfs = (setup: Setup) => {
300
347
} ) ;
301
348
} ) ;
302
349
303
- test ( 'throws when try to list a non-existing collection' , async ( ) => {
350
+ test ( 'throws when trying to list a non-existing collection' , async ( ) => {
304
351
const { crud } = setup ( ) ;
305
352
await crud . put ( [ 'foo' ] , 'bar' , b ( '1' ) ) ;
306
353
await crud . put ( [ ] , 'baz' , b ( '1' ) ) ;
0 commit comments