@@ -137,4 +137,132 @@ describe('ContentFetch', () => {
137
137
expect ( onStart ) . toHaveBeenCalled ( )
138
138
expect ( onEnd ) . toHaveBeenCalled ( )
139
139
} )
140
+
141
+ // Test fromTo method
142
+ test ( 'fromTo fetches and injects content' , async ( ) => {
143
+ document . body . innerHTML = `
144
+ <div id="source">Source Content</div>
145
+ <div id="target"></div>
146
+ `
147
+
148
+ const mockHtml = '<div class="content">New Content</div>'
149
+ mockFetch . mockResolvedValueOnce ( {
150
+ ok : true ,
151
+ text : ( ) =>
152
+ Promise . resolve ( `
153
+ <html>
154
+ <body>
155
+ <div id="source">${ mockHtml } </div>
156
+ </body>
157
+ </html>
158
+ ` ) ,
159
+ } )
160
+
161
+ await contentFetch . fromTo (
162
+ {
163
+ selector : '#source' ,
164
+ url : 'http://localhost/test' ,
165
+ } ,
166
+ {
167
+ destination : '#target' ,
168
+ } ,
169
+ )
170
+
171
+ expect ( document . querySelector ( '#target' ) . innerHTML ) . toContain ( 'New Content' )
172
+ } )
173
+
174
+ // Test fromTo with callbacks
175
+ test ( 'fromTo executes callbacks' , async ( ) => {
176
+ document . body . innerHTML = `
177
+ <div id="source">Source Content</div>
178
+ <div id="target"></div>
179
+ `
180
+
181
+ const onStartFrom = jest . fn ( )
182
+ const onEndFrom = jest . fn ( )
183
+ const onStartTo = jest . fn ( )
184
+ const onEndTo = jest . fn ( )
185
+
186
+ const mockHtml = '<div class="content">New Content</div>'
187
+ mockFetch . mockResolvedValueOnce ( {
188
+ ok : true ,
189
+ text : ( ) =>
190
+ Promise . resolve ( `
191
+ <html>
192
+ <body>
193
+ <div id="source">${ mockHtml } </div>
194
+ </body>
195
+ </html>
196
+ ` ) ,
197
+ } )
198
+
199
+ await contentFetch . fromTo (
200
+ {
201
+ selector : '#source' ,
202
+ url : 'http://localhost/test' ,
203
+ onStart : onStartFrom ,
204
+ onEnd : onEndFrom ,
205
+ } ,
206
+ {
207
+ destination : '#target' ,
208
+ onStart : onStartTo ,
209
+ onEnd : onEndTo ,
210
+ } ,
211
+ )
212
+
213
+ // Remove setTimeout and directly check callbacks
214
+ expect ( onStartFrom ) . toHaveBeenCalled ( )
215
+ expect ( onEndFrom ) . toHaveBeenCalled ( )
216
+ expect ( onStartTo ) . toHaveBeenCalled ( )
217
+ expect ( onEndTo ) . toHaveBeenCalled ( )
218
+ } )
219
+
220
+ // Test fromTo error handling
221
+ test ( 'fromTo handles errors properly' , async ( ) => {
222
+ const onError = jest . fn ( )
223
+
224
+ // Expect the promise to reject
225
+ await expect (
226
+ contentFetch . fromTo (
227
+ {
228
+ selector : '#nonexistent' ,
229
+ onError,
230
+ } ,
231
+ {
232
+ destination : '#target' ,
233
+ } ,
234
+ ) ,
235
+ ) . rejects . toThrow ( 'Element not found for selector: #nonexistent' )
236
+
237
+ // Verify onError was called
238
+ expect ( onError ) . toHaveBeenCalled ( )
239
+ expect ( onError ) . toHaveBeenCalledWith (
240
+ expect . objectContaining ( {
241
+ message : 'Element not found for selector: #nonexistent' ,
242
+ } ) ,
243
+ )
244
+ } )
245
+
246
+ // Test abortFetch
247
+ test ( 'abortFetch cancels ongoing requests' , async ( ) => {
248
+ const abortSpy = jest . spyOn ( contentFetch . controller , 'abort' )
249
+ const clearSpy = jest . spyOn ( contentFetch . cache , 'clear' )
250
+
251
+ contentFetch . abortFetch ( )
252
+
253
+ expect ( abortSpy ) . toHaveBeenCalled ( )
254
+ expect ( clearSpy ) . toHaveBeenCalled ( )
255
+ expect ( contentFetch . controller ) . toBeDefined ( ) // New controller created
256
+ } )
257
+
258
+ // Test cache clearing on abort
259
+ test ( 'abortFetch clears cache' , async ( ) => {
260
+ // Pre-populate cache
261
+ contentFetch . cache . set ( 'test-key' , 'test-data' )
262
+ expect ( contentFetch . cache . size ) . toBe ( 1 )
263
+
264
+ contentFetch . abortFetch ( )
265
+
266
+ expect ( contentFetch . cache . size ) . toBe ( 0 )
267
+ } )
140
268
} )
0 commit comments