@@ -839,5 +839,88 @@ describe('store: [adapter=DOMAIN]', () => {
839839 globalStore . initialize ( adapter ) ( callback ) ;
840840 } ) ;
841841 } ) ;
842+
843+ it ( 'should return the response from callback function.' , ( done ) => {
844+ const callback = ( ) => {
845+ globalStore . set ( { foo : 'foo' } ) ;
846+
847+ return functionAccessingStore ( ) ;
848+ } ;
849+
850+ const functionAccessingStore = ( ) => {
851+ return globalStore . get ( 'foo' ) ;
852+ } ;
853+
854+ const response = globalStore . initialize ( adapter ) ( callback ) ;
855+ expect ( response ) . to . equal ( 'foo' ) ;
856+
857+ done ( ) ;
858+ } ) ;
859+
860+ it ( 'should return the response from async callback function.' , async ( ) => {
861+ const callback = async ( ) => {
862+ globalStore . set ( { foo : 'foo' } ) ;
863+
864+ functionAccessingStore ( ) ;
865+ const response = await asyncTask ( ) ;
866+
867+ return response ;
868+ } ;
869+
870+ const functionAccessingStore = ( ) => {
871+ expect ( globalStore . get ( 'foo' ) ) . to . equal ( 'foo' ) ;
872+ } ;
873+
874+ const asyncTask = ( ) => {
875+ return new Promise ( ( resolve ) => {
876+ setTimeout ( ( ) => {
877+ resolve ( globalStore . get ( 'foo' ) ) ;
878+ } , 1 ) ;
879+ } ) ;
880+ } ;
881+
882+ const response = await globalStore . initialize ( adapter ) ( callback ) ;
883+ expect ( response ) . to . equal ( 'foo' ) ;
884+ } ) ;
885+ } ) ;
886+
887+ describe ( 'Error Handling:' , ( ) => {
888+ it ( 'should bubble up the promise rejection from the callback.' , async ( ) => {
889+ const callback = ( ) => {
890+ globalStore . set ( { foo : 'foo' } ) ;
891+
892+ return new Promise ( ( resolve , reject ) => {
893+ setTimeout ( ( ) => {
894+ reject ( 'Hello world' ) ;
895+ } , 1 ) ;
896+ } ) ;
897+ } ;
898+
899+ try {
900+ await globalStore . initialize ( adapter ) ( callback ) ;
901+ expect . fail ( 'Should not reach here.' ) ;
902+ } catch ( e ) {
903+ expect ( e ) . to . equal ( 'Hello world' ) ;
904+ }
905+ } ) ;
906+
907+ it ( 'should bubble up the error thrown from the callback.' , ( done ) => {
908+ const callback = ( ) => {
909+ globalStore . set ( { foo : 'foo' } ) ;
910+
911+ throw new Error ( 'Hello world' ) ;
912+ } ;
913+
914+ try {
915+ globalStore . initialize ( adapter ) ( callback ) ;
916+ expect . fail ( 'Should not reach here.' ) ;
917+ } catch ( e ) {
918+ if ( e instanceof Error ) {
919+ expect ( e . message ) . to . equal ( 'Hello world' ) ;
920+ }
921+ }
922+
923+ done ( ) ;
924+ } ) ;
842925 } ) ;
843926} ) ;
0 commit comments