@@ -74,8 +74,156 @@ describe('WebMapBase - MapBase Methods', () => {
7474
7575 it ( 'should return empty array when _sourceListModel is null' , ( ) => {
7676 instance . _sourceListModel = null ;
77-
77+
7878 expect ( instance . getLayers ( ) ) . toEqual ( [ ] ) ;
7979 } ) ;
8080 } ) ;
8181} ) ;
82+
83+ describe ( 'WebMapBase - Handler Methods' , ( ) => {
84+ let instance ;
85+ let mockHandler ;
86+
87+ beforeEach ( ( ) => {
88+ const MapBase = createMapClassExtending ( ) ;
89+ instance = new MapBase ( ) ;
90+ instance . getLegendInfos = function ( ) {
91+ return ( this . _handler && this . _handler . _getLegendInfos ( ) ) || [ ] ;
92+ } ;
93+ // Mock WebMapV2 handler
94+ mockHandler = {
95+ _getLegendInfos : jasmine . createSpy ( '_getLegendInfos' ) . and . callFake ( ( ) => {
96+ return [
97+ { showLegend : true , id : 'layer1' , title : 'Layer 1' } ,
98+ { showLegend : false , id : 'layer2' , title : 'Layer 2' }
99+ ] ;
100+ } )
101+ } ;
102+ instance . _handler = mockHandler ;
103+ } ) ;
104+
105+ describe ( 'getLegendInfos' , ( ) => {
106+ it ( 'should call handler._getLegendInfos and return result' , ( ) => {
107+ const result = instance . getLegendInfos ( ) ;
108+
109+ expect ( mockHandler . _getLegendInfos ) . toHaveBeenCalled ( ) ;
110+ expect ( result ) . toEqual ( [
111+ { showLegend : true , id : 'layer1' , title : 'Layer 1' } ,
112+ { showLegend : false , id : 'layer2' , title : 'Layer 2' }
113+ ] ) ;
114+ } ) ;
115+
116+ it ( 'should return empty array when handler._getLegendInfos returns null' , ( ) => {
117+ mockHandler . _getLegendInfos . and . returnValue ( null ) ;
118+
119+ const result = instance . getLegendInfos ( ) ;
120+
121+ expect ( result ) . toEqual ( [ ] ) ;
122+ } ) ;
123+
124+ it ( 'should return empty array when handler._getLegendInfos returns undefined' , ( ) => {
125+ mockHandler . _getLegendInfos . and . returnValue ( undefined ) ;
126+
127+ const result = instance . getLegendInfos ( ) ;
128+
129+ expect ( result ) . toEqual ( [ ] ) ;
130+ } ) ;
131+
132+ it ( 'should return empty array when handler is null' , ( ) => {
133+ instance . _handler = null ;
134+
135+ const result = instance . getLegendInfos ( ) ;
136+
137+ expect ( result ) . toEqual ( [ ] ) ;
138+ } ) ;
139+ } ) ;
140+
141+ describe ( 'getLegendInfos with WebMapV2-like handler' , ( ) => {
142+ it ( 'should return legend info from _mapInfo.layers (WebMapV2)' , ( ) => {
143+ const webMapV2Handler = {
144+ _getLegendInfos : jasmine . createSpy ( '_getLegendInfos' ) . and . callFake ( function ( ) {
145+ const { layers = [ ] } = this . _mapInfo || { } ;
146+ return layers . map ( ( layer ) => {
147+ const { legendSetting, name, layerID : layerId } = layer ;
148+ return {
149+ showLegend : legendSetting ?. isShow !== false ,
150+ id : layerId ,
151+ title : name
152+ } ;
153+ } ) ;
154+ } ) ,
155+ _mapInfo : {
156+ layers : [
157+ { name : 'Layer1' , layerID : 'layer1' , legendSetting : { isShow : true } } ,
158+ { name : 'Layer2' , layerID : 'layer2' , legendSetting : { isShow : false } }
159+ ]
160+ }
161+ } ;
162+ instance . _handler = webMapV2Handler ;
163+
164+ const result = instance . getLegendInfos ( ) ;
165+
166+ expect ( webMapV2Handler . _getLegendInfos ) . toHaveBeenCalled ( ) ;
167+ expect ( result ) . toEqual ( [
168+ { showLegend : true , id : 'layer1' , title : 'Layer1' } ,
169+ { showLegend : false , id : 'layer2' , title : 'Layer2' }
170+ ] ) ;
171+ } ) ;
172+ } ) ;
173+
174+ describe ( 'getLegendInfos with WebMapV3-like handler' , ( ) => {
175+ it ( 'should return legend info from catalogs (WebMapV3)' , ( ) => {
176+ const webMapV3Handler = {
177+ _getLegendInfos : jasmine . createSpy ( '_getLegendInfos' ) . and . callFake ( function ( ) {
178+ const { catalogs = [ ] } = this . _mapResourceInfo || { } ;
179+ const res = [ ] ;
180+ const processCatalog = ( catalog ) => {
181+ const { catalogType, children, showLegend, title, layersContent, id } = catalog ;
182+ if ( catalogType === 'group' && children ) {
183+ children . forEach ( child => processCatalog ( child ) ) ;
184+ }
185+ if ( catalogType === 'layer' ) {
186+ res . push ( {
187+ showLegend : showLegend !== false ,
188+ id : layersContent || id ,
189+ title : title
190+ } ) ;
191+ }
192+ } ;
193+ catalogs . forEach ( item => processCatalog ( item ) ) ;
194+ return res ;
195+ } ) ,
196+ _mapResourceInfo : {
197+ catalogs : [
198+ {
199+ catalogType : 'layer' ,
200+ title : 'Layer1' ,
201+ showLegend : true ,
202+ layersContent : 'layer1'
203+ } ,
204+ {
205+ catalogType : 'group' ,
206+ children : [
207+ {
208+ catalogType : 'layer' ,
209+ title : 'Layer2' ,
210+ showLegend : false ,
211+ layersContent : 'layer2'
212+ }
213+ ]
214+ }
215+ ]
216+ }
217+ } ;
218+ instance . _handler = webMapV3Handler ;
219+
220+ const result = instance . getLegendInfos ( ) ;
221+
222+ expect ( webMapV3Handler . _getLegendInfos ) . toHaveBeenCalled ( ) ;
223+ expect ( result ) . toEqual ( [
224+ { showLegend : true , id : 'layer1' , title : 'Layer1' } ,
225+ { showLegend : false , id : 'layer2' , title : 'Layer2' }
226+ ] ) ;
227+ } ) ;
228+ } ) ;
229+ } ) ;
0 commit comments