Van 6 years ago
parent
commit
e0069a1bd5

+ 2 - 1
.gitignore

@@ -2,4 +2,5 @@
 .DS_Store
 .DS_Store
 /dist
 /dist
 /node_modules
 /node_modules
-/demo/dist
+/demo/dist
+/coverage

+ 0 - 5
__test__/base.spec.ts

@@ -1,5 +0,0 @@
-import { commandable } from '../src/ts/util/commandable';
-
-test('basic', () => {
-    expect(commandable()).toBe(true);
-});

+ 107 - 0
__test__/util/Options.test.ts

@@ -0,0 +1,107 @@
+import {Options} from '../../src/ts/util/Options';
+
+describe('Options', () => {
+    test('Options toolbar', () => {
+        const options = new Options({
+            toolbar: ['br', 'fullscreen', {
+                hotkey: "⌘-a",
+                name: "preview",
+            }]
+        });
+        expect(options.merge()).toMatchObject({
+            toolbar: [{
+                name: "br",
+            }, {
+                hotkey: "⌘-'",
+                name: "fullscreen",
+                tipPosition: "nw",
+            }, {
+                hotkey: "⌘-a",
+                name: "preview",
+                tipPosition: "nw",
+            }],
+        })
+    });
+
+    test('Options upload', () => {
+        const options = new Options({
+            upload: {
+                accept: '.jpg'
+            }
+        });
+        expect(options.merge()).toMatchObject({
+            upload: {
+                filename: expect.anything(),
+                linkToImgUrl: "",
+                max: 10 * 1024 * 1024,
+                url: "",
+                accept: '.jpg'
+            },
+        })
+    })
+
+    test('Options classes', () => {
+        const options = new Options({
+            classes: {
+                preview: "content-reset",
+            },
+        });
+        expect(options.merge()).toMatchObject({
+            classes: {
+                preview: "content-reset",
+            },
+        })
+    });
+
+
+    test('Options preview', () => {
+        const options = new Options({
+            preview: {
+                url: 'https://hacpai.com/md',
+                show: true,
+            },
+        });
+        expect(options.merge()).toMatchObject({
+            preview: {
+                url: 'https://hacpai.com/md',
+                delay: 1000,
+                show: true,
+            },
+        })
+    });
+
+    test('Options hint', () => {
+        const options = new Options({
+            hint: {
+                emojiTail: '前往设置',
+                emoji: {
+                    "+1": "👍",
+                },
+            },
+        });
+        expect(options.merge()).toMatchObject({
+            hint: {
+                delay: 200,
+                emojiTail: '前往设置',
+                emoji: {
+                    "+1": "👍",
+                },
+                emojiPath: "https://cdn.jsdelivr.net/npm/vditor/src/assets/emoji",
+            },
+        })
+    });
+
+    test('Options resize', () => {
+        const options = new Options({
+            resize: {
+                enable: true,
+            },
+        })
+        expect(options.merge()).toMatchObject({
+            resize: {
+                enable: true,
+                position: "bottom",
+            },
+        })
+    });
+})

+ 12 - 0
__test__/util/commandable.test.ts

@@ -0,0 +1,12 @@
+import { commandable } from '../../src/ts/util/commandable';
+
+test('commandable', () => {
+    expect(commandable()).toBeTruthy()
+
+    Object.defineProperty(navigator, 'userAgent', {
+        writable: true,
+        value: 'Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0'
+    })
+
+    expect(commandable()).toBeFalsy()
+});

+ 3 - 3
jest.config.js

@@ -15,7 +15,7 @@ module.exports = {
   // cacheDirectory: "/private/var/folders/bm/7369j389267bsy4rhs0t51km0000gn/T/jest_dx",
   // cacheDirectory: "/private/var/folders/bm/7369j389267bsy4rhs0t51km0000gn/T/jest_dx",
 
 
   // Automatically clear mock calls and instances between every test
   // Automatically clear mock calls and instances between every test
-  clearMocks: true,
+  // clearMocks: true,
 
 
   // Indicates whether the coverage information should be collected while executing the test
   // Indicates whether the coverage information should be collected while executing the test
   // collectCoverage: false,
   // collectCoverage: false,
@@ -149,7 +149,7 @@ module.exports = {
   // ],
   // ],
 
 
   // The regexp pattern or array of patterns that Jest uses to detect test files
   // The regexp pattern or array of patterns that Jest uses to detect test files
-  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
+  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$",
 
 
   // This option allows the use of a custom results processor
   // This option allows the use of a custom results processor
   // testResultsProcessor: null,
   // testResultsProcessor: null,
@@ -165,7 +165,7 @@ module.exports = {
 
 
   // A map from regular expressions to paths to transformers
   // A map from regular expressions to paths to transformers
   transform: {
   transform: {
-    "^.+\\.tsx?$": "ts-jest",
+    "^.+\\.ts$": "ts-jest",
   },
   },
 
 
   // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
   // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation

+ 2 - 1
package.json

@@ -53,7 +53,8 @@
     "lint": "tslint --fix -c tslint.json 'src/**/*.ts'",
     "lint": "tslint --fix -c tslint.json 'src/**/*.ts'",
     "dev": "webpack --config webpack.dev.js",
     "dev": "webpack --config webpack.dev.js",
     "start": "webpack-dev-server --config webpack.start.js",
     "start": "webpack-dev-server --config webpack.start.js",
-    "test": "jest"
+    "test:watch": "jest --watch",
+    "test": "jest --coverage"
   },
   },
   "keywords": [
   "keywords": [
     "editor",
     "editor",

+ 1 - 1
src/ts/types/index.d.ts

@@ -50,7 +50,7 @@ interface IClasses {
 }
 }
 
 
 interface IUpload {
 interface IUpload {
-    url: string;
+    url?: string;
     max?: number;
     max?: number;
     linkToImgUrl?: string;
     linkToImgUrl?: string;
     success?: (textarea: HTMLTextAreaElement, msg: string) => void;
     success?: (textarea: HTMLTextAreaElement, msg: string) => void;

+ 1 - 1
src/ts/util/Options.ts

@@ -146,7 +146,7 @@ export class Options {
             name: "br",
             name: "br",
         }],
         }],
         upload: {
         upload: {
-            filename: (name) => name.replace(/\W/g, ""),
+            filename: (name: string) => name.replace(/\W/g, ""),
             linkToImgUrl: "",
             linkToImgUrl: "",
             max: 10 * 1024 * 1024,
             max: 10 * 1024 * 1024,
             url: "",
             url: "",