@@ -45,59 +45,67 @@ afterEach(() => {
4545} ) ;
4646
4747describe ( 'ScriptManager hooks' , ( ) => {
48- it ( 'should sequentially call hooks in series' , async ( ) => {
49- const executionOrder : string [ ] = [ ] ;
50- [ 'first' , 'second' , 'third' ] . forEach ( ( prefix ) => {
51- ScriptManager . shared . hooks . beforeResolve ( async ( args ) => {
52- executionOrder . push ( `${ prefix } -beforeResolve` ) ;
53- return {
54- options : {
55- ...args . options ,
56- scriptId : `${ prefix } -${ args . options . scriptId } ` ,
57- } ,
58- } ;
48+ it . each ( [
49+ [ 'beforeResolve' , 'resolveScript' ] ,
50+ [ 'afterResolve' , 'resolveScript' ] ,
51+ [ 'beforeLoad' , 'loadScript' ] ,
52+ [ 'afterLoad' , 'loadScript' ] ,
53+ ] as const ) (
54+ 'should sequentially call hook in series - %s' ,
55+ async ( hookName , methodName ) => {
56+ const executionOrder : string [ ] = [ ] ;
57+ [ 'first' , 'second' , 'third' ] . forEach ( ( prefix ) => {
58+ ScriptManager . shared . hooks [ hookName ] ( async ( args ) => {
59+ executionOrder . push ( `${ prefix } -${ hookName } ` ) ;
60+ return args as any ;
61+ } ) ;
5962 } ) ;
60- } ) ;
61-
62- ScriptManager . shared . addResolver ( async ( scriptId ) => {
63- return { url : Script . getRemoteURL ( `http://domain.ext/${ scriptId } ` ) } ;
64- } ) ;
65-
66- await ScriptManager . shared . resolveScript ( 'test-script' , 'test-caller' ) ;
67-
68- expect ( executionOrder ) . toEqual ( [
69- 'first-beforeResolve' ,
70- 'second-beforeResolve' ,
71- 'third-beforeResolve' ,
72- ] ) ;
73- } ) ;
7463
75- it ( 'should pass args between hooks using waterfall pattern' , async ( ) => {
76- [ 'first' , 'second' , 'third' ] . forEach ( ( prefix ) => {
77- ScriptManager . shared . hooks . beforeResolve ( async ( args ) => {
78- return {
79- options : {
80- ...args . options ,
81- scriptId : `${ prefix } -${ args . options . scriptId } ` ,
82- } ,
83- } ;
64+ ScriptManager . shared . addResolver ( async ( scriptId ) => {
65+ return { url : Script . getRemoteURL ( `http://domain.ext/${ scriptId } ` ) } ;
66+ } ) ;
67+
68+ await ScriptManager . shared [ methodName ] ( 'test-script' , 'test-caller' ) ;
69+
70+ expect ( executionOrder ) . toEqual ( [
71+ `first-${ hookName } ` ,
72+ `second-${ hookName } ` ,
73+ `third-${ hookName } ` ,
74+ ] ) ;
75+ }
76+ ) ;
77+
78+ it . each ( [
79+ [ 'beforeResolve' , 'resolveScript' ] ,
80+ [ 'afterResolve' , 'resolveScript' ] ,
81+ [ 'beforeLoad' , 'loadScript' ] ,
82+ [ 'afterLoad' , 'loadScript' ] ,
83+ ] as const ) (
84+ 'should pass args between hooks using waterfall pattern - %s' ,
85+ async ( hookName , methodName ) => {
86+ let testScriptId : string ;
87+ let testCaller : string ;
88+
89+ [ 'first' , 'second' , 'third' ] . forEach ( ( prefix ) => {
90+ ScriptManager . shared . hooks [ hookName ] ( async ( args ) => {
91+ args . options . scriptId =
92+ testScriptId = `${ prefix } -${ args . options . scriptId } ` ;
93+ args . options . caller = testCaller = `${ prefix } -${ args . options . caller } ` ;
94+
95+ return args as any ;
96+ } ) ;
8497 } ) ;
85- } ) ;
8698
87- ScriptManager . shared . addResolver ( async ( scriptId ) => {
88- return { url : Script . getRemoteURL ( `http://domain.ext/${ scriptId } ` ) } ;
89- } ) ;
99+ ScriptManager . shared . addResolver ( async ( scriptId ) => {
100+ return { url : Script . getRemoteURL ( `http://domain.ext/${ scriptId } ` ) } ;
101+ } ) ;
90102
91- const script = await ScriptManager . shared . resolveScript (
92- 'test-script' ,
93- 'test-caller'
94- ) ;
103+ await ScriptManager . shared [ methodName ] ( 'test-script' , 'test-caller' ) ;
95104
96- expect ( script . scriptId ) . toBe ( 'third-second-first-test-script' ) ;
97- expect ( script . locator . url ) . toBe (
98- 'http://domain.ext/third-second-first-test-script.chunk.bundle'
99- ) ;
100- } ) ;
105+ expect ( testScriptId ! ) . toBe ( 'third-second-first-test-script' ) ;
106+ expect ( testCaller ! ) . toBe ( 'third-second-first-test-caller' ) ;
107+ }
108+ ) ;
101109
102110 describe ( 'resolve hooks' , ( ) => {
103111 it ( 'should call resolve hooks in correct lifecycle order' , async ( ) => {
0 commit comments