Skip to content

Commit 610df3c

Browse files
committed
增加conmon工具类的单元测试 review by sunxy
1 parent 2aa8ad6 commit 610df3c

File tree

3 files changed

+348
-1
lines changed

3 files changed

+348
-1
lines changed

src/common/commontypes/Util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ SuperMap.Util.lineIntersection = function (a1, a2, b1, b2) {
10441044

10451045
/**
10461046
* @description 获取文本外接矩形宽度与高度。
1047-
* @param {SuperMap.Style} style - 文本样式。
1047+
* @param {SuperMap.ThemeStyle} style - 文本样式。
10481048
* @param {string} text - 文本内容。
10491049
* @param {Object} element - DOM 元素。
10501050
* @returns {Object} 返回裁剪后的宽度,高度信息。
Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
import { Util } from '../../../src/common/commontypes/Util';
2+
import {SuperMap} from "@supermap/iclient-common";
3+
4+
describe('Util', () =>{
5+
6+
it('extend', () => {
7+
var source = {
8+
"ch": 791.998416,
9+
"cm": 0.3937,
10+
"dm": 3936.9999999999995,
11+
"fath": 71.999856,
12+
"ind - ch": 791.9942845122
13+
};
14+
var destination = {
15+
"50kilometers":5,
16+
"150kilometers":5,
17+
"BenoitChain":7,
18+
"BenoitLink":7,
19+
"Brealey":14763.75,
20+
};
21+
var result = Util.extend(destination, source);
22+
expect(result).toEqual(destination);
23+
});
24+
25+
it('copy', () => {
26+
var des={
27+
"fath": 71.999856,
28+
"ind": 791.9942845122
29+
};
30+
var soc = {
31+
"fath": 6,
32+
"ind": 9
33+
};
34+
Util.copy(des,soc);
35+
expect(des.fath).toEqual(6);
36+
expect(des.ind).toEqual(9);
37+
});
38+
39+
it('reset', () => {
40+
var obj={"apple":1, "bear":2, banana:{"orange":3},name:['Alice','Jack']};
41+
Util.reset(obj);
42+
expect(obj.apple).toBe(null);
43+
expect(obj.bear).toBe(null);
44+
expect(obj.banana).toBe(null);
45+
expect(obj.name).toBe(null);
46+
});
47+
48+
it('getElement', () => {
49+
var resulrArr=Util.getElement('string',1,2,3);
50+
expect(resulrArr[1]).toEqual(1);
51+
expect(resulrArr[2]).toEqual(2);
52+
});
53+
54+
it('isElement', () => {
55+
var o={a:1,b:2};
56+
var elements=Util.isElement(o);
57+
expect(elements).toEqual (false);
58+
});
59+
60+
it('isArray',()=>{
61+
var arr=[1,2];
62+
var obj=Util.isArray(arr);
63+
expect(obj).toEqual(true);
64+
});
65+
66+
it('removeItem', () => {
67+
var arr=[1,2,3,4,5];
68+
var item=5;
69+
var arrResult=Util.removeItem(arr,item);
70+
expect(arrResult).toEqual([1,2,3,4]);
71+
});
72+
73+
it('indexOf', () => {
74+
//arr为null的情况
75+
var arr=null;
76+
var obj1={a:"lll"};
77+
var re=Util.indexOf(arr,obj1);
78+
expect(re).toEqual(-1);
79+
//array不为null的情况
80+
var array=[1,2,3,{ hobby:"dancing"}];
81+
var obj={hobby:"dancing"};
82+
//备份js语言的数组的indexOf方法
83+
var backUpMethod = array.indexOf;
84+
//去掉js语言的数组的indexOf方法
85+
array.indexOf=null;
86+
//测试
87+
var result=Util.indexOf(array,obj);
88+
expect(result).toEqual(-1);
89+
//从备份恢复js语言的数组的indexOf方法
90+
//array.indexOf=backUpMethod;
91+
92+
});
93+
94+
it('modifyDOMElement', () => {
95+
var testDom = document.createElement("div");
96+
var id="box";
97+
var px = new SuperMap.Pixel(99,80);
98+
var sz = new SuperMap.Size(99,80);
99+
var position="absolute";
100+
var border="1px solid red";
101+
var overflow="hidden";
102+
//opacity<1.0
103+
var opacity = 0.5;
104+
Util.modifyDOMElement(testDom,id,px,sz,position,border,overflow,opacity);
105+
expect(testDom.id).toBe("box");
106+
expect(testDom.style.opacity).toBe('0.5');
107+
//opacity=1.0
108+
var opa = 1.0;
109+
Util.modifyDOMElement(testDom,id,px,sz,position,border,overflow,opa);
110+
expect(testDom.style.opa).toBe();
111+
});
112+
113+
it('applyDefaults', () => {
114+
var test;
115+
var to = {
116+
advantage:test
117+
};
118+
var from = {
119+
toString:"World",
120+
advantage:"difficult"
121+
};
122+
var result=Util.applyDefaults(to,from);
123+
expect(result.advantage).toEqual(from.advantage);
124+
expect(result.toString).toEqual(from.toString);
125+
});
126+
127+
it('getParameterString', () => {
128+
var params={
129+
type:"json",
130+
coordinates:["abc","edf"],
131+
properties:{
132+
constructor:[1,2,3]
133+
}
134+
};
135+
var paramsArr=Util.getParameterString(params);
136+
expect(paramsArr).toBe("type=json&coordinates=abc,edf&properties=%5Bobject%20Object%5D");
137+
});
138+
139+
it('urlAppend', () => {
140+
var url="http:/www.baidu.com";
141+
var paramsStr="returnContent=true";
142+
var newUrl=Util.urlAppend(url,paramsStr);
143+
expect(newUrl).toBe("http:/www.baidu.com?returnContent=true");
144+
});
145+
146+
it('toFloat',()=>{
147+
var number="1.234567890345678912345612234555667";
148+
var precision=null;
149+
var newNumber=Util.toFloat(number,precision);
150+
expect(newNumber).toEqual(1.2345678903457);
151+
});
152+
153+
it('rad',()=>{
154+
var angle=720;
155+
var result=Util.rad(angle);
156+
expect(result).toBe(12.566370614359172);
157+
});
158+
159+
it('getParameters',()=>{
160+
var URL=" http://54.223.164.155:8090/iserver/services/addressmatch-Address/restjsr/v1/address?a=1&b=2";
161+
var parameters=Util.getParameters(URL);
162+
expect(parameters).toEqual({ a: "1", b: "2" });
163+
});
164+
it('normalizeScale',()=>{
165+
var scale=1/10000000000;
166+
var normScale=Util.normalizeScale(scale);
167+
expect(normScale).toBe(1e-10);
168+
});
169+
170+
it('getResolutionFromScale',()=>{
171+
var scale=1/1250000000;
172+
var units=null;
173+
var resolution=Util.getResolutionFromScale(scale,units);
174+
expect(resolution).toEqual(2.97635783253946);
175+
});
176+
177+
it('getScaleFromResolution',()=>{
178+
var getScaleFromResolution=2.97635783253946;
179+
var units=null;
180+
var scale=Util.getScaleFromResolution(getScaleFromResolution,units);
181+
expect(scale).toEqual(1250000000);
182+
});
183+
184+
185+
it('isInTheSameDomain',()=>{
186+
var Url="http://iclient.supermap.io/examples/leaflet/editor.html#addressMatchService";
187+
var correct=Util.isInTheSameDomain(Url);
188+
expect(correct).toBeFalsy(false);
189+
190+
var errorUrl="httttttp:wwwwwww.bbbb";
191+
var error=Util.isInTheSameDomain(errorUrl);
192+
expect(error).toBeTruthy(true);
193+
});
194+
195+
it("calculateDpi",()=>{
196+
var viewBounds = new SuperMap.Bounds(-65.71902951325971,-65.71902951331374,65.71902951333179,65.71902951327776);
197+
var viewer = new SuperMap.Size(256,256);
198+
var scale=4.629244301712164E-9;
199+
var coordUnit="DEGREE";
200+
var datumAxis=6378137;
201+
var dpi=Util.calculateDpi(viewBounds,viewer,scale,coordUnit,datumAxis);
202+
expect(dpi).toEqual(95.99999999999964);
203+
});
204+
205+
it('toJSON',()=>{
206+
//1、obj为null
207+
var nullObj=null;
208+
var result=Util.toJSON(nullObj);
209+
expect(result).toBe(null);
210+
//2、obj不为null
211+
//var date = new Date();
212+
//console.log(date);
213+
var obj = {
214+
resourceConfigID: "maps",
215+
resourceType: "CatalogList",
216+
array:[1,2,3,4,5,6],
217+
1:1,
218+
true:true,
219+
time:"Wed Sep 19 2018 15:33:53 GMT+0800 (中国标准时间)"
220+
};
221+
var jsonObj=Util.toJSON(obj);
222+
expect(jsonObj).toEqual(`{'1':1,'resourceConfigID':"maps",'resourceType':"CatalogList",'array':[1,2,3,4,5,6],'true':true,'time':"Wed Sep 19 2018 15:33:53 GMT+0800 (中国标准时间)"}`);
223+
224+
//obj[ "toJSON"]为function
225+
var funObj = {
226+
"toJSON":function(){
227+
return "aaa";
228+
}
229+
};
230+
var funResults=Util.toJSON(funObj);
231+
expect(funResults).toEqual("aaa");
232+
});
233+
234+
it('getResolutionFromScaleDpi',()=>{
235+
var scale=4.629244301712164E-9;
236+
var dpi=95.99999999999964;
237+
var coordUnit="DEGREE";
238+
var datumAxis=6378137;
239+
var result=Util.getResolutionFromScaleDpi(scale,dpi,coordUnit,datumAxis);
240+
expect(result).toEqual(0.513429918072625);
241+
242+
});
243+
244+
it('getScaleFromResolutionDpi',()=>{
245+
var resolution=0.513429918072625;
246+
var dpi=95.99999999999964;
247+
var coordUnit="DEGREE";
248+
var datumAxis=6378137;
249+
var result=Util.getScaleFromResolutionDpi(resolution,dpi,coordUnit,datumAxis);
250+
expect(result).toEqual(4.629244301712165e-9);
251+
});
252+
253+
it('transformResult',()=>{
254+
var result={
255+
true:true,
256+
respose:"helloWorld"
257+
};
258+
Util.transformResult(result);
259+
expect(result).toEqual({"true":true,"respose":"helloWorld"})
260+
261+
});
262+
it('copyAttributes',()=>{
263+
var soc={
264+
name:"Lucy",
265+
height:90,
266+
age:18,
267+
birthday:"八月十九",
268+
};
269+
var des={};
270+
Util.copyAttributes(des,soc);
271+
expect(des).toEqual(soc);
272+
});
273+
274+
it('copyAttributesWithClip',()=>{
275+
var source={
276+
name:"Lucy",
277+
say:function(){
278+
console.log(this.name);
279+
},
280+
hobby:"dancing",
281+
age:18,
282+
height:160,
283+
"CLASS_NAME":123
284+
};
285+
var destination={
286+
name:"xixi",
287+
hobby:"sing",
288+
age:20,
289+
height:180
290+
};
291+
var clip=[
292+
"say","CLASS_NAME"
293+
];
294+
Util.copyAttributesWithClip(source,destination,clip);
295+
expect(destination.name).toEqual(source.name);
296+
expect(destination.hobby).toEqual(source.hobby);
297+
expect(destination.age).toEqual(source.age);
298+
expect(destination.height).toEqual(source.height);
299+
});
300+
301+
it('cloneObject',()=>{
302+
var needCloneObj={
303+
hobby:"dancing",
304+
age:18,
305+
height:160
306+
};
307+
var copy=Util.cloneObject(needCloneObj);
308+
expect(copy).toEqual(needCloneObj);
309+
});
310+
311+
it('lineIntersection',()=>{
312+
//重合
313+
var a1 = new SuperMap.Geometry.Point(-111.04, 45.68);
314+
var a2 = new SuperMap.Geometry.Point(-152, 89);
315+
var a3 = new SuperMap.Geometry.Point(-111.04, 45.68);
316+
var a4 = new SuperMap.Geometry.Point(-152, 89);
317+
var intersectValue=Util.lineIntersection(a1,a2,a3,a4);
318+
expect(intersectValue).toEqual("Coincident");
319+
//平行
320+
var a5 = new SuperMap.Geometry.Point(20, 80);
321+
var a6 = new SuperMap.Geometry.Point(140, 160);
322+
var a7 = new SuperMap.Geometry.Point(20, 20);
323+
var a8 = new SuperMap.Geometry.Point(140, 100);
324+
var intersectValue1 =Util.lineIntersection(a5,a6,a7,a8);
325+
expect(intersectValue1).toEqual("Parallel");
326+
});
327+
328+
it('getTextBounds',()=>{
329+
var style = new SuperMap.ThemeStyle({
330+
width:200,
331+
height:300,
332+
fontSize:12,
333+
fontFamily:"宋体",
334+
fontWeight:"bold",
335+
position:"relative",
336+
visibility:"hidden",
337+
display:"inline-block",
338+
});
339+
var text = "北京超图";
340+
var dom = document.createElement('div');
341+
var result=Util.getTextBounds(style,text,dom);
342+
expect(result.textWidth).toEqual(65);
343+
expect(result.textHeight).toEqual(18);
344+
});
345+
346+
});

test/test-main-common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ import './common/commonTypes/geometry/CollectionSpec.js';
112112
import './common/commonTypes/geometry/GeoTextSpec.js';
113113
import './common/commonTypes/geometry/LineStringSpec.js';
114114
import './common/commonTypes/geometry/LinearRingSpec.js';
115+
import './common/commontypes/geometry/UtilSpec.js';
115116

116117
/**common -- overlay**/
117118
import './common/overlay/feature/ShapeFactorySpec.js';

0 commit comments

Comments
 (0)