Browse Source

jest 测试用例

YaolongHan 4 years ago
parent
commit
055ea06fd3

+ 15 - 3
persagy-web-big-edit/src/until.ts

@@ -38,6 +38,7 @@ export function svgTobase64(url: string) {
   return new Promise((relve, res) => {
     if (url.includes(".svg")) {
       axios.get(url).then((res) => {
+        let svgUrl = ''
         if (res.data) {
           const regexp = /\<\?xml .*\?\>/i,
             newXml = '<?xml version="1.0" encoding="UTF-8"?>';
@@ -46,12 +47,14 @@ export function svgTobase64(url: string) {
             res.data = '<?xml version="1.0" encoding="UTF-8"?>' + res.data;
           }
           const index = res.data.indexOf('"UTF-8"?>');
-          const str = res.data.substr(index + 9);
-          const a = crypto.enc.Utf8.parse(str);
+          // const str = res.data.substr(index + 9);
+          // const a = crypto.enc.Utf8.parse(str);
+          svgUrl = res.data.substr(index + 9);
+          const a = crypto.default.enc.Utf8.parse(svgUrl);
           baseUrl =
             "data:image/svg+xml;base64," + crypto.enc.Base64.stringify(a);
         }
-        relve(baseUrl)
+        relve([baseUrl, svgUrl])
       }).catch((err) => {
         res(err)
       });
@@ -61,4 +64,13 @@ export function svgTobase64(url: string) {
     }
   })
 
+}
+export function svgUrlTobase64(str) {
+  var div = document.createElement("div");
+  div.appendChild(str);
+  const svgUrl = crypto.default.enc.Utf8.parse(div.innerHTML);
+  const baseUrl =
+    "data:image/svg+xml;base64," + crypto.default.enc.Base64.stringify(svgUrl);
+  return baseUrl
+
 }

+ 57 - 0
persagy-web-draw/__tests__/types/SLine.test.ts

@@ -0,0 +1,57 @@
+import { SPoint } from "../../src";
+import { SLine } from "../../src";
+
+test("构造函数", () => {
+    // 无参数
+    let line = new SLine()
+    expect(line.p1).toStrictEqual(new SPoint(0, 0));
+    expect(line.p2).toStrictEqual(new SPoint(0, 0));
+
+    // 一个参数
+    let line1 = new SLine(line)
+    expect(line1.p1).toStrictEqual(new SPoint(0, 0));
+    expect(line1.p2).toStrictEqual(new SPoint(0, 0));
+    // 两个参数
+    let p1 = new SPoint(1, 2);
+    let p2 = new SPoint(2, 4);
+    let line2 = new SLine(p1,p2)
+    expect(line2.p1).toStrictEqual(p1);
+    expect(line2.p2).toStrictEqual(p2);
+
+    // 四个参数
+    let line3 = new SLine(0,0,2,2)
+    expect(line3.x1).toBe(0);
+    expect(line3.x2).toBe(2);
+    expect(line3.y1).toBe(0);
+    expect(line3.y2).toBe(2);
+});
+
+
+test("isNull", () => {
+    // 答案为 true
+    let line = new SLine();
+    expect(line.isNull()).toBeTruthy();
+    // 答案为 false
+    let line1 = new SLine(0,0,1,1);
+    expect(line1.isNull()).toBeFalsy();
+});
+
+test("center", () => {
+    let line = new SLine(0,0,2,2);
+    expect(line.center()).toStrictEqual(new SPoint(1,1));
+});
+
+test("translated", () => {
+    let line = new SLine(0,0,2,2);
+
+    expect(line.translated(2,2)).toStrictEqual(new SLine(2,2,4,4));
+});
+
+test("translate", () => {
+    let line = new SLine(0,0,2,2);
+    line.translate(2,2)
+    expect(line.x1).toBe(2);
+    expect(line.y1).toBe(2);
+    expect(line.x2).toBe(4);
+    expect(line.y2).toBe(4);
+});

+ 53 - 33
persagy-web-draw/__tests__/types/SPoint.test.ts

@@ -23,49 +23,69 @@
  *
  * *********************************************************************************************************************
  */
-
 import { SPoint } from "../../src";
+import { SMatrix } from "@persagy-web/base";
+import expect from "expect"
 
-test("构造函数", () => {
-    expect(new SPoint().toJson()).toBe("{\"x\":0,\"y\":0}");
-    expect(new SPoint(10, 10).toJson()).toBe("{\"x\":10,\"y\":10}");
-});
-
-test("属性修改", () => {
-    const p = new SPoint();
-    expect(p.toJson()).toBe("{\"x\":0,\"y\":0}");
-
-    p.y = 10;
-    expect(p.toJson()).toBe("{\"x\":0,\"y\":10}");
-    p.x = -10;
-    expect(p.toJson()).toBe("{\"x\":-10,\"y\":10}");
+/**
+ * 点类测试
+ *
+ * @author 韩耀龙 han_yao_long@163.com
+ */
 
-    p.setPoint(-100, -30);
-    expect(p.toJson()).toBe("{\"x\":-100,\"y\":-30}");
+test("构造函数", () => {
+    // 无参数
+    let p1 = new SPoint()
+    expect(p1.x).toBe(0);
+    expect(p1.y).toBe(0);
 
-    p.setPoint(new SPoint(-5, -12));
-    expect(p.toJson()).toBe("{\"x\":-5,\"y\":-12}");
+    // 一个参数
+    let p2 = new SPoint(1, 2);
+    expect(p2.x).toBe(1);
+    expect(p2.y).toBe(2);
+    // 两个参数
+    let p3 = new SPoint(p2);
+    expect(p3.x).toBe(1);
+    expect(p3.y).toBe(2);
 });
 
-test("setPoint()", () => {
-    const p = new SPoint();
-    p.setPoint(-100, -30);
-    expect(p.toJson()).toBe("{\"x\":-100,\"y\":-30}");
+test("setPoint", () => {
+    // 无参数
+    let p1 = new SPoint()
+    expect(p1.x).toBe(0);
+    expect(p1.y).toBe(0);
+    // 一个参数
+    p1.setPoint(1, 2)
+    expect(p1.x).toBe(1);
+    expect(p1.y).toBe(2);
+    // 两个参数
+    let p3 = new SPoint();
+    p3.setPoint(p1)
+    expect(p3.x).toBe(1);
+    expect(p3.y).toBe(2);
+});
 
-    p.setPoint(new SPoint(-5, -12));
-    expect(p.toJson()).toBe("{\"x\":-5,\"y\":-12}");
+test("manhattanLength", () => {
+    let p1 = new SPoint(1, 2)
+    expect(p1.manhattanLength()).toBe(3);
 });
 
-test("manhattanLength()", () => {
-    const p = new SPoint(10, 20);
-    expect(p.manhattanLength()).toBe(30);
+test("translate", () => {
+    let p1 = new SPoint(1, 2);
+    p1.translate(1, 2)
+    expect(p1.x).toBe(2);
+    expect(p1.y).toBe(4);
+});
 
-    p.setPoint(-10, 20);
-    expect(p.manhattanLength()).toBe(30);
+test("matrixTransform", () => {
+    let p1 = new SPoint(1, 2);
+    // 位移
+    const sm = new SMatrix().translate(1,2)
+    expect(p1.matrixTransform(sm)).toStrictEqual(new SPoint(2,4));
 
-    p.setPoint(10, -20);
-    expect(p.manhattanLength()).toBe(30);
+    // 放大
+    const sm1 = new SMatrix().scale(2,2);
 
-    p.setPoint(-10, -20);
-    expect(p.manhattanLength()).toBe(30);
+    expect(p1.matrixTransform(sm1)).toStrictEqual(new SPoint(2,4));
+    // 旋转--待测
 });

+ 168 - 0
persagy-web-draw/__tests__/types/SRect.test.ts

@@ -0,0 +1,168 @@
+/*
+ * *********************************************************************************************************************
+ *
+ *          !!
+ *        .F88X
+ *        X8888Y
+ *      .}888888N;
+ *        i888888N;        .:!              .I$WI:
+ *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
+ *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
+ *            +888888N;  .8888888Y                                  "&&8Y.}8,
+ *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
+ *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
+ *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
+ *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
+ *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
+ *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
+ *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
+ *      .:R888888I
+ *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
+ *        ~8888'
+ *        .!88~                                                                     All rights reserved.
+ *
+ * *********************************************************************************************************************
+ */
+
+import { SPoint } from "../../src";
+import { SRect, SSize } from "../../src";
+import { SMatrix } from "@persagy-web/base";
+import expect from "expect"
+
+/**
+ * 矩形测试
+ *
+ * @author 韩耀龙 han_yao_long@163.com
+ */
+
+test("构造函数", () => {
+    // 无参数
+    let rect = new SRect()
+    expect(rect.leftTop).toStrictEqual(new SPoint(0, 0));
+    expect(rect.size).toStrictEqual(new SSize(0, 0));
+
+    // 两个参数
+    const leftPoint = new SPoint(0, 0);
+    const rightPoint = new SPoint(2, 2)
+    let rect1 = new SRect(leftPoint, rightPoint)
+    expect(rect1.leftTop).toStrictEqual(new SPoint(0, 0));
+    expect(rect1.size).toStrictEqual(new SSize(2, 2));
+
+    // 四个参数
+    let rect2 = new SRect(0, 0, 4, 4)
+    expect(rect2.leftTop).toStrictEqual(new SPoint(0, 0));
+    expect(rect2.size).toStrictEqual(new SSize(4, 4));
+});
+
+test("isEmpty", () => {
+    // false
+    let rect = new SRect()
+    expect(rect.isEmpty()).toBeTruthy();
+    // true
+    let rect1 = new SRect(0, 0, 4, 4)
+    expect(rect1.isEmpty()).toBeFalsy();
+});
+
+test("isNull", () => {
+    // false
+    let rect = new SRect()
+    expect(rect.isNull()).toBeTruthy();
+    // true
+    let rect1 = new SRect(0, 0, 4, 4)
+    expect(rect1.isNull()).toBeFalsy();
+});
+
+test("isValid,", () => {
+    //  width 与 height 都大于 0,返回 true,否则返回 false。
+    // ture
+    let rect = new SRect()
+    expect(rect.isValid()).toBeFalsy();
+    // true
+    let rect1 = new SRect(0, 0, 4, 4)
+    expect(rect1.isValid()).toBeTruthy();
+});
+
+test("isIn", () => {
+    //  width 与 height 都大于 0,返回 true,否则返回 false。
+    // ture
+    let rect: SRect = new SRect(0, 0, 4, 4)
+    const inRect = new SRect()
+    expect(rect.isIn(inRect)).toBeTruthy();
+    // false
+    const inRect1 = new SRect(0, 0, 5, 5)
+    expect(rect.isIn(inRect1)).toBeFalsy();
+});
+
+test("contains", () => {
+    // ture
+    let rect = new SRect(0, 0, 4, 4)
+    expect(rect.contains(2, 2)).toBeTruthy();
+    // false
+    expect(rect.contains(5, 5)).toBeFalsy();
+});
+
+test("center", () => {
+    let rect = new SRect(0, 0, 4, 4)
+    expect(rect.center()).toStrictEqual(new SPoint(2, 2));
+});
+
+test("translate", () => {
+    let rect = new SRect(0, 0, 4, 4);
+    rect.translate(2, 2)
+    expect(rect.x).toBe(2);
+    expect(rect.y).toBe(2);
+});
+
+test("translated", () => {
+    let rect = new SRect(0, 0, 4, 4);
+    expect(rect.translated(2, 2)).toStrictEqual(new SRect(2, 2, 4, 4));
+});
+
+test("adjust", () => {
+    let rect = new SRect(0, 0, 4, 4);
+    rect.adjust(2, 2, 2, 2)
+    expect(rect.x).toBe(2);
+    expect(rect.x).toBe(2);
+    expect(rect.width).toBe(6);
+    expect(rect.height).toBe(6);
+});
+
+test("adjusted", () => {
+    let rect = new SRect(0, 0, 4, 4);
+    expect(rect.adjusted(2, 2, 2, 2)).toStrictEqual(new SRect(2, 2, 6, 6));
+});
+
+test("union", () => {
+    let rect = new SRect(0, 0, 4, 4);
+    let rect1 = new SRect(2, 2, 4, 4);
+    rect.union(rect1)
+    expect(rect.x).toBe(0);
+    expect(rect.x).toBe(0);
+    expect(rect.width).toBe(6);
+    expect(rect.height).toBe(6);
+});
+
+test("unioned", () => {
+    let rect = new SRect(0, 0, 4, 4);
+    let rect1 = new SRect(2, 2, 4, 4);
+    expect(rect.unioned(rect1)).toStrictEqual(new SRect(0, 0, 6, 6));
+});
+
+test("intersected", () => {
+    let rect = new SRect(0, 0, 4, 4);
+    let rect1 = new SRect(2, 2, 4, 4);
+    expect(rect.intersected(rect1)).toStrictEqual(new SRect(2, 2, 2, 2));
+    let rect2 = new SRect(5, 5, 4, 4);
+    expect(rect.intersected(rect2)).toStrictEqual(new SRect());
+});
+
+test("intersect", () => {
+    let rect = new SRect(0, 0, 4, 4);
+    let rect1 = new SRect(2, 2, 4, 4);
+    rect.intersected(rect1)
+    expect(rect).toStrictEqual(new SRect(2, 2, 2, 2));
+    let rect2 = new SRect(5, 5, 4, 4);
+    let rect3 = new SRect(1, 1, 1, 1);
+    rect2.intersected(rect3)
+    expect(rect2).toStrictEqual(new SRect());
+});

+ 97 - 0
persagy-web-draw/__tests__/types/SSize.test.ts

@@ -0,0 +1,97 @@
+/*
+ * *********************************************************************************************************************
+ *
+ *          !!
+ *        .F88X
+ *        X8888Y
+ *      .}888888N;
+ *        i888888N;        .:!              .I$WI:
+ *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
+ *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
+ *            +888888N;  .8888888Y                                  "&&8Y.}8,
+ *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
+ *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
+ *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
+ *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
+ *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
+ *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
+ *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
+ *      .:R888888I
+ *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
+ *        ~8888'
+ *        .!88~                                                                     All rights reserved.
+ *
+ * *********************************************************************************************************************
+ */
+
+import { SSize,SAspectRatioMode } from "../../src";
+import expect from "expect"
+
+/**
+ * 矩形测试
+ *
+ * @author 韩耀龙 han_yao_long@163.com
+ */
+
+test("构造函数", () => {
+
+    // 无参数
+    let size = new SSize()
+    expect(size.width).toBe(0);
+    expect(size.height).toBe(0);
+
+    // 一个参数
+    let size1 = new SSize(1,1)
+    let size2 = new SSize(size1)
+    expect(size2.width).toBe(1);
+    expect(size2.height).toBe(1);
+
+    // 两个参数
+
+    let size3 = new SSize(3,3)
+    expect(size3.width).toBe(3);
+    expect(size3.height).toBe(3);
+});
+
+
+test("isEmpty", () => {
+    // true
+    let size = new SSize()
+    expect(size.isEmpty()).toBeTruthy();
+    // false
+    let size1 = new SSize(4, 4)
+    expect(size1.isEmpty()).toBeFalsy();
+});
+
+
+test("isNull", () => {
+    // true
+    let size = new SSize()
+    expect(size.isNull()).toBeTruthy();
+    // false
+    let size1 = new SSize(4, 4)
+    expect(size1.isNull()).toBeFalsy();
+});
+
+test("isValid", () => {
+    // true
+    let size = new SSize()
+    expect(size.isNull()).toBeFalsy();
+    // false
+    let size1 = new SSize(4, 4)
+    expect(size1.isNull()).toBeTruthy();
+});
+
+test("scale", () => {
+    let size1 = new SSize(4, 4);
+    size1.scale(2,2)
+    expect(size1.width).toBe(2);
+    expect(size1.height).toBe(2);
+});
+
+test("scaled", () => {
+    let size1 = new SSize(4, 4);
+    size1.scaled(2,2)
+    expect(size1.width).toBe(8);
+    expect(size1.height).toBe(8);
+});

+ 3 - 1
persagy-web-draw/jest.config.js

@@ -28,7 +28,9 @@ module.exports = {
     preset: "ts-jest",
     moduleFileExtensions: ["js", "ts"],
     transform: {
-        "^.+\\.tsx?$": "ts-jest"
+        "^.+\\.tsx?$": "ts-jest",
+         // typescript转换
+        '^.+\\.ts?$': 'ts-jest'
     },
     transformIgnorePatterns: ["/node_modules/"],
     moduleNameMapper: {

+ 3 - 1
persagy-web-draw/package.json

@@ -9,6 +9,7 @@
         "publish": "npm publish",
         "lint": "eslint src/**/*.{js,ts,tsx}",
         "test": "jest",
+        "test-c": "jest --coverage",
         "typedoc": "typedoc --hideGenerator src ../persagy-web-base/src"
     },
     "keywords": [
@@ -28,7 +29,8 @@
         "node-ssh": "^6.0.0",
         "prettier": "^1.18.2",
         "@types/jest": "^24.0.15",
-        "ts-jest": "^24.0.2",
+        "jest": "^26.6.3",
+        "ts-jest": "^26.5.0",
         "jest-canvas-mock": "^2.1.0",
         "typescript": "^3.5.3"
     },

+ 1 - 1
persagy-web-draw/src/SCanvasView.ts

@@ -435,7 +435,7 @@ export class SCanvasView extends SObject {
      *
      * @param callback        回调函数
      */
-    private requestAnimationFrame(callback: FrameRequestCallback): number {
+    private requestAnimationFrame(callback: FrameRequestCallback): any {
         let currTime = new Date().getTime();
         let timeToCall = Math.max(0, 16 - (currTime - this._lastFrameTime));
         let id = setTimeout(function(): void {

+ 1 - 0
persagy-web-draw/tsconfig.json

@@ -9,6 +9,7 @@
         "removeComments": true,                     // 去除注释
         "noImplicitAny": true,                      // 在表达式和声明上有隐含的 any类型时报错。
         "esModuleInterop": true,                    // 支持别名导入
+        "types": ["jest","node","@types/jest"],
         "moduleResolution": "node"                  // 此处设置为node,才能解析import xx from 'xx'
     },
     "include": ["./src"],