@@ -123,6 +123,32 @@ describe('createScript', () => {
123
123
124
124
expect ( clearTimeout ) . toHaveBeenCalled ( ) ;
125
125
} ) ;
126
+
127
+ it ( 'should set section attributes on the script element' , ( ) => {
128
+ const url = 'https://example.com/script.js' ;
129
+ const cb = jest . fn ( ) ;
130
+ const attrs = {
131
+ async : true ,
132
+ 'data-test' : 'test' ,
133
+ crossOrigin : 'anonymous' ,
134
+ } ;
135
+ const { script } = createScript ( {
136
+ url,
137
+ cb,
138
+ attrs,
139
+ createScriptHook : ( url ) => {
140
+ const scriptEle = document . createElement ( 'script' ) ;
141
+ scriptEle . src = url ;
142
+ scriptEle . crossOrigin = 'use-credentials' ;
143
+ scriptEle . async = false ;
144
+ return scriptEle ;
145
+ } ,
146
+ } ) ;
147
+
148
+ expect ( script . async ) . toBe ( true ) ;
149
+ expect ( script . crossOrigin ) . toBe ( 'use-credentials' ) ;
150
+ expect ( script . getAttribute ( 'data-test' ) ) . toBe ( 'test' ) ;
151
+ } ) ;
126
152
} ) ;
127
153
} ) ;
128
154
@@ -175,6 +201,33 @@ describe('createLink', () => {
175
201
expect ( link . getAttribute ( 'data-test' ) ) . toBe ( 'test' ) ;
176
202
} ) ;
177
203
204
+ it ( 'should set section attributes on the link element' , ( ) => {
205
+ const url = 'https://example.com/script.js' ;
206
+ const cb = jest . fn ( ) ;
207
+ const attrs = {
208
+ rel : 'preload' ,
209
+ as : 'script' ,
210
+ 'data-test' : 'test' ,
211
+ crossOrigin : 'anonymous' ,
212
+ } ;
213
+ const { link } = createLink ( {
214
+ url,
215
+ cb,
216
+ attrs,
217
+ createLinkHook : ( url ) => {
218
+ const linkEle = document . createElement ( 'link' ) ;
219
+ linkEle . href = url ;
220
+ linkEle . crossOrigin = 'use-credentials' ;
221
+ return linkEle ;
222
+ } ,
223
+ } ) ;
224
+
225
+ expect ( link . rel ) . toBe ( 'preload' ) ;
226
+ expect ( link . crossOrigin ) . toBe ( 'use-credentials' ) ;
227
+ expect ( link . getAttribute ( 'as' ) ) . toBe ( 'script' ) ;
228
+ expect ( link . getAttribute ( 'data-test' ) ) . toBe ( 'test' ) ;
229
+ } ) ;
230
+
178
231
it ( 'should call the callback when the link loads' , ( ) => {
179
232
const url = 'https://example.com/script.js' ;
180
233
const cb = jest . fn ( ) ;
0 commit comments