Jelajahi Sumber

add test for validateThemePluginGrowiDirective

Yuki Takei 1 tahun lalu
induk
melakukan
73d4e245a7

+ 50 - 0
packages/pluginkit/src/v4/server/utils/theme/validate-growi-plugin-directive.spec.ts

@@ -0,0 +1,50 @@
+import path from 'path';
+
+import { isGrowiThemeMetadata } from '@growi/core';
+
+import { validateThemePluginGrowiDirective } from './validate-growi-plugin-directive';
+
+
+describe('validateThemePluginGrowiDirective()', () => {
+
+  it('returns a data object', async() => {
+    // setup
+    const projectDirRoot = path.resolve(__dirname, '../../../../../test/fixtures/example-package/theme1');
+
+    // when
+    const data = validateThemePluginGrowiDirective(projectDirRoot);
+
+    // then
+    expect(data).not.toBeNull();
+    expect(data.growiPlugin).not.toBeNull();
+    expect(data.themes).not.toBeNull();
+    expect(isGrowiThemeMetadata(data.themes[0])).toBeTruthy();
+  });
+
+  describe('should throw an GrowiPluginValidationError', () => {
+
+    it("when the pkg does not have 'growiPlugin.themes' directive", () => {
+      // setup
+      const projectDirRoot = path.resolve(__dirname, '../../../../../test/fixtures/example-package/invalid-theme1');
+
+      // when
+      const caller = () => { validateThemePluginGrowiDirective(projectDirRoot) };
+
+      // then
+      expect(caller).toThrow("Theme plugin must have 'themes' array and that must have one or more theme metadata");
+    });
+
+    it("when the pkg does not have 'growiPlugin.locale' directive", () => {
+      // setup
+      const projectDirRoot = path.resolve(__dirname, '../../../../../test/fixtures/example-package/invalid-theme2');
+
+      // when
+      const caller = () => { validateThemePluginGrowiDirective(projectDirRoot) };
+
+      // then
+      expect(caller).toThrow(/^Some of theme metadata are invalid:/);
+    });
+
+  });
+
+});

+ 2 - 2
packages/pluginkit/src/v4/server/utils/theme/validate-growi-plugin-directive.ts

@@ -11,7 +11,7 @@ import { validateGrowiDirective } from '../common';
  * @param projectDirRoot
  */
 export const validateThemePluginGrowiDirective = (projectDirRoot: string): GrowiThemePluginValidationData => {
-  const data = validateGrowiDirective(projectDirRoot, GrowiPluginType.Template);
+  const data = validateGrowiDirective(projectDirRoot, GrowiPluginType.Theme);
 
   const { growiPlugin } = data;
 
@@ -35,7 +35,7 @@ export const validateThemePluginGrowiDirective = (projectDirRoot: string): Growi
 
   if (invalidObjects.length > 0) {
     throw new GrowiPluginValidationError<GrowiPluginValidationData>(
-      `Some of theme metadata are invalid: ${invalidObjects}`,
+      `Some of theme metadata are invalid: ${invalidObjects.toString()}`,
     );
   }
 

+ 1 - 21
packages/pluginkit/test/fixtures/example-package/invalid-theme1/package.json

@@ -4,26 +4,6 @@
   "main": "index.js",
   "growiPlugin": {
     "schemaVersion": "4",
-    "types": ["theme"],
-    "themes": [
-      {
-        "name": "theme1-1",
-        "manifestKey": "src/styles/style.scss",
-        "schemeType": "light",
-        "lightBg": "#ff0000",
-        "darkBg": "#0000ff",
-        "lightSidebar": "#ffff00",
-        "darkSidebar": "#ff8800",
-        "lightIcon": "#ff0000",
-        "darkIcon": "#000000",
-        "createBtn": "#00ff00"
-      },
-      {
-        "name": "theme1-2",
-        "manifestKey": "src/styles/style.scss",
-        "schemeType": "light",
-        "// missing some attributes": ""
-      }
-    ]
+    "types": ["theme"]
   }
 }

+ 0 - 0
packages/pluginkit/test/fixtures/example-package/invalid-theme2/index.js


+ 29 - 0
packages/pluginkit/test/fixtures/example-package/invalid-theme2/package.json

@@ -0,0 +1,29 @@
+{
+  "name": "example-package-invalid-theme2",
+  "version": "1.0.0",
+  "main": "index.js",
+  "growiPlugin": {
+    "schemaVersion": "4",
+    "types": ["theme"],
+    "themes": [
+      {
+        "name": "theme2-1",
+        "manifestKey": "src/styles/style.scss",
+        "schemeType": "light",
+        "lightBg": "#ff0000",
+        "darkBg": "#0000ff",
+        "lightSidebar": "#ffff00",
+        "darkSidebar": "#ff8800",
+        "lightIcon": "#ff0000",
+        "darkIcon": "#000000",
+        "createBtn": "#00ff00"
+      },
+      {
+        "name": "theme2-2",
+        "manifestKey": "src/styles/style.scss",
+        "schemeType": "light",
+        "// missing some attributes": ""
+      }
+    ]
+  }
+}