11import mapboxgl from 'mapbox-gl' ;
22import '../../libs/deck.gl/5.1.3/deck.gl' ;
3- import { GraphicLayer } from '../../../src/mapboxgl/overlay/GraphicLayer' ;
3+ import { GraphicLayer } from '../../../src/mapboxgl/overlay/GraphicLayer' ;
44
55mapboxgl . accessToken = 'pk.eyJ1IjoibW9ua2VyIiwiYSI6ImNpd2Z6aTE5YTAwdHEyb2tpOWs2ZzRydmoifQ.LwQMRArUP8Q9P7QApuOIHg' ;
66describe ( 'mapboxgl_GraphicLayer' , ( ) => {
7- let originalTimeout , testDiv , map , graphics = [ ] , graphicLayer ;
8- const coors = [
9- [ - 35.16 , 38.05 ] ,
10- [ - 36.16 , 39.05 ] ,
11- [ - 36.16 , 40.05 ] ,
12- [ - 37.16 , 40.05 ] ,
13- [ - 38.16 , 39.05 ]
14- ] ;
7+ let originalTimeout ;
8+ const coors = [
9+ [ - 35.16 , 38.05 ] ,
10+ [ - 36.16 , 39.05 ] ,
11+ [ - 36.16 , 40.05 ] ,
12+ [ - 37.16 , 40.05 ] ,
13+ [ - 38.16 , 39.05 ]
14+ ] ;
15+ function creatGraphicLayer ( ) {
16+ let testDiv , map , graphics = [ ] , graphicLayer ;
1517 //构建数据
1618 for ( let i = 0 ; i < coors . length ; i ++ ) {
17- let lngLat = {
18- lng : parseFloat ( coors [ i ] [ 0 ] ) ,
19- lat : parseFloat ( coors [ i ] [ 1 ] )
20- } ;
21- graphics . push ( new mapboxgl . supermap . Graphic ( lngLat ) ) ;
22- graphics [ i ] . setId ( i ) ;
23- graphics [ i ] . setAttributes ( { name : "graphic_" + i } ) ;
19+ let lngLat = {
20+ lng : parseFloat ( coors [ i ] [ 0 ] ) ,
21+ lat : parseFloat ( coors [ i ] [ 1 ] )
22+ } ;
23+ graphics . push ( new mapboxgl . supermap . Graphic ( lngLat ) ) ;
24+ graphics [ i ] . setId ( i ) ;
25+ graphics [ i ] . setAttributes ( { name : "graphic_" + i } ) ;
2426 }
25-
26- beforeAll ( ( ) => {
27- testDiv = window . document . createElement ( "div" ) ;
28- testDiv . setAttribute ( "id" , "map" ) ;
29- testDiv . style . styleFloat = "left" ;
30- testDiv . style . marginLeft = "8px" ;
31- testDiv . style . marginTop = "50px" ;
32- testDiv . style . width = "500px" ;
33- testDiv . style . height = "500px" ;
34- window . document . body . appendChild ( testDiv ) ;
35- map = new mapboxgl . Map ( {
36- container : 'map' ,
37- style : 'mapbox://styles/mapbox/streets-v9' ,
38- center : [ 13.413952 , 52.531913 ] ,
39- zoom : 16.000000000000004 ,
40- pitch : 33.2
41- } ) ;
42- graphicLayer = new GraphicLayer ( "graphicLayer" , {
43- graphics : graphics
44- } ) ;
45- graphicLayer . onAdd ( map ) ;
46-
47- } ) ;
48- beforeEach ( ( ) => {
49- originalTimeout = jasmine . DEFAULT_TIMEOUT_INTERVAL ;
50- jasmine . DEFAULT_TIMEOUT_INTERVAL = 50000 ;
27+ testDiv = window . document . createElement ( "div" ) ;
28+ testDiv . setAttribute ( "id" , "map" ) ;
29+ testDiv . style . styleFloat = "left" ;
30+ testDiv . style . marginLeft = "8px" ;
31+ testDiv . style . marginTop = "50px" ;
32+ testDiv . style . width = "500px" ;
33+ testDiv . style . height = "500px" ;
34+ window . document . body . appendChild ( testDiv ) ;
35+ map = new mapboxgl . Map ( {
36+ container : 'map' ,
37+ style : 'mapbox://styles/mapbox/streets-v9' ,
38+ center : [ 13.413952 , 52.531913 ] ,
39+ zoom : 16.000000000000004 ,
40+ pitch : 33.2
5141 } ) ;
52- afterEach ( ( ) => {
53- jasmine . DEFAULT_TIMEOUT_INTERVAL = originalTimeout ;
54- } ) ;
55- afterAll ( ( ) => {
56- document . body . removeChild ( testDiv ) ;
57- // map.removeLayer("graphicLayer");
42+ graphicLayer = new GraphicLayer ( "graphicLayer" , {
43+ graphics : graphics
5844 } ) ;
45+ graphicLayer . onAdd ( map ) ;
46+ return graphicLayer
47+ }
48+ beforeEach ( ( ) => {
49+ originalTimeout = jasmine . DEFAULT_TIMEOUT_INTERVAL ;
50+ jasmine . DEFAULT_TIMEOUT_INTERVAL = 50000 ;
51+ } ) ;
52+ afterEach ( ( ) => {
53+ jasmine . DEFAULT_TIMEOUT_INTERVAL = originalTimeout ;
54+ } ) ;
55+ afterAll ( ( ) => {
56+ document . body . removeChild ( document . getElementById ( 'map' ) ) ;
57+ // map.removeLayer("graphicLayer");
58+ } ) ;
5959
60- it ( 'constructor' , ( done ) => {
61- setTimeout ( ( ) => {
62- expect ( graphicLayer . deckGL ) . not . toBeNull ( ) ;
63- done ( ) ;
64- } , 1000 )
65- } ) ;
60+ it ( 'constructor' , ( done ) => {
61+ let graphicLayer = creatGraphicLayer ( ) ;
62+ setTimeout ( ( ) => {
63+ expect ( graphicLayer . deckGL ) . not . toBeNull ( ) ;
64+ graphicLayer . remove ( ) ;
65+ done ( ) ;
66+ } , 1000 )
67+ } ) ;
6668
67- it ( 'setVisibility' , ( done ) => {
68- setTimeout ( ( ) => {
69- graphicLayer . setVisibility ( false ) ;
70- expect ( graphicLayer . visibility ) . toBeFalsy ( ) ;
71- done ( ) ;
72- } , 1000 )
73- } ) ;
69+ it ( 'setVisibility' , ( done ) => {
70+ let graphicLayer = creatGraphicLayer ( ) ;
71+ setTimeout ( ( ) => {
72+ graphicLayer . setVisibility ( false ) ;
73+ expect ( graphicLayer . visibility ) . toBeFalsy ( ) ;
74+ graphicLayer . remove ( ) ;
75+ done ( ) ;
76+ } , 1000 )
77+ } ) ;
7478
75- it ( "getGraphicBy add getGraphicById" , ( done ) => {
76- setTimeout ( ( ) => {
77- const graphic = graphicLayer . getGraphicBy ( "id" , 1 ) ;
78- expect ( graphic ) . not . toBeNull ( ) ;
79- expect ( graphic . getId ( ) ) . toEqual ( 1 ) ;
79+ it ( "getGraphicBy add getGraphicById" , ( done ) => {
80+ let graphicLayer = creatGraphicLayer ( ) ;
81+ setTimeout ( ( ) => {
82+ const graphic = graphicLayer . getGraphicBy ( "id" , 1 ) ;
83+ expect ( graphic ) . not . toBeNull ( ) ;
84+ expect ( graphic . getId ( ) ) . toEqual ( 1 ) ;
8085
81- const graphic1 = graphicLayer . getGraphicById ( 1 ) ;
82- expect ( graphic1 . getId ( ) ) . toEqual ( 1 ) ;
86+ const graphic1 = graphicLayer . getGraphicById ( 1 ) ;
87+ expect ( graphic1 . getId ( ) ) . toEqual ( 1 ) ;
88+ graphicLayer . remove ( ) ;
8389
84- done ( ) ;
85- } , 1000 )
86- } ) ;
87- it ( "getGraphicsByAttribute" , ( done ) => {
88- setTimeout ( ( ) => {
89- const graphic = graphicLayer . getGraphicsByAttribute ( "name" , "graphic_1" ) ;
90- expect ( graphic ) . not . toBeNull ( ) ;
91- expect ( graphic [ 0 ] . getAttributes ( ) . name ) . toBe ( "graphic_1" ) ;
92- done ( ) ;
93- } , 1000 ) ;
94- } ) ;
95- it ( "removeGraphics" , ( done ) => {
96- setTimeout ( ( ) => {
97- //删除单个
98- let deleteGraphic = graphics [ 0 ] ;
99- expect ( graphicLayer . graphics . length ) . toEqual ( 5 ) ;
100- graphicLayer . removeGraphics ( deleteGraphic ) ;
101- expect ( graphicLayer . graphics . length ) . toEqual ( 4 ) ;
102-
103- //多个
104- deleteGraphic = [ graphics [ 1 ] , graphics [ 2 ] ] ;
105- graphicLayer . removeGraphics ( deleteGraphic ) ;
106- expect ( graphicLayer . graphics . length ) . toEqual ( 2 ) ;
107-
108- //默认
109- graphicLayer . removeGraphics ( ) ;
110- expect ( graphicLayer . graphics . length ) . toEqual ( 0 ) ;
111-
112- done ( ) ;
113- } , 1000 ) ;
90+ done ( ) ;
91+ } , 1000 )
92+ } ) ;
93+ it ( "getGraphicsByAttribute" , ( done ) => {
94+ let graphicLayer = creatGraphicLayer ( ) ;
95+ setTimeout ( ( ) => {
96+ const graphic = graphicLayer . getGraphicsByAttribute ( "name" , "graphic_1" ) ;
97+ expect ( graphic ) . not . toBeNull ( ) ;
98+ expect ( graphic [ 0 ] . getAttributes ( ) . name ) . toBe ( "graphic_1" ) ;
99+ graphicLayer . remove ( ) ;
100+ done ( ) ;
101+ } , 1000 ) ;
102+ } ) ;
103+ it ( "removeGraphics" , ( done ) => {
104+ let graphicLayer = creatGraphicLayer ( ) ;
105+ setTimeout ( ( ) => {
106+ //删除单个
107+ let deleteGraphic = graphicLayer . graphics [ 0 ] ;
108+ expect ( graphicLayer . graphics . length ) . toEqual ( 5 ) ;
109+ graphicLayer . removeGraphics ( deleteGraphic ) ;
110+ expect ( graphicLayer . graphics . length ) . toEqual ( 4 ) ;
114111
115- } ) ;
116- it ( "getState" , ( done ) => {
117- setTimeout ( ( ) => {
118- const state = graphicLayer . getState ( ) ;
119- expect ( state ) . not . toBeNull ( ) ;
120- expect ( state . color ) . toEqual ( [ 0 , 0 , 0 , 255 ] ) ;
121- done ( ) ;
122- } , 1000 ) ;
123- } ) ;
112+ //多个
113+ deleteGraphic = [ graphicLayer . graphics [ 1 ] , graphicLayer . graphics [ 2 ] ] ;
114+ graphicLayer . removeGraphics ( deleteGraphic ) ;
115+ expect ( graphicLayer . graphics . length ) . toEqual ( 2 ) ;
124116
125- it ( "setStyle" , ( done ) => {
126- setTimeout ( ( ) => {
127- expect ( graphicLayer . color ) . toEqual ( [ 0 , 0 , 0 , 255 ] ) ;
128- graphicLayer . setStyle ( { color : "blue" } ) ;
129- expect ( graphicLayer . color ) . toEqual ( "blue" ) ;
130- done ( ) ;
131- } , 4000 ) ;
132- } ) ;
117+ //默认
118+ graphicLayer . removeGraphics ( ) ;
119+ expect ( graphicLayer . graphics . length ) . toEqual ( 0 ) ;
133120
134- it ( "addGraphics" , ( done ) => {
135- setTimeout ( ( ) => {
136- graphicLayer . clear ( ) ;
137- expect ( graphicLayer . graphics . length ) . toEqual ( 0 ) ;
138- let graphics = [ ] ;
139- for ( let i = 0 ; i < coors . length ; i ++ ) {
140- let lngLat = {
141- lng : parseFloat ( coors [ i ] [ 0 ] ) ,
142- lat : parseFloat ( coors [ i ] [ 1 ] )
143- } ;
144- graphics . push ( new mapboxgl . supermap . Graphic ( lngLat ) ) ;
145- graphics [ i ] . setId ( i ) ;
146- graphics [ i ] . setAttributes ( { name : "graphic_" + i } ) ;
147- }
148- graphicLayer . addGraphics ( graphics ) ;
149- expect ( graphicLayer . graphics . length ) . toEqual ( 5 ) ;
150- done ( ) ;
151- } , 4000 ) ;
152- } ) ;
121+ graphicLayer . remove ( ) ;
153122
154- it ( "setGraphics" , ( done ) => {
155- setTimeout ( ( ) => {
156- let graphics = [ ] ;
157- for ( let i = 0 ; i < coors . length ; i ++ ) {
158- let lngLat = {
159- lng : parseFloat ( coors [ i ] [ 0 ] ) ,
160- lat : parseFloat ( coors [ i ] [ 1 ] )
161- } ;
162- graphics . push ( new mapboxgl . supermap . Graphic ( lngLat ) ) ;
163- graphics [ i ] . setId ( i ) ;
164- graphics [ i ] . setAttributes ( { name : "graphic_" + i } ) ;
165- }
166- graphicLayer . setGraphics ( graphics ) ;
167- expect ( graphicLayer . graphics . length ) . toEqual ( 5 ) ;
168- done ( ) ;
169- } , 4000 ) ;
170- } ) ;
123+ done ( ) ;
124+ } , 1000 ) ;
125+
126+ } ) ;
127+ it ( "getState" , ( done ) => {
128+ let graphicLayer = creatGraphicLayer ( ) ;
129+ setTimeout ( ( ) => {
130+ const state = graphicLayer . getState ( ) ;
131+ expect ( state ) . not . toBeNull ( ) ;
132+ expect ( state . color ) . toEqual ( [ 0 , 0 , 0 , 255 ] ) ;
133+
134+ graphicLayer . remove ( ) ;
135+ done ( ) ;
136+ } , 1000 ) ;
137+ } ) ;
138+
139+ it ( "setStyle" , ( done ) => {
140+ let graphicLayer = creatGraphicLayer ( ) ;
141+ setTimeout ( ( ) => {
142+ expect ( graphicLayer . color ) . toEqual ( [ 0 , 0 , 0 , 255 ] ) ;
143+ graphicLayer . setStyle ( { color : "blue" } ) ;
144+ expect ( graphicLayer . color ) . toEqual ( "blue" ) ;
145+
146+ graphicLayer . remove ( ) ;
147+ done ( ) ;
148+ } , 4000 ) ;
149+ } ) ;
150+
151+ it ( "addGraphics" , ( done ) => {
152+ let graphicLayer = creatGraphicLayer ( ) ;
153+ setTimeout ( ( ) => {
154+ graphicLayer . clear ( ) ;
155+ expect ( graphicLayer . graphics . length ) . toEqual ( 0 ) ;
156+ let graphics = [ ] ;
157+ for ( let i = 0 ; i < coors . length ; i ++ ) {
158+ let lngLat = {
159+ lng : parseFloat ( coors [ i ] [ 0 ] ) ,
160+ lat : parseFloat ( coors [ i ] [ 1 ] )
161+ } ;
162+ graphics . push ( new mapboxgl . supermap . Graphic ( lngLat ) ) ;
163+ graphics [ i ] . setId ( i ) ;
164+ graphics [ i ] . setAttributes ( { name : "graphic_" + i } ) ;
165+ }
166+ graphicLayer . addGraphics ( graphics ) ;
167+ expect ( graphicLayer . graphics . length ) . toEqual ( 5 ) ;
168+
169+ graphicLayer . remove ( ) ;
170+ done ( ) ;
171+ } , 4000 ) ;
172+ } ) ;
173+
174+ it ( "setGraphics" , ( done ) => {
175+ let graphicLayer = creatGraphicLayer ( ) ;
176+ setTimeout ( ( ) => {
177+ let graphics = [ ] ;
178+ for ( let i = 0 ; i < coors . length ; i ++ ) {
179+ let lngLat = {
180+ lng : parseFloat ( coors [ i ] [ 0 ] ) ,
181+ lat : parseFloat ( coors [ i ] [ 1 ] )
182+ } ;
183+ graphics . push ( new mapboxgl . supermap . Graphic ( lngLat ) ) ;
184+ graphics [ i ] . setId ( i ) ;
185+ graphics [ i ] . setAttributes ( { name : "graphic_" + i } ) ;
186+ }
187+ graphicLayer . setGraphics ( graphics ) ;
188+ expect ( graphicLayer . graphics . length ) . toEqual ( 5 ) ;
189+
190+ graphicLayer . remove ( ) ;
191+ done ( ) ;
192+ } , 4000 ) ;
193+ } ) ;
171194} ) ;
0 commit comments