import test from 'ava' ; import { createMagnetarInstance } from '../helpers/createMagnetarInstance' ; import { pokedex , PokedexEntry } from '@magnetarjs/test-utils' ; import { merge } from 'merge-anything' ; import { DocInstance } from '../../src' ; test ( 'write: insert (document)' , async ( t ) => { const { pokedexModule , magnetar } = createMagnetarInstance ( ) ; const payload = pokedex ( 7 ) ; t . deepEqual ( pokedexModule . data . get ( '7' ) , undefined ) ; await pokedexModule . doc ( '7' ) . insert ( payload ) . catch ( ( e ) => t . fail ( e . message ) ) ; t . deepEqual ( pokedexModule . data . get ( '7' ) , payload ) ; t . deepEqual ( pokedexModule . doc ( '7' ) . data , payload ) ; t . deepEqual ( magnetar . doc ( 'pokedex/7' ) . data , payload ) ; t . deepEqual ( magnetar . collection ( 'pokedex' ) . doc ( '7' ) . data , payload ) ; } ) ; test ( 'write: insert (collection) → random ID' , async ( t ) => { const { pokedexModule , magnetar } = createMagnetarInstance ( ) ; const payload = pokedex ( 7 ) ; let moduleFromResult ; try { moduleFromResult = await pokedexModule . insert ( payload ) ; } catch ( error ) { t . fail ( JSON . stringify ( error ) ) ; return ; } const newId = moduleFromResult . id ; t . deepEqual ( moduleFromResult . data , payload ) ; t . deepEqual ( pokedexModule . data . get ( newId ) , payload ) ; t . deepEqual ( magnetar . doc ( `pokedex/${ newId }` ) . data , payload ) ; t . deepEqual ( magnetar . collection ( 'pokedex' ) . doc ( newId ) . data , payload ) ; t . deepEqual ( pokedexModule . doc ( newId ) . data , payload ) ; } ) ; test ( 'deleteProp: (document)' , async ( t ) => { const { trainerModule , magnetar } = createMagnetarInstance ( ) ; const prop = 'age' ; t . deepEqual ( trainerModule . data ?. age , 10 ) ; const magnetarDoc = magnetar . doc ( 'app-data/trainer' ) ; const magnetarCollectionDoc = magnetar . collection ( 'app-data' ) . doc ( 'trainer' ) ; const magnetarCollectionData = magnetar . collection ( 'app-data' ) . data ; try { const result = await trainerModule . deleteProp ( prop ) ; t . deepEqual ( result , trainerModule . data as any ) ; } catch ( error ) { t . fail ( JSON . stringify ( error ) ) ; } t . deepEqual ( magnetarDoc . data ?. [ prop ] , undefined ) ; t . deepEqual ( magnetarCollectionDoc . data ?. [ prop ] , undefined ) ; t . deepEqual ( magnetarCollectionData . get ( 'trainer' ) ?. [ prop ] , undefined ) ; t . deepEqual ( trainerModule . data ?. [ prop ] , undefined ) ; } ) ; test ( 'delete: (document)' , async ( t ) => { const { trainerModule , magnetar } = createMagnetarInstance ( ) ; t . deepEqual ( trainerModule . data , { age : 10 , name : 'Luca' } ) ; const magnetarCollection = magnetar . collection ( 'app-data' ) ; const magnetarCollectionDoc = magnetar . collection ( 'app-data' ) . doc ( 'trainer' ) ; const magnetarDoc = magnetar . doc ( 'app-data/trainer' ) ; try { const result = await trainerModule . delete ( ) ; t . deepEqual ( result , undefined ) ; } catch ( error ) { t . fail ( JSON . stringify ( error ) ) ; } t . deepEqual ( magnetarCollection . data . get ( 'trainer' ) , undefined ) ; t . deepEqual ( magnetarCollectionDoc . data , undefined ) ; t . deepEqual ( magnetarDoc . data , undefined ) ; t . deepEqual ( trainerModule . data , undefined ) ; t . deepEqual ( magnetar . collection ( 'app-data' ) . doc ( 'trainer' ) . data , undefined ) ; t . deepEqual ( magnetar . doc ( 'app-data/trainer' ) . data , undefined ) ; t . deepEqual ( magnetar . collection ( 'app-data' ) . data . get ( 'trainer' ) , undefined ) ; } ) ; test ( 'write: merge (document)' , async ( t ) => { const { pokedexModule } = createMagnetarInstance ( ) ; const payload = { base : { HP : 9000 } } ; const doc = pokedexModule . doc ( '1' ) ; t . deepEqual ( doc . data , pokedex ( 1 ) ) ; await doc . merge ( payload ) . catch ( ( e ) => t . fail ( e . message ) ) ; const mergedResult = merge ( pokedex ( 1 ) , { base : { HP : 9000 } } ) ; t . deepEqual ( pokedexModule . data . get ( '1' ) , mergedResult ) ; t . deepEqual ( doc . data , mergedResult ) ; } ) ; test ( 'read: fetch (collection)' , async ( t ) => { const { pokedexModule } = createMagnetarInstance ( ) ; t . deepEqual ( pokedexModule . data . get ( '1' ) , pokedex ( 1 ) ) ; t . is ( pokedexModule . data . size , 1 ) ; try { const result = await pokedexModule . fetch ( { force : true } ) ; t . deepEqual ( result . get ( '1' ) , pokedex ( 1 ) ) ; t . deepEqual ( result . get ( '136' ) , pokedex ( 136 ) ) ; } catch ( error ) { t . fail ( JSON . stringify ( error ) ) ; } t . deepEqual ( pokedexModule . data . get ( '1' ) , pokedex ( 1 ) ) ; t . deepEqual ( pokedexModule . data . get ( '136' ) , pokedex ( 136 ) ) ; t . is ( pokedexModule . data . size , 151 ) ; } ) ; test ( 'read: fetch (document)' , async ( t ) => { const { trainerModule } = createMagnetarInstance ( ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 } ) ; try { const result = await trainerModule . fetch ( { force : true } ) ; t . deepEqual ( result , { name : 'Luca' , age : 10 , dream : 'job' } ) ; } catch ( error ) { t . fail ( JSON . stringify ( error ) ) ; } t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' } ) ; } ) ; test ( 'fetch (collection) where-filter: ==' , async ( t ) => { const { pokedexModule , magnetar } = createMagnetarInstance ( ) ; const pokedexModuleWithQuery = pokedexModule . where ( 'name' , '==' , 'Flareon' ) ; try { const queryModuleRef = pokedexModuleWithQuery ; await queryModuleRef . fetch ( { force : true } ) ; t . deepEqual ( [ ... queryModuleRef . data . values ( ) ] , [ pokedex ( 136 ) ] ) ; } catch ( error ) { t . fail ( JSON . stringify ( error ) ) ; } const queryModuleRef = pokedexModule . where ( 'name' , '==' , 'Flareon' ) ; t . deepEqual ( [ ... queryModuleRef . data . values ( ) ] , [ pokedex ( 136 ) ] ) ; t . deepEqual ( [ ... pokedexModuleWithQuery . data . values ( ) ] , [ pokedex ( 136 ) ] ) ; t . deepEqual ( pokedexModuleWithQuery . doc ( '136' ) . data , pokedex ( 136 ) ) ; t . deepEqual ( pokedexModule . doc ( '136' ) . data , pokedex ( 136 ) ) ; t . deepEqual ( magnetar . doc ( 'pokedex/136' ) . data , pokedex ( 136 ) ) ; t . deepEqual ( [ ... pokedexModule . data . values ( ) ] , [ pokedex ( 1 ) , pokedex ( 136 ) ] ) ; } ) ;