@@ -878,6 +878,116 @@ describe('addDestinationMiddleware', () => {
878878 } )
879879 } )
880880
881+ it ( 'drops events if next is never called' , async ( ) => {
882+ const testPlugin : Plugin = {
883+ name : 'test' ,
884+ type : 'destination' ,
885+ version : '0.1.0' ,
886+ load : ( ) => Promise . resolve ( ) ,
887+ track : jest . fn ( ) ,
888+ isLoaded : ( ) => true ,
889+ }
890+
891+ const [ analytics ] = await AnalyticsBrowser . load ( {
892+ writeKey,
893+ } )
894+
895+ const fullstory = new ActionDestination ( 'fullstory' , testPlugin )
896+
897+ await analytics . register ( fullstory )
898+ await fullstory . ready ( )
899+ analytics . addDestinationMiddleware ( 'fullstory' , ( ) => {
900+ // do nothing
901+ } )
902+
903+ await analytics . track ( 'foo' )
904+
905+ expect ( testPlugin . track ) . not . toHaveBeenCalled ( )
906+ } )
907+
908+ it ( 'drops events if next is called with null' , async ( ) => {
909+ const testPlugin : Plugin = {
910+ name : 'test' ,
911+ type : 'destination' ,
912+ version : '0.1.0' ,
913+ load : ( ) => Promise . resolve ( ) ,
914+ track : jest . fn ( ) ,
915+ isLoaded : ( ) => true ,
916+ }
917+
918+ const [ analytics ] = await AnalyticsBrowser . load ( {
919+ writeKey,
920+ } )
921+
922+ const fullstory = new ActionDestination ( 'fullstory' , testPlugin )
923+
924+ await analytics . register ( fullstory )
925+ await fullstory . ready ( )
926+ analytics . addDestinationMiddleware ( 'fullstory' , ( { next } ) => {
927+ next ( null )
928+ } )
929+
930+ await analytics . track ( 'foo' )
931+
932+ expect ( testPlugin . track ) . not . toHaveBeenCalled ( )
933+ } )
934+
935+ it ( 'applies to all destinations if * glob is passed as name argument' , async ( ) => {
936+ const [ analytics ] = await AnalyticsBrowser . load ( {
937+ writeKey,
938+ } )
939+
940+ const p1 = new ActionDestination ( 'p1' , { ...googleAnalytics } )
941+ const p2 = new ActionDestination ( 'p2' , { ...amplitude } )
942+
943+ await analytics . register ( p1 , p2 )
944+ await p1 . ready ( )
945+ await p2 . ready ( )
946+
947+ const middleware = jest . fn ( )
948+
949+ analytics . addDestinationMiddleware ( '*' , middleware )
950+ await analytics . track ( 'foo' )
951+
952+ expect ( middleware ) . toHaveBeenCalledTimes ( 2 )
953+ expect ( middleware ) . toHaveBeenCalledWith (
954+ expect . objectContaining ( { integration : 'p1' } )
955+ )
956+ expect ( middleware ) . toHaveBeenCalledWith (
957+ expect . objectContaining ( { integration : 'p2' } )
958+ )
959+ } )
960+
961+ it ( 'middleware is only applied to type: destination plugins' , async ( ) => {
962+ const [ analytics ] = await AnalyticsBrowser . load ( {
963+ writeKey,
964+ } )
965+
966+ const utilityPlugin = new ActionDestination ( 'p1' , {
967+ ...xt ,
968+ type : 'utility' ,
969+ } )
970+
971+ const destinationPlugin = new ActionDestination ( 'p2' , {
972+ ...xt ,
973+ type : 'destination' ,
974+ } )
975+
976+ await analytics . register ( utilityPlugin , destinationPlugin )
977+ await utilityPlugin . ready ( )
978+ await destinationPlugin . ready ( )
979+
980+ const middleware = jest . fn ( )
981+
982+ analytics . addDestinationMiddleware ( '*' , middleware )
983+ await analytics . track ( 'foo' )
984+
985+ expect ( middleware ) . toHaveBeenCalledTimes ( 1 )
986+ expect ( middleware ) . toHaveBeenCalledWith (
987+ expect . objectContaining ( { integration : 'p2' } )
988+ )
989+ } )
990+
881991 it ( 'supports registering action destination middlewares' , async ( ) => {
882992 const testPlugin : Plugin = {
883993 name : 'test' ,
0 commit comments