tangjinzhou 6 years ago
parent
commit
82d0bbbf20
4 changed files with 33 additions and 6 deletions
  1. 2 1
      .eslintrc.js
  2. 4 4
      jest.config.js
  3. 4 1
      src/utils/auth.js
  4. 23 0
      src/utils/auth.spec.js

+ 2 - 1
.eslintrc.js

@@ -1,7 +1,8 @@
 module.exports = {
   root: true,
   env: {
-    node: true
+    node: true,
+    jest: true
   },
   extends: ["plugin:vue/essential", "@vue/prettier"],
   rules: {

+ 4 - 4
jest.config.js

@@ -11,8 +11,8 @@ module.exports = {
     "^@/(.*)$": "<rootDir>/src/$1"
   },
   snapshotSerializers: ["jest-serializer-vue"],
-  testMatch: [
-    "**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"
-  ],
-  testURL: "http://localhost/"
+  testMatch: ["**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"],
+  testURL: "http://localhost/",
+  collectCoverage: process.env.COVERAGE === "true",
+  collectCoverageFrom: ["src/**/*.{js,vue}", "!**/node_modules/**"]
 };

+ 4 - 1
src/utils/auth.js

@@ -1,5 +1,8 @@
+const currentAuth = ["admin"];
+export { currentAuth };
+
 export function getCurrentAuthority() {
-  return ["admin"];
+  return currentAuth;
 }
 
 export function check(authority) {

+ 23 - 0
src/utils/auth.spec.js

@@ -0,0 +1,23 @@
+import { check, currentAuth } from "./auth";
+
+describe("auth test", () => {
+  it("empty auth", () => {
+    currentAuth.splice(0, currentAuth.length);
+    expect(check(["user"])).toEqual(false);
+    expect(check(["admin"])).toEqual(false);
+  });
+
+  it("user auth", () => {
+    currentAuth.splice(0, currentAuth.length);
+    currentAuth.push("user");
+    expect(check(["user"])).toEqual(true);
+    expect(check(["admin"])).toEqual(false);
+  });
+
+  it("admin auth", () => {
+    currentAuth.push("admin");
+    expect(check(["user"])).toEqual(true);
+    expect(check(["admin"])).toEqual(true);
+    expect(check(["user", "admin"])).toEqual(true);
+  });
+});