@@ -211,42 +211,90 @@ describe('ol-mapbox-style', function () {
211211 . catch ( done ) ;
212212 } ) ;
213213
214- it ( 'handles geojson wfs sources with bbox loadingstrategy and custom projection' , function ( done ) {
215- fetch ( './fixtures/geojson-wfs.json' )
216- . then ( function ( response ) {
217- return response . json ( ) ;
214+ describe ( 'geojson' , function ( ) {
215+ let originalFetch ;
216+ const requests = [ ] ;
217+ beforeEach ( function ( ) {
218+ originalFetch = fetch ;
219+ window . fetch = ( request ) => {
220+ requests . push ( request ) ;
221+ return originalFetch ( request ) ;
222+ } ;
223+ } ) ;
224+ afterEach ( function ( ) {
225+ window . fetch = originalFetch ;
226+ requests . length = 0 ;
227+ } ) ;
228+ it ( 'handles geojson wfs sources with bbox loadingstrategy and custom projection' , function ( done ) {
229+ fetch ( './fixtures/geojson-wfs.json' )
230+ . then ( function ( response ) {
231+ return response . json ( ) ;
232+ } )
233+ . then ( function ( style ) {
234+ style . sources . water_areas . data =
235+ style . sources . water_areas . data . replace ( '3857' , '4326' ) ;
236+ apply ( target , style , { projection : 'EPSG:4326' } )
237+ . then ( function ( map ) {
238+ const layer = map
239+ . getAllLayers ( )
240+ . find ( ( x ) => x . get ( 'mapbox-source' ) === 'water_areas' ) ;
241+ const source = layer . getSource ( ) ;
242+ source . once ( 'change' , ( ) => {
243+ try {
244+ const url = new URL ( requests [ requests . length - 1 ] . url ) ;
245+ const bbox = url . searchParams . get ( 'bbox' ) ;
246+ const extent = map . getView ( ) . calculateExtent ( ) ;
247+ should ( bbox ) . be . equal ( extent . join ( ',' ) ) ;
248+ done ( ) ;
249+ } catch ( e ) {
250+ done ( e ) ;
251+ }
252+ } ) ;
253+ source . loadFeatures (
254+ map . getView ( ) . calculateExtent ( ) ,
255+ 1 ,
256+ map . getView ( ) . getProjection ( )
257+ ) ;
258+ } )
259+ . catch ( done ) ;
260+ } ) ;
261+ } ) ;
262+
263+ it ( 'handles geojson wfs sources with bbox loadingstrategy & transformRequest' , function ( done ) {
264+ apply ( target , './fixtures/geojson-wfs.json' , {
265+ transformRequest : ( urlStr , type ) => {
266+ if ( type === 'GeoJSON' ) {
267+ const url = new URL ( urlStr + '&transformRequest=true' ) ;
268+ return new Request ( url ) ;
269+ }
270+ } ,
218271 } )
219- . then ( function ( style ) {
220- style . sources . water_areas . data =
221- style . sources . water_areas . data . replace ( '3857' , '4326' ) ;
222- apply ( target , style , { projection : 'EPSG:4326' } )
223- . then ( function ( map ) {
224- const layer = map
225- . getAllLayers ( )
226- . find ( ( x ) => x . get ( 'mapbox-source' ) === 'water_areas' ) ;
227- const source = layer . getSource ( ) ;
228- const originalFetch = fetch ;
229- const requests = [ ] ;
230- fetch = ( request ) => {
231- requests . push ( request ) ;
232- return originalFetch ( request ) ;
233- } ;
234- source . loadFeatures (
235- map . getView ( ) . calculateExtent ( ) ,
236- 1 ,
237- map . getView ( ) . getProjection ( )
238- ) ;
239- source . once ( 'change' , ( ) => {
240- window . fetch = originalFetch ;
241- const url = new URL ( requests [ 0 ] . url ) ;
242- const bbox = url . searchParams . get ( 'bbox' ) ;
243- const extent = map . getView ( ) . calculateExtent ( ) ;
244- should ( bbox ) . be . equal ( extent . join ( ',' ) ) ;
272+ . then ( function ( map ) {
273+ const layer = map
274+ . getAllLayers ( )
275+ . find ( ( x ) => x . get ( 'mapbox-source' ) === 'water_areas' ) ;
276+ const source = layer . getSource ( ) ;
277+ source . loadFeatures (
278+ map . getView ( ) . calculateExtent ( ) ,
279+ 1 ,
280+ get ( 'EPSG:3857' )
281+ ) ;
282+ source . once ( 'change' , ( ) => {
283+ try {
284+ const url = new URL ( requests [ requests . length - 1 ] . url ) ;
285+ should ( url . searchParams . get ( 'transformRequest' ) ) . be . equal (
286+ 'true'
287+ ) ;
288+ should ( source ) . be . instanceof ( VectorSource ) ;
289+ should ( layer . getStyle ( ) ) . be . a . Function ( ) ;
245290 done ( ) ;
246- } ) ;
247- } )
248- . catch ( done ) ;
249- } ) ;
291+ } catch ( e ) {
292+ done ( e ) ;
293+ }
294+ } ) ;
295+ } )
296+ . catch ( done ) ;
297+ } ) ;
250298 } ) ;
251299
252300 it ( 'sets the correct GeoJON data projection for custom projections' , function ( done ) {
@@ -317,43 +365,6 @@ describe('ol-mapbox-style', function () {
317365 } ) ;
318366 } ) ;
319367
320- it ( 'handles geojson wfs sources with bbox loadingstrategy & transformRequest' , function ( done ) {
321- apply ( target , './fixtures/geojson-wfs.json' , {
322- transformRequest : ( urlStr , type ) => {
323- if ( type === 'GeoJSON' ) {
324- const url = new URL ( urlStr + '&transformRequest=true' ) ;
325- return new Request ( url ) ;
326- }
327- } ,
328- } )
329- . then ( function ( map ) {
330- const layer = map
331- . getAllLayers ( )
332- . find ( ( x ) => x . get ( 'mapbox-source' ) === 'water_areas' ) ;
333- const source = layer . getSource ( ) ;
334- const originalFetch = fetch ;
335- const requests = [ ] ;
336- fetch = ( request ) => {
337- requests . push ( request ) ;
338- return originalFetch ( request ) ;
339- } ;
340- source . loadFeatures (
341- map . getView ( ) . calculateExtent ( ) ,
342- 1 ,
343- get ( 'EPSG:3857' )
344- ) ;
345- source . once ( 'change' , ( ) => {
346- window . fetch = originalFetch ;
347- const url = new URL ( requests [ 0 ] . url ) ;
348- should ( url . searchParams . get ( 'transformRequest' ) ) . be . equal ( 'true' ) ;
349- should ( source ) . be . instanceof ( VectorSource ) ;
350- should ( layer . getStyle ( ) ) . be . a . Function ( ) ;
351- done ( ) ;
352- } ) ;
353- } )
354- . catch ( done ) ;
355- } ) ;
356-
357368 it ( 'handles geojson sources with inline GeoJSON' , function ( done ) {
358369 const map = new Map ( { target : target } ) ;
359370 map . getLayers ( ) . once ( 'add' , function ( e ) {
0 commit comments