@@ -8,7 +8,7 @@ describe('createScript', () => {
8
8
it ( 'should create a new script element if one does not exist' , ( ) => {
9
9
const url = 'https://example.com/script.js' ;
10
10
const cb = jest . fn ( ) ;
11
- const { script, needAttach } = createScript ( url , cb ) ;
11
+ const { script, needAttach } = createScript ( { url, cb } ) ;
12
12
13
13
expect ( script . tagName ) . toBe ( 'SCRIPT' ) ;
14
14
expect ( script . src ) . toBe ( url ) ;
@@ -19,7 +19,7 @@ describe('createScript', () => {
19
19
const url = 'https://example.com/script.js' ;
20
20
const cb = jest . fn ( ) ;
21
21
document . body . innerHTML = `<script src="${ url } "></script>` ;
22
- const { script, needAttach } = createScript ( url , cb ) ;
22
+ const { script, needAttach } = createScript ( { url, cb } ) ;
23
23
24
24
expect ( script . tagName ) . toBe ( 'SCRIPT' ) ;
25
25
expect ( script . src ) . toBe ( url ) ;
@@ -30,7 +30,7 @@ describe('createScript', () => {
30
30
const url = 'https://example.com/script.js' ;
31
31
const cb = jest . fn ( ) ;
32
32
const attrs = { async : true , 'data-test' : 'test' } ;
33
- const { script } = createScript ( url , cb , attrs ) ;
33
+ const { script } = createScript ( { url, cb, attrs } ) ;
34
34
35
35
expect ( script . async ) . toBe ( true ) ;
36
36
expect ( script . getAttribute ( 'data-test' ) ) . toBe ( 'test' ) ;
@@ -39,7 +39,7 @@ describe('createScript', () => {
39
39
it ( 'should call the callback when the script loads' , ( ) => {
40
40
const url = 'https://example.com/script.js' ;
41
41
const cb = jest . fn ( ) ;
42
- const { script, needAttach } = createScript ( url , cb ) ;
42
+ const { script, needAttach } = createScript ( { url, cb } ) ;
43
43
44
44
if ( needAttach ) {
45
45
document . body . appendChild ( script ) ;
@@ -52,7 +52,12 @@ describe('createScript', () => {
52
52
it ( 'should call the callback when the script times out' , ( ) => {
53
53
const url = 'https://example.com/script.js' ;
54
54
const cb = jest . fn ( ) ;
55
- createScript ( url , cb , { } , ( ) => ( { timeout : 100 } ) ) ;
55
+ createScript ( {
56
+ url,
57
+ cb,
58
+ attrs : { } ,
59
+ createScriptHook : ( ) => ( { timeout : 100 } ) ,
60
+ } ) ;
56
61
57
62
setTimeout ( ( ) => {
58
63
expect ( cb ) . toHaveBeenCalled ( ) ;
@@ -71,7 +76,7 @@ describe('createScript', () => {
71
76
it ( 'should use the default timeout of 20000ms if no timeout is specified' , ( ) => {
72
77
const url = 'https://example.com/script.js' ;
73
78
const cb = jest . fn ( ) ;
74
- const { script } = createScript ( url , cb ) ;
79
+ const { script } = createScript ( { url, cb } ) ;
75
80
76
81
expect ( setTimeout ) . toHaveBeenCalledWith ( expect . any ( Function ) , 20000 ) ;
77
82
} ) ;
@@ -80,7 +85,12 @@ describe('createScript', () => {
80
85
const url = 'https://example.com/script.js' ;
81
86
const cb = jest . fn ( ) ;
82
87
const customTimeout = 5000 ;
83
- createScript ( url , cb , { } , ( ) => ( { timeout : customTimeout } ) ) ;
88
+ createScript ( {
89
+ url,
90
+ cb,
91
+ attrs : { } ,
92
+ createScriptHook : ( ) => ( { timeout : customTimeout } ) ,
93
+ } ) ;
84
94
85
95
expect ( setTimeout ) . toHaveBeenCalledWith (
86
96
expect . any ( Function ) ,
@@ -91,7 +101,7 @@ describe('createScript', () => {
91
101
it ( 'should clear the timeout when the script loads successfully' , ( ) => {
92
102
const url = 'https://example.com/script.js' ;
93
103
const cb = jest . fn ( ) ;
94
- const { script, needAttach } = createScript ( url , cb ) ;
104
+ const { script, needAttach } = createScript ( { url, cb } ) ;
95
105
96
106
if ( needAttach ) {
97
107
document . body . appendChild ( script ) ;
@@ -104,7 +114,7 @@ describe('createScript', () => {
104
114
it ( 'should clear the timeout when the script fails to load' , ( ) => {
105
115
const url = 'https://example.com/script.js' ;
106
116
const cb = jest . fn ( ) ;
107
- const { script, needAttach } = createScript ( url , cb ) ;
117
+ const { script, needAttach } = createScript ( { url, cb } ) ;
108
118
109
119
if ( needAttach ) {
110
120
document . body . appendChild ( script ) ;
@@ -124,7 +134,11 @@ describe('createLink', () => {
124
134
it ( 'should create a new link element if one does not exist' , ( ) => {
125
135
const url = 'https://example.com/script.js' ;
126
136
const cb = jest . fn ( ) ;
127
- const { link, needAttach } = createLink ( url , cb , { as : 'script' } ) ;
137
+ const { link, needAttach } = createLink ( {
138
+ url,
139
+ cb,
140
+ attrs : { as : 'script' } ,
141
+ } ) ;
128
142
129
143
expect ( link . tagName ) . toBe ( 'LINK' ) ;
130
144
expect ( link . href ) . toBe ( url ) ;
@@ -136,9 +150,13 @@ describe('createLink', () => {
136
150
const url = 'https://example.com/script.js' ;
137
151
const cb = jest . fn ( ) ;
138
152
document . head . innerHTML = `<link href="${ url } " rel="preload" as="script">` ;
139
- const { link, needAttach } = createLink ( url , cb , {
140
- rel : 'preload' ,
141
- as : 'script' ,
153
+ const { link, needAttach } = createLink ( {
154
+ url,
155
+ cb,
156
+ attrs : {
157
+ rel : 'preload' ,
158
+ as : 'script' ,
159
+ } ,
142
160
} ) ;
143
161
144
162
expect ( link . tagName ) . toBe ( 'LINK' ) ;
@@ -150,7 +168,7 @@ describe('createLink', () => {
150
168
const url = 'https://example.com/script.js' ;
151
169
const cb = jest . fn ( ) ;
152
170
const attrs = { rel : 'preload' , as : 'script' , 'data-test' : 'test' } ;
153
- const { link } = createLink ( url , cb , attrs ) ;
171
+ const { link } = createLink ( { url, cb, attrs } ) ;
154
172
155
173
expect ( link . rel ) . toBe ( 'preload' ) ;
156
174
expect ( link . getAttribute ( 'as' ) ) . toBe ( 'script' ) ;
@@ -160,7 +178,11 @@ describe('createLink', () => {
160
178
it ( 'should call the callback when the link loads' , ( ) => {
161
179
const url = 'https://example.com/script.js' ;
162
180
const cb = jest . fn ( ) ;
163
- const { link, needAttach } = createLink ( url , cb , { as : 'script' } ) ;
181
+ const { link, needAttach } = createLink ( {
182
+ url,
183
+ cb,
184
+ attrs : { as : 'script' } ,
185
+ } ) ;
164
186
165
187
if ( needAttach ) {
166
188
document . head . appendChild ( link ) ;
@@ -173,7 +195,11 @@ describe('createLink', () => {
173
195
it ( 'should call the callback when the link fails to load' , ( ) => {
174
196
const url = 'https://example.com/script.js' ;
175
197
const cb = jest . fn ( ) ;
176
- const { link, needAttach } = createLink ( url , cb , { as : 'script' } ) ;
198
+ const { link, needAttach } = createLink ( {
199
+ url,
200
+ cb,
201
+ attrs : { as : 'script' } ,
202
+ } ) ;
177
203
178
204
if ( needAttach ) {
179
205
document . head . appendChild ( link ) ;
@@ -190,7 +216,12 @@ describe('createLink', () => {
190
216
customLink . href = url ;
191
217
customLink . rel = 'preload' ;
192
218
customLink . setAttribute ( 'as' , 'script' ) ;
193
- const { link } = createLink ( url , cb , { } , ( ) => customLink ) ;
219
+ const { link } = createLink ( {
220
+ url,
221
+ cb,
222
+ attrs : { } ,
223
+ createLinkHook : ( ) => customLink ,
224
+ } ) ;
194
225
195
226
expect ( link ) . toBe ( customLink ) ;
196
227
} ) ;
0 commit comments