import test from 'ava' ; import { createMagnetarInstance } from '../helpers/createMagnetarInstance' ; import { pokedex , waitMs } from '@magnetarjs/test-utils' ; { const testName = 'stream (collection)' ; test ( testName , async ( t ) => { const { pokedexModule } = await createMagnetarInstance ( 'read' ) ; t . deepEqual ( pokedexModule . doc ( '136' ) . data , undefined ) ; t . is ( pokedexModule . data . size , 0 ) ; pokedexModule . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; await waitMs ( 3500 ) ; pokedexModule . closeStream ( ) ; t . deepEqual ( pokedexModule . data . get ( '1' ) , pokedex ( 1 ) ) ; t . deepEqual ( pokedexModule . data . get ( '2' ) , pokedex ( 2 ) ) ; t . deepEqual ( pokedexModule . data . get ( '3' ) , pokedex ( 3 ) ) ; t . is ( pokedexModule . data . size , 151 ) ; } ) ; } { const testName = 'stream (doc)' ; test ( testName , async ( t ) => { const { trainerModule } = await createMagnetarInstance ( 'read' ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 } ) ; trainerModule . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; await waitMs ( 3500 ) ; trainerModule . closeStream ( ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' } ) ; } ) ; } { const testName = 'stream (collection) where-filter' ; test ( testName , async ( t ) => { const { pokedexModule } = await createMagnetarInstance ( 'read' ) ; t . is ( pokedexModule . data . size , 0 ) ; const pokedexModuleWithQuery = pokedexModule . where ( 'type' , 'array-contains' , 'Fire' ) . where ( 'base.Speed' , '>=' , 100 ) . orderBy ( 'base.Speed' , 'asc' ) . orderBy ( 'name' , 'asc' ) ; pokedexModuleWithQuery . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; await waitMs ( 5000 ) ; pokedexModuleWithQuery . closeStream ( ) ; t . deepEqual ( [ ... pokedexModuleWithQuery . data . values ( ) ] , [ pokedex ( 6 ) , pokedex ( 38 ) , pokedex ( 78 ) ] ) ; t . is ( pokedexModule . data . size , 3 ) ; } ) ; } { const testName = 'stream: errors are thrown' ; test ( testName , async ( t ) => { const { pokedexModule } = await createMagnetarInstance ( 'read-no-access' ) ; let error ; try { const a = await pokedexModule . stream ( ) ; } catch ( _error ) { error = _error ; } t . truthy ( error ) ; } ) ; }