1+ import { olExtends } from '../../../../src/openlayers/overlay/vectortile/olExtends.js' ;
2+ import { LineString } from 'ol/geom' ;
3+ import { Util } from '../../../../src/openlayers/core/Util' ;
4+
5+ describe ( 'olExtends' , ( ) => {
6+ let originalOl ;
7+ let originalGetOlVersion ;
8+ let originalLineStringGetFlatMidpoint ;
9+
10+ beforeEach ( ( ) => {
11+ originalOl = window . ol ;
12+ originalGetOlVersion = Util . getOlVersion ;
13+ originalLineStringGetFlatMidpoint = LineString . prototype . getFlatMidpoint ;
14+
15+ // 删除可能存在的原型方法
16+ delete LineString . prototype . getFlatMidpoint ;
17+
18+ window . ol = {
19+ format : {
20+ MVT : {
21+ prototype : { }
22+ }
23+ } ,
24+ render : {
25+ canvas : {
26+ Replay : {
27+ prototype : { }
28+ } ,
29+ Instruction : {
30+ SET_FILL_STYLE : 'setFillStyle'
31+ }
32+ }
33+ } ,
34+ geom : {
35+ flat : {
36+ textpath : { } ,
37+ orient : {
38+ linearRingIsClockwise : jasmine . createSpy ( 'linearRingIsClockwise' ) . and . returnValue ( true )
39+ }
40+ } ,
41+ GeometryType : {
42+ POINT : 'Point' ,
43+ LINE_STRING : 'LineString' ,
44+ POLYGON : 'Polygon' ,
45+ MULTI_POINT : 'MultiPoint' ,
46+ MULTI_LINE_STRING : 'MultiLineString'
47+ } ,
48+ GeometryLayout : {
49+ XY : 'XY'
50+ }
51+ } ,
52+ layer : {
53+ VectorTile : {
54+ prototype : { }
55+ }
56+ } ,
57+ renderer : {
58+ canvas : {
59+ VectorTileLayer : {
60+ prototype : { }
61+ }
62+ }
63+ } ,
64+ proj : {
65+ Units : {
66+ TILE_PIXELS : 'tile-pixels'
67+ } ,
68+ Projection : function ( options ) {
69+ this . code_ = options . code ;
70+ this . units_ = options . units ;
71+ }
72+ } ,
73+ math : {
74+ lerp : jasmine . createSpy ( 'lerp' ) . and . callFake ( ( a , b , t ) => a + ( b - a ) * t )
75+ }
76+ } ;
77+ } ) ;
78+
79+ afterEach ( ( ) => {
80+ window . ol = originalOl ;
81+ Util . getOlVersion = originalGetOlVersion ;
82+ LineString . prototype . getFlatMidpoint = originalLineStringGetFlatMidpoint ;
83+ } ) ;
84+
85+ it ( 'should add getFlatMidpoint method to LineString prototype if it does not exist' , ( ) => {
86+ expect ( LineString . prototype . getFlatMidpoint ) . toBeUndefined ( ) ;
87+ olExtends ( ) ;
88+ expect ( LineString . prototype . getFlatMidpoint ) . toBeDefined ( ) ;
89+ } ) ;
90+
91+ it ( 'should modify ol.format.MVT and ol.render.canvas.Replay methods when OpenLayers version is 4' , ( ) => {
92+ spyOn ( Util , 'getOlVersion' ) . and . returnValue ( '4' ) ;
93+ const targetMap = {
94+ getView : ( ) => ( {
95+ getProjection : ( ) => ( {
96+ getExtent : ( ) => [ 0 , 0 , 100 , 100 ]
97+ } )
98+ } )
99+ } ;
100+
101+ olExtends ( targetMap ) ;
102+
103+ expect ( window . ol . format . MVT . prototype . readProjection ) . toBeDefined ( ) ;
104+ expect ( window . ol . render . canvas . Replay . prototype . applyFill ) . toBeDefined ( ) ;
105+ } ) ;
106+
107+ it ( 'should not modify ol.format.MVT and ol.render.canvas.Replay methods when OpenLayers version is not 4' , ( ) => {
108+ spyOn ( Util , 'getOlVersion' ) . and . returnValue ( '6' ) ;
109+ olExtends ( ) ;
110+
111+ expect ( window . ol . format . MVT . prototype . readProjection ) . toBeUndefined ( ) ;
112+ expect ( window . ol . render . canvas . Replay . prototype . applyFill ) . toBeUndefined ( ) ;
113+ } ) ;
114+
115+ it ( 'should add ol.geom.flat.textpath.lineString method' , ( ) => {
116+ spyOn ( Util , 'getOlVersion' ) . and . returnValue ( '4' ) ;
117+ olExtends ( ) ;
118+ expect ( window . ol . geom . flat . textpath . lineString ) . toBeDefined ( ) ;
119+ } ) ;
120+
121+ it ( 'should add setFastRender and postCompose methods to VectorTileLayer and VectorTileRenderer' , ( ) => {
122+ spyOn ( Util , 'getOlVersion' ) . and . returnValue ( '4' ) ;
123+ const targetMap = {
124+ getView : ( ) => ( {
125+ getProjection : ( ) => ( {
126+ getExtent : ( ) => [ 0 , 0 , 100 , 100 ]
127+ } )
128+ } )
129+ } ;
130+
131+ olExtends ( targetMap ) ;
132+
133+ expect ( window . ol . layer . VectorTile . prototype . setFastRender ) . toBeDefined ( ) ;
134+ expect ( window . ol . renderer . canvas . VectorTileLayer . prototype . postCompose ) . toBeDefined ( ) ;
135+ } ) ;
136+ } ) ;
0 commit comments