import test from 'ava' ; import { createMagnetarInstance } from '../helpers/createMagnetarInstance' ; import { pokedex , waitMs } from '@magnetarjs/test-utils' ; import { isPromise } from 'is-what' ; test ( 'stream (collection)' , async ( t ) => { const { pokedexModule } = createMagnetarInstance ( ) ; t . deepEqual ( pokedexModule . data . get ( '1' ) , pokedex ( 1 ) ) ; t . is ( pokedexModule . data . size , 1 ) ; t . deepEqual ( pokedexModule . streaming ( ) , null ) ; pokedexModule . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; await waitMs ( 600 ) ; t . deepEqual ( isPromise ( pokedexModule . streaming ( ) ) , true ) ; pokedexModule . closeStream ( ) ; t . deepEqual ( pokedexModule . streaming ( ) , null ) ; 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 , 3 ) ; await waitMs ( 1000 ) ; t . is ( pokedexModule . data . size , 3 ) ; } ) ; test ( 'stream (doc)' , async ( t ) => { const { trainerModule } = createMagnetarInstance ( ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 } ) ; t . deepEqual ( trainerModule . streaming ( ) , null ) ; trainerModule . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; await waitMs ( 600 ) ; t . deepEqual ( isPromise ( trainerModule . streaming ( ) ) , true ) ; trainerModule . closeStream ( ) ; t . deepEqual ( trainerModule . streaming ( ) , null ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' } ) ; await waitMs ( 1000 ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' } ) ; } ) ; test ( 'stream (collection) where-filter' , async ( t ) => { const { pokedexModule } = createMagnetarInstance ( ) ; t . is ( pokedexModule . data . size , 1 ) ; const pokedexModuleWithQuery = pokedexModule . where ( 'type' , 'array-contains' , 'Fire' ) . where ( 'base.Speed' , '>=' , 100 ) ; pokedexModuleWithQuery . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; await waitMs ( 600 ) ; t . deepEqual ( isPromise ( pokedexModuleWithQuery . streaming ( ) ) , true ) ; t . deepEqual ( pokedexModule . streaming ( ) , null ) ; pokedexModuleWithQuery . closeStream ( ) ; t . deepEqual ( [ ... pokedexModuleWithQuery . data . values ( ) ] , [ pokedex ( 6 ) , pokedex ( 38 ) , pokedex ( 78 ) ] ) ; t . is ( pokedexModule . data . size , 4 ) ; } ) ; test ( 'stream (collection) - can open multiple streams at once' , async ( t ) => { const { pokedexModule } = createMagnetarInstance ( ) ; const pokedexModuleWithQuery = pokedexModule . where ( 'type' , 'array-contains' , 'Fire' ) . where ( 'base.Speed' , '>=' , 100 ) ; pokedexModule . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; pokedexModuleWithQuery . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; await waitMs ( 1 ) ; t . deepEqual ( isPromise ( pokedexModule . streaming ( ) ) , true ) ; t . deepEqual ( isPromise ( pokedexModuleWithQuery . streaming ( ) ) , true ) ; pokedexModule . closeAllStreams ( ) ; t . deepEqual ( pokedexModule . streaming ( ) , null ) ; t . deepEqual ( pokedexModuleWithQuery . streaming ( ) , null ) ; } ) ;