import test from 'ava' ; import { createMagnetarInstance } from '../helpers/createMagnetarInstance' ; import { pokedex } from '@magnetarjs/test-utils' ; import { firestoreDeepEqual } from '../helpers/firestoreDeepEqual' ; { const testName = 'revert: (remote → local) insert (document)' ; test ( testName , async ( t ) => { const { pokedexModule } = await createMagnetarInstance ( testName ) ; const payload = { ... pokedex ( 7 ) , shouldFail : 'local' } ; t . deepEqual ( pokedexModule . doc ( '7' ) . data , undefined ) ; const squirtle = pokedexModule . doc ( '7' ) ; try { await squirtle . insert ( payload , { onError : 'revert' , executionOrder : [ 'remote' , 'local' ] , on : { success : async ( { storeName } ) => { if ( storeName !== 'remote' ) return ; await firestoreDeepEqual ( t , testName , 'pokedex/7' , payload , 'should exist' ) ; } , } , } ) ; } catch ( error ) { t . truthy ( error ) ; } const expected = undefined ; t . deepEqual ( pokedexModule . doc ( '7' ) . data , expected as any ) ; await firestoreDeepEqual ( t , testName , 'pokedex/7' , expected , 'should be deleted' ) ; } ) ; } { const testName = 'revert: (remote → local) insert (collection) → random ID' ; test ( testName , async ( t ) => { const { pokedexModule } = await createMagnetarInstance ( testName ) ; const payload = { ... pokedex ( 7 ) , shouldFail : 'local' } ; try { await pokedexModule . insert ( payload , { onError : 'revert' , executionOrder : [ 'remote' , 'local' ] , on : { success : async ( { storeName } ) => { if ( storeName !== 'remote' ) return ; await firestoreDeepEqual ( t , testName , 'pokedex/7' , payload , 'should exist' ) ; } , } , } ) ; t . fail ( ) ; } catch ( error ) { t . truthy ( error ) ; } const expected = undefined ; t . deepEqual ( pokedexModule . doc ( '7' ) . data , expected as any ) ; await firestoreDeepEqual ( t , testName , 'pokedex/7' , expected , 'should be deleted' ) ; } ) ; } { const testName = 'revert: merge (with extra checks)' ; test ( testName , async ( t ) => { const { pokedexModule } = await createMagnetarInstance ( testName , { insertDocs : { 'pokedex/1' : pokedex ( 1 ) } , } ) ; await firestoreDeepEqual ( t , testName , 'pokedex/1' , pokedex ( 1 ) ) ; const payload = { base : { HP : undefined } } ; t . deepEqual ( pokedexModule . doc ( '1' ) . data , pokedex ( 1 ) ) ; try { await pokedexModule . doc ( '1' ) . merge ( payload , { onError : 'revert' , on : { success : ( { storeName } ) => { if ( storeName !== 'local' ) return ; const expectedMidway = { id : 1 , name : 'Bulbasaur' , type : [ 'Grass' , 'Poison' ] , base : { 'HP' : undefined , 'Attack' : 49 , 'Defense' : 49 , 'SpAttack' : 65 , 'SpDefense' : 65 , 'Speed' : 45 , } , } ; t . deepEqual ( pokedexModule . doc ( '1' ) . data , expectedMidway as any ) ; } , } , } ) ; } catch ( error ) { t . truthy ( error ) ; } const expected = pokedex ( 1 ) ; t . deepEqual ( pokedexModule . doc ( '1' ) . data , expected as any ) ; await firestoreDeepEqual ( t , testName , 'pokedex/1' , expected as any ) ; } ) ; } { const testName = 'revert: assign (with extra checks)' ; test ( testName , async ( t ) => { const { pokedexModule } = await createMagnetarInstance ( testName , { insertDocs : { 'pokedex/1' : pokedex ( 1 ) } , } ) ; await firestoreDeepEqual ( t , testName , 'pokedex/1' , pokedex ( 1 ) ) ; const payload = { base : { HP : undefined } } ; t . deepEqual ( pokedexModule . doc ( '1' ) . data , pokedex ( 1 ) ) ; try { await pokedexModule . doc ( '1' ) . assign ( payload , { onError : 'revert' , on : { success : ( { storeName } ) => { if ( storeName !== 'local' ) return ; const expectedMidway = { id : 1 , name : 'Bulbasaur' , type : [ 'Grass' , 'Poison' ] , base : { HP : undefined , } , } ; t . deepEqual ( pokedexModule . doc ( '1' ) . data , expectedMidway as any ) ; } , } , } ) ; } catch ( error ) { t . truthy ( error ) ; } const expected = pokedex ( 1 ) ; t . deepEqual ( pokedexModule . doc ( '1' ) . data , expected as any ) ; await firestoreDeepEqual ( t , testName , 'pokedex/1' , expected as any ) ; } ) ; } { const testName = 'revert: replace (with extra checks)' ; test ( testName , async ( t ) => { const { pokedexModule } = await createMagnetarInstance ( testName , { insertDocs : { 'pokedex/1' : pokedex ( 1 ) } , } ) ; await firestoreDeepEqual ( t , testName , 'pokedex/1' , pokedex ( 1 ) ) ; const payload = { base : { HP : undefined } } ; t . deepEqual ( pokedexModule . doc ( '1' ) . data , pokedex ( 1 ) ) ; try { await pokedexModule . doc ( '1' ) . replace ( payload , { onError : 'revert' , on : { success : ( { storeName } ) => { if ( storeName !== 'local' ) return ; const expectedMidway = { base : { HP : undefined } } ; t . deepEqual ( pokedexModule . doc ( '1' ) . data , expectedMidway as any ) ; } , } , } ) ; } catch ( error ) { t . truthy ( error ) ; } const expected = pokedex ( 1 ) ; t . deepEqual ( pokedexModule . doc ( '1' ) . data , expected as any ) ; await firestoreDeepEqual ( t , testName , 'pokedex/1' , expected as any ) ; } ) ; }