@@ -1048,6 +1048,63 @@ describe('timeout', () => {
10481048 expect ( analytics . settings . timeout ) . toEqual ( 50 )
10491049 } )
10501050} )
1051+ describe ( 'register' , ( ) => {
1052+ it ( 'will not invoke any plugins that have initialization errors' , async ( ) => {
1053+ const analytics = AnalyticsBrowser . load ( {
1054+ writeKey,
1055+ } )
1056+
1057+ const errorPlugin = new ActionDestination ( 'Error Plugin' , {
1058+ ...googleAnalytics ,
1059+ load : ( ) => Promise . reject ( 'foo' ) ,
1060+ } )
1061+ const errorPluginSpy = jest . spyOn ( errorPlugin , 'track' )
1062+
1063+ const goodPlugin = new ActionDestination ( 'Good Plugin' , {
1064+ ...googleAnalytics ,
1065+ load : ( ) => Promise . resolve ( 'foo' ) ,
1066+ } )
1067+ const goodPluginSpy = jest . spyOn ( goodPlugin , 'track' )
1068+
1069+ await analytics . register ( goodPlugin , errorPlugin )
1070+ await errorPlugin . ready ( )
1071+ await goodPlugin . ready ( )
1072+
1073+ await analytics . track ( 'foo' )
1074+
1075+ expect ( errorPluginSpy ) . not . toHaveBeenCalled ( )
1076+ expect ( goodPluginSpy ) . toHaveBeenCalled ( )
1077+ } )
1078+
1079+ it ( 'will emit initialization errors' , async ( ) => {
1080+ const analytics = AnalyticsBrowser . load ( {
1081+ writeKey,
1082+ } )
1083+
1084+ const errorPlugin = new ActionDestination ( 'Error Plugin' , {
1085+ ...googleAnalytics ,
1086+ load : ( ) => Promise . reject ( 'foo' ) ,
1087+ } )
1088+
1089+ const goodPlugin = new ActionDestination ( 'Good Plugin' , {
1090+ ...googleAnalytics ,
1091+ load : ( ) => Promise . resolve ( 'foo' ) ,
1092+ } )
1093+
1094+ await analytics
1095+ const errorPluginName = new Promise < string > ( ( resolve ) => {
1096+ analytics . instance ?. queue . on ( 'initialization_failure' , ( plugin ) =>
1097+ resolve ( plugin . name )
1098+ )
1099+ } )
1100+
1101+ await analytics . register ( goodPlugin , errorPlugin )
1102+ await errorPlugin . ready ( )
1103+ await goodPlugin . ready ( )
1104+
1105+ expect ( await errorPluginName ) . toBe ( 'Error Plugin' )
1106+ } )
1107+ } )
10511108
10521109describe ( 'deregister' , ( ) => {
10531110 beforeEach ( async ( ) => {
0 commit comments