@@ -10,7 +10,7 @@ import {
1010 APP_READY ,
1111 APP_INIT_ERROR ,
1212} from './constants' ;
13- import { initialize } from './initialize' ;
13+ import { initialize , loadExternalScripts } from './initialize' ;
1414import { subscribe } from './pubSub' ;
1515
1616import {
@@ -352,6 +352,63 @@ describe('initialize', () => {
352352 expect ( hydrateAuthenticatedUser ) . not . toHaveBeenCalled ( ) ;
353353 expect ( logError ) . not . toHaveBeenCalled ( ) ;
354354 } ) ;
355+
356+ it ( 'should load externalScripts from config when provided' , async ( ) => {
357+ const mockScript = jest . fn ( ) . mockImplementation ( ( ) => ( { loadScript : jest . fn ( ) } ) ) ;
358+ config . externalScripts = [ mockScript ] ;
359+
360+ await initialize ( { messages : null } ) ;
361+
362+ expect ( mockScript ) . toHaveBeenCalledWith ( { config } ) ;
363+ delete config . externalScripts ;
364+ } ) ;
365+
366+ it ( 'should fall back to externalScripts parameter when config has none' , async ( ) => {
367+ const mockScript = jest . fn ( ) . mockImplementation ( ( ) => ( { loadScript : jest . fn ( ) } ) ) ;
368+
369+ await initialize ( { messages : null , externalScripts : [ mockScript ] } ) ;
370+
371+ expect ( mockScript ) . toHaveBeenCalledWith ( { config } ) ;
372+ } ) ;
373+
374+ it ( 'should load GoogleAnalyticsLoader by default' , async ( ) => {
375+ const { GoogleAnalyticsLoader : GALoader } = jest . requireActual ( './scripts' ) ;
376+ const loadScriptSpy = jest . spyOn ( GALoader . prototype , 'loadScript' ) . mockImplementation ( ( ) => { } ) ;
377+
378+ await initialize ( { messages : null } ) ;
379+
380+ expect ( loadScriptSpy ) . toHaveBeenCalled ( ) ;
381+ loadScriptSpy . mockRestore ( ) ;
382+ } ) ;
383+
384+ it ( 'should prefer config externalScripts over the parameter' , async ( ) => {
385+ const configScript = jest . fn ( ) . mockImplementation ( ( ) => ( { loadScript : jest . fn ( ) } ) ) ;
386+ const paramScript = jest . fn ( ) . mockImplementation ( ( ) => ( { loadScript : jest . fn ( ) } ) ) ;
387+ config . externalScripts = [ configScript ] ;
388+
389+ await initialize ( { messages : null , externalScripts : [ paramScript ] } ) ;
390+
391+ expect ( configScript ) . toHaveBeenCalled ( ) ;
392+ expect ( paramScript ) . not . toHaveBeenCalled ( ) ;
393+ delete config . externalScripts ;
394+ } ) ;
395+ } ) ;
396+
397+ describe ( 'loadExternalScripts' , ( ) => {
398+ it ( 'should instantiate each script with data and call loadScript' , ( ) => {
399+ const loadScript = jest . fn ( ) ;
400+ const MockScript = jest . fn ( ) . mockImplementation ( ( ) => ( { loadScript } ) ) ;
401+ const data = { config : { some : 'value' } } ;
402+
403+ loadExternalScripts ( [ MockScript ] , data ) ;
404+
405+ expect ( MockScript ) . toHaveBeenCalledWith ( data ) ;
406+ expect ( loadScript ) . toHaveBeenCalled ( ) ;
407+ } ) ;
408+
409+ it ( 'should handle an empty array' , ( ) => {
410+ expect ( ( ) => loadExternalScripts ( [ ] , { } ) ) . not . toThrow ( ) ;
411+ } ) ;
355412} ) ;
356413
357414describe ( 'history' , ( ) => {
0 commit comments