@@ -839,5 +839,120 @@ describe('store: [adapter=DOMAIN]', () => {
839839 globalStore . initialize ( adapter ) ( callback ) ;
840840 } ) ;
841841 } ) ;
842+
843+ it ( "should work even if the store is initialized under active domain w/o affecting the existing domain's attributes." , ( done ) => {
844+ // Existing domain.
845+ const d = domain . create ( ) as any ;
846+
847+ d . existingData = 'Hello world' ;
848+
849+ const callback = ( ) => {
850+ Promise . resolve ( ) . then ( first ) . then ( second ) . then ( third ) . then ( done ) . catch ( done ) ;
851+ } ;
852+
853+ const first = ( ) => {
854+ globalStore . set ( { foo : 'foo' } ) ;
855+ } ;
856+
857+ const second = ( ) => {
858+ // Store should still have the data set.
859+ expect ( globalStore . get ( 'foo' ) ) . to . equal ( 'foo' ) ;
860+
861+ // And the existing data in the domain before our store
862+ // was initialized should still be there.
863+ expect ( ( process . domain as any ) . existingData ) . to . equal ( 'Hello world' ) ;
864+ } ;
865+
866+ const third = ( ) => {
867+ // Ensure the same existing domain is used instead of creating a new one.
868+ expect ( process . domain ) . to . equal ( d ) ;
869+ } ;
870+
871+ d . run ( ( ) => {
872+ // Ensure data in the existing domain is available at this point.
873+ expect ( ( process . domain as any ) . existingData ) . to . equal ( 'Hello world' ) ;
874+
875+ globalStore . initialize ( adapter ) ( callback ) ;
876+ } ) ;
877+ } ) ;
878+ it ( "should work even if the store is initialized under active domain w/o affecting the existing domain's attributes." , ( done ) => {
879+ // Existing domain.
880+ const d = domain . create ( ) as any ;
881+
882+ d . existingData = 'Hello world' ;
883+
884+ const callback = ( ) => {
885+ Promise . resolve ( ) . then ( first ) . then ( second ) . then ( third ) . then ( done ) . catch ( done ) ;
886+ } ;
887+
888+ const first = ( ) => {
889+ globalStore . set ( { foo : 'foo' } ) ;
890+ } ;
891+
892+ const second = ( ) => {
893+ // Store should still have the data set.
894+ expect ( globalStore . get ( 'foo' ) ) . to . equal ( 'foo' ) ;
895+
896+ // And the existing data in the domain before our store
897+ // was initialized should still be there.
898+ expect ( ( process . domain as any ) . existingData ) . to . equal ( 'Hello world' ) ;
899+ } ;
900+
901+ const third = ( ) => {
902+ // Ensure the same existing domain is used instead of creating a new one.
903+ expect ( process . domain ) . to . equal ( d ) ;
904+ } ;
905+
906+ d . run ( ( ) => {
907+ // Ensure data in the existing domain is available at this point.
908+ expect ( ( process . domain as any ) . existingData ) . to . equal ( 'Hello world' ) ;
909+
910+ globalStore . initialize ( adapter ) ( callback ) ;
911+ } ) ;
912+ } ) ;
913+
914+ it ( 'should return the same response from the callback function.' , async ( ) => {
915+ const callback = async ( ) => {
916+ globalStore . set ( { foo : 'foo' } ) ;
917+
918+ return Promise . resolve ( 'Hello world' ) ;
919+ } ;
920+
921+ const response = await globalStore . initialize ( adapter ) ( callback ) ;
922+
923+ expect ( response ) . to . equal ( 'Hello world' ) ;
924+ } ) ;
925+ } ) ;
926+
927+ describe ( 'Error Handling:' , ( ) => {
928+ it ( 'should bubble up the promise rejection from the callback.' , async ( ) => {
929+ const callback = async ( ) => {
930+ globalStore . set ( { foo : 'foo' } ) ;
931+
932+ return Promise . reject ( 'Hello world' ) ;
933+ } ;
934+
935+ try {
936+ await globalStore . initialize ( adapter ) ( callback ) ;
937+ } catch ( e ) {
938+ expect ( e ) . to . equal ( 'Hello world' ) ;
939+ }
940+ } ) ;
941+
942+ it ( 'should bubble up the error thrown from the callback.' , async ( ) => {
943+ const callback = async ( ) => {
944+ globalStore . set ( { foo : 'foo' } ) ;
945+
946+ throw new Error ( 'Hello world' ) ;
947+ } ;
948+
949+ try {
950+ await globalStore . initialize ( adapter ) ( callback ) ;
951+ } catch ( e ) {
952+ if ( e instanceof Error ) {
953+ expect ( e . message ) . to . equal ( 'Hello world' ) ;
954+ }
955+ }
956+ } ) ;
842957 } ) ;
843958} ) ;
0 commit comments