@@ -319,20 +319,20 @@ describe('openlayers_GraphicLayer', () => {
319319 } )
320320 } ) ;
321321 map . addLayer ( graphicLayer ) ;
322+ const key = graphicLayer . on ( 'postrender' , function ( ) {
323+ if ( graphicLayer . getSource ( ) . renderer ) {
324+ unByKey ( key ) ;
325+ const graphic = graphicLayer . getSource ( ) . getGraphicBy ( "id" , 1 ) ;
326+ expect ( graphic ) . not . toBeNull ( ) ;
327+ expect ( graphic . getId ( ) ) . toEqual ( 1 ) ;
322328
323- setTimeout ( ( ) => {
324- const graphic = graphicLayer . getSource ( ) . getGraphicBy ( "id" , 1 ) ;
325- expect ( graphic ) . not . toBeNull ( ) ;
326- expect ( graphic . getId ( ) ) . toEqual ( 1 ) ;
327-
328- const graphic1 = graphicLayer . getSource ( ) . getGraphicById ( 1 ) ;
329- expect ( graphic1 . getId ( ) ) . toEqual ( 1 ) ;
329+ const graphic1 = graphicLayer . getSource ( ) . getGraphicById ( 1 ) ;
330+ expect ( graphic1 . getId ( ) ) . toEqual ( 1 ) ;
330331
331332 // map.removeLayer(graphicLayer);
332- done ( ) ;
333- } , 0 )
334-
335-
333+ done ( ) ;
334+ }
335+ } ) ;
336336 } ) ;
337337 it ( "getGraphicsByAttribute" , ( done ) => {
338338 let graphics = [ ] ;
@@ -359,14 +359,21 @@ describe('openlayers_GraphicLayer', () => {
359359 } )
360360 } ) ;
361361 map . addLayer ( graphicLayer ) ;
362-
363- setTimeout ( ( ) => {
364- const graphic = graphicLayer . getSource ( ) . getGraphicsByAttribute ( "name" , "graphic_1" ) ;
365- expect ( graphic ) . not . toBeNull ( ) ;
366- expect ( graphic [ 0 ] . getAttributes ( ) . name ) . toBe ( "graphic_1" ) ;
367- // map.removeLayer(graphicLayer);
368- done ( ) ;
369- } , 0 ) ;
362+ const key = graphicLayer . on ( 'postrender' , function ( ) {
363+ if ( graphicLayer . getSource ( ) . renderer ) {
364+ unByKey ( key ) ;
365+ const graphic = graphicLayer . getSource ( ) . getGraphicsByAttribute ( "name" , "graphic_1" ) ;
366+ const key1 = graphic [ 0 ] . on ( 'postrender' , function ( ) {
367+ if ( graphic [ 0 ] . getSource ( ) . renderer ) {
368+ unByKey ( key1 ) ;
369+ expect ( graphic ) . not . toBeNull ( ) ;
370+ expect ( graphic [ 0 ] . getAttributes ( ) . name ) . toBe ( "graphic_1" ) ;
371+ // map.removeLayer(graphicLayer);
372+ done ( ) ;
373+ }
374+ } )
375+ }
376+ } ) ;
370377 } ) ;
371378
372379 it ( "removeGraphics" , ( done ) => {
@@ -394,25 +401,27 @@ describe('openlayers_GraphicLayer', () => {
394401 } )
395402 } ) ;
396403 map . addLayer ( graphicLayer ) ;
404+ const key = graphicLayer . on ( 'postrender' , function ( ) {
405+ if ( graphicLayer . getSource ( ) . renderer ) {
406+ unByKey ( key ) ;
407+ const graphicSource = graphicLayer . getSource ( ) ;
408+ //删除单个
409+ let deleteGraphic = graphics [ 0 ] ;
410+ expect ( graphicSource . graphics . length ) . toEqual ( 5 ) ;
411+ graphicSource . removeGraphics ( deleteGraphic ) ;
412+ expect ( graphicSource . graphics . length ) . toEqual ( 4 ) ;
397413
398- setTimeout ( ( ) => {
399- const graphicSource = graphicLayer . getSource ( ) ;
400- //删除单个
401- let deleteGraphic = graphics [ 0 ] ;
402- expect ( graphicSource . graphics . length ) . toEqual ( 5 ) ;
403- graphicSource . removeGraphics ( deleteGraphic ) ;
404- expect ( graphicSource . graphics . length ) . toEqual ( 4 ) ;
405-
406- //多个
407- deleteGraphic = [ graphics [ 1 ] , graphics [ 2 ] ] ;
408- graphicSource . removeGraphics ( deleteGraphic ) ;
409- expect ( graphicSource . graphics . length ) . toEqual ( 2 ) ;
414+ //多个
415+ deleteGraphic = [ graphics [ 1 ] , graphics [ 2 ] ] ;
416+ graphicSource . removeGraphics ( deleteGraphic ) ;
417+ expect ( graphicSource . graphics . length ) . toEqual ( 2 ) ;
410418
411- //默认
412- graphicSource . removeGraphics ( ) ;
413- expect ( graphicSource . graphics . length ) . toEqual ( 0 ) ;
414- done ( ) ;
415- } , 0 ) ;
419+ //默认
420+ graphicSource . removeGraphics ( ) ;
421+ expect ( graphicSource . graphics . length ) . toEqual ( 0 ) ;
422+ done ( ) ;
423+ }
424+ } ) ;
416425 } ) ;
417426 it ( "getLayerState" , ( done ) => {
418427 let graphics = [ ] ;
@@ -440,13 +449,15 @@ describe('openlayers_GraphicLayer', () => {
440449 } )
441450 } ) ;
442451 map . addLayer ( graphicLayer ) ;
443-
444- setTimeout ( ( ) => {
445- const state = graphicLayer . getSource ( ) . getLayerState ( ) ;
446- expect ( state ) . not . toBeNull ( ) ;
447- expect ( state . color ) . toEqual ( "red" ) ;
448- done ( ) ;
449- } , 0 ) ;
452+ const key = graphicLayer . on ( 'postrender' , function ( ) {
453+ if ( graphicLayer . getSource ( ) . renderer ) {
454+ unByKey ( key ) ;
455+ const state = graphicLayer . getSource ( ) . getLayerState ( ) ;
456+ expect ( state ) . not . toBeNull ( ) ;
457+ expect ( state . color ) . toEqual ( "red" ) ;
458+ done ( ) ;
459+ }
460+ } ) ;
450461 } ) ;
451462
452463 it ( "setGraphics" , ( done ) => {
@@ -474,23 +485,25 @@ describe('openlayers_GraphicLayer', () => {
474485 } )
475486 } ) ;
476487 map . addLayer ( graphicLayer ) ;
477-
478- setTimeout ( ( ) => {
479- graphicLayer . getSource ( ) . clear ( ) ;
480- expect ( graphicLayer . getSource ( ) . graphics . length ) . toEqual ( 0 ) ;
481- let graphics = [ ] ;
482- for ( let j = 0 ; j < coors . length ; ++ j ) {
483- graphics [ j ] = new GraphicObj ( new Point ( coors [ j ] ) ) ;
484- graphics [ j ] . setId ( j ) ;
485- graphics [ j ] . setAttributes ( {
486- name : "graphic_" + j
487- } ) ;
488+ const key = graphicLayer . on ( 'postrender' , function ( ) {
489+ if ( graphicLayer . getSource ( ) . renderer ) {
490+ unByKey ( key ) ;
491+ graphicLayer . getSource ( ) . clear ( ) ;
492+ expect ( graphicLayer . getSource ( ) . graphics . length ) . toEqual ( 0 ) ;
493+ let graphics = [ ] ;
494+ for ( let j = 0 ; j < coors . length ; ++ j ) {
495+ graphics [ j ] = new GraphicObj ( new Point ( coors [ j ] ) ) ;
496+ graphics [ j ] . setId ( j ) ;
497+ graphics [ j ] . setAttributes ( {
498+ name : "graphic_" + j
499+ } ) ;
500+ }
501+
502+ graphicLayer . getSource ( ) . setGraphics ( graphics ) ;
503+ expect ( graphicLayer . getSource ( ) . graphics . length ) . toEqual ( 5 ) ;
504+ done ( )
488505 }
489-
490- graphicLayer . getSource ( ) . setGraphics ( graphics ) ;
491- expect ( graphicLayer . getSource ( ) . graphics . length ) . toEqual ( 5 ) ;
492- done ( )
493- } , 0 ) ;
506+ } ) ;
494507 } ) ;
495508
496509 it ( "setStyle" , ( done ) => {
@@ -519,15 +532,17 @@ describe('openlayers_GraphicLayer', () => {
519532 } )
520533 } ) ;
521534 map . addLayer ( graphicLayer ) ;
522-
523- setTimeout ( ( ) => {
524- expect ( graphicLayer . getSource ( ) . color ) . toEqual ( "red" ) ;
525- graphicLayer . getSource ( ) . setStyle ( {
526- color : "blue"
527- } ) ;
528- expect ( graphicLayer . getSource ( ) . color ) . toEqual ( "blue" ) ;
529- done ( )
530- } , 0 ) ;
535+ const key = graphicLayer . on ( 'postrender' , function ( ) {
536+ if ( graphicLayer . getSource ( ) . renderer ) {
537+ unByKey ( key ) ;
538+ expect ( graphicLayer . getSource ( ) . color ) . toEqual ( "red" ) ;
539+ graphicLayer . getSource ( ) . setStyle ( {
540+ color : "blue"
541+ } ) ;
542+ expect ( graphicLayer . getSource ( ) . color ) . toEqual ( "blue" ) ;
543+ done ( )
544+ }
545+ } ) ;
531546 } ) ;
532547
533548 it ( "clear" , ( done ) => {
@@ -555,13 +570,15 @@ describe('openlayers_GraphicLayer', () => {
555570 } )
556571 } ) ;
557572 map . addLayer ( graphicLayer ) ;
558-
559- setTimeout ( ( ) => {
560- const graphicSource = graphicLayer . getSource ( ) ;
561- graphicSource . clear ( ) ;
562- expect ( graphicSource . graphics . length ) . toEqual ( 0 ) ;
563- done ( ) ;
564- } , 0 ) ;
573+ const key = graphicLayer . on ( 'postrender' , function ( ) {
574+ if ( graphicLayer . getSource ( ) . renderer ) {
575+ unByKey ( key ) ;
576+ const graphicSource = graphicLayer . getSource ( ) ;
577+ graphicSource . clear ( ) ;
578+ expect ( graphicSource . graphics . length ) . toEqual ( 0 ) ;
579+ done ( ) ;
580+ }
581+ } ) ;
565582 } ) ;
566583
567584 it ( 'forEachFeatureAtCoordinate_ICL_1047' , ( done ) => {
@@ -610,29 +627,32 @@ describe('openlayers_GraphicLayer', () => {
610627 color : "rgba(0,166,0,1)" ,
611628 } ) ,
612629 } ) ;
613- setTimeout ( ( ) => {
614- var resgraphics = graphicLayer . getSource ( ) ;
615- for ( let j = 0 ; j < resgraphics . length ; ++ j ) {
616- resgraphics [ j ] . setStyle ( cloverShapeStyle ) ;
617- var resolution = 1 ;
618- var evtPixel = [ - 35.16 , 38.05 ] ;
619- //1、当鼠标点击在三叶草叶子内时,得到要素,调用高亮函数
620- var innerCoors = [ 25.576171875 , - 27.158203125 ] ;
621- var callback = function ( a , b ) {
622- expect ( b ) . toNotBe ( null ) ;
623- expect ( a . coordinate ) . toBe ( [ 25.576171875 , - 27.158203125 ] ) ;
624- } ;
625- graphicLayer . getSource ( ) . _forEachFeatureAtCoordinate ( innerCoors , resolution , callback , evtPixel ) ;
626- spyOn ( graphicLayer , '_highLight' ) ;
627- expect ( graphicLayer . getSource ( ) . _highLight ) . toHaveBeenCalled ( ) ;
628- //2、当鼠标点击在三叶草外时, 关闭高亮,返回undefined
629- var outerCoors = [ 27.685546875 , - 26.015625 ] ;
630- var re = graphicLayer . getSource ( ) . _forEachFeatureAtCoordinate ( outerCoors , resolution , callback , evtPixel ) ;
631- spyOn ( graphicLayer , '_highLightClose' ) ;
632- expect ( graphicLayer . getSource ( ) . _highLightClose ) . toHaveBeenCalled ( ) ;
633- expect ( re ) . toBe ( undefined ) ;
630+ const key = graphicLayer . on ( 'postrender' , function ( ) {
631+ if ( graphicLayer . getSource ( ) . renderer ) {
632+ unByKey ( key ) ;
633+ var resgraphics = graphicLayer . getSource ( ) ;
634+ for ( let j = 0 ; j < resgraphics . length ; ++ j ) {
635+ resgraphics [ j ] . setStyle ( cloverShapeStyle ) ;
636+ var resolution = 1 ;
637+ var evtPixel = [ - 35.16 , 38.05 ] ;
638+ //1、当鼠标点击在三叶草叶子内时,得到要素,调用高亮函数
639+ var innerCoors = [ 25.576171875 , - 27.158203125 ] ;
640+ var callback = function ( a , b ) {
641+ expect ( b ) . toNotBe ( null ) ;
642+ expect ( a . coordinate ) . toBe ( [ 25.576171875 , - 27.158203125 ] ) ;
643+ } ;
644+ graphicLayer . getSource ( ) . _forEachFeatureAtCoordinate ( innerCoors , resolution , callback , evtPixel ) ;
645+ spyOn ( graphicLayer , '_highLight' ) ;
646+ expect ( graphicLayer . getSource ( ) . _highLight ) . toHaveBeenCalled ( ) ;
647+ //2、当鼠标点击在三叶草外时, 关闭高亮,返回undefined
648+ var outerCoors = [ 27.685546875 , - 26.015625 ] ;
649+ var re = graphicLayer . getSource ( ) . _forEachFeatureAtCoordinate ( outerCoors , resolution , callback , evtPixel ) ;
650+ spyOn ( graphicLayer , '_highLightClose' ) ;
651+ expect ( graphicLayer . getSource ( ) . _highLightClose ) . toHaveBeenCalled ( ) ;
652+ expect ( re ) . toBe ( undefined ) ;
653+ }
654+ done ( ) ;
634655 }
635- done ( ) ;
636- } , 0 ) ;
656+ } ) ;
637657 } ) ;
638658} ) ;
0 commit comments