@@ -24,6 +24,7 @@ import Stroke from 'ol/style/Stroke';
2424import Point from 'ol/geom/Point' ;
2525import ImageLayer from 'ol/layer/Image' ;
2626import RegularShape from 'ol/style/RegularShape' ;
27+ import { unByKey } from 'ol/Observable' ;
2728
2829
2930var url = "http://supermapiserver:8090/iserver/services/map-china400/rest/maps/China_4326" ;
@@ -272,23 +273,25 @@ describe('openlayers_GraphicLayer', () => {
272273 } )
273274 } ) ;
274275 map . addLayer ( graphicLayer ) ;
275-
276- setTimeout ( ( ) => {
277- const graphicSource = graphicLayer . getSource ( ) ;
278- graphicSource . addGraphics ( graphics ) ;
279- expect ( graphicSource . graphics . length ) . toEqual ( 10 ) ;
280- graphicLayer . getSource ( ) . _forEachFeatureAtCoordinate ( [ - 35.16 , 38.05 ] , 1 , ( result ) => {
281- expect ( result ) . not . toBeNull ( ) ;
282- } ) ;
283-
284- let pixel = map . getPixelFromCoordinate ( [ - 35.16 , 38.05 ] ) ;
285- map . forEachFeatureAtPixel ( pixel ,
286- ( graphic , layer ) => {
287- expect ( graphic ) . toBe ( graphics [ 0 ] ) ;
288- expect ( layer ) . toBe ( graphicLayer ) ;
289- } ) ;
290- done ( ) ;
291- } , 0 ) ;
276+ const key = graphicLayer . on ( 'postrender' , function ( ) {
277+ if ( graphicLayer . getSource ( ) . renderer ) {
278+ unByKey ( key ) ;
279+ const graphicSource = graphicLayer . getSource ( ) ;
280+ graphicSource . addGraphics ( graphics ) ;
281+ expect ( graphicSource . graphics . length ) . toEqual ( 10 ) ;
282+ graphicLayer . getSource ( ) . _forEachFeatureAtCoordinate ( [ - 35.16 , 38.05 ] , 1 , ( result ) => {
283+ expect ( result ) . not . toBeNull ( ) ;
284+ } ) ;
285+
286+ let pixel = map . getPixelFromCoordinate ( [ - 35.16 , 38.05 ] ) ;
287+ map . forEachFeatureAtPixel ( pixel ,
288+ ( graphic , layer ) => {
289+ expect ( graphic ) . toBe ( graphics [ 0 ] ) ;
290+ expect ( layer ) . toBe ( graphicLayer ) ;
291+ } ) ;
292+ done ( ) ;
293+ }
294+ } ) ;
292295 } ) ;
293296
294297 it ( "getGraphicBy add getGraphicById" , ( done ) => {
@@ -632,4 +635,4 @@ describe('openlayers_GraphicLayer', () => {
632635 done ( ) ;
633636 } , 0 ) ;
634637 } ) ;
635- } ) ;
638+ } ) ;
0 commit comments