Yuki Takei 2 lat temu
rodzic
commit
7f6d5556b0

+ 103 - 2
packages/pluginkit/src/server/utils/v4/package-json/validate.spec.ts

@@ -1,15 +1,116 @@
-import path from 'path';
+import examplePkg from '^/test/fixtures/example-package/template1/package.json';
+
+import { GrowiPluginType } from '~/consts';
 
 import { validatePackageJson } from './validate';
 
+const mocks = vi.hoisted(() => {
+  return {
+    importPackageJsonMock: vi.fn(),
+  };
+});
+
+vi.mock('./import', () => {
+  return { importPackageJson: mocks.importPackageJsonMock };
+});
+
 describe('validatePackageJson()', () => {
 
   it('returns a data object', async() => {
+    // setup
+    mocks.importPackageJsonMock.mockResolvedValue(examplePkg);
+
     // when
-    const data = await validatePackageJson(path.resolve(__dirname, '../../../../../test/fixtures/example-package/template1'));
+    const data = await validatePackageJson('package.json');
 
     // then
     expect(data).not.toBeNull();
   });
 
+  it("with the 'expectedPluginType' argument returns a data object", async() => {
+    // setup
+    mocks.importPackageJsonMock.mockResolvedValue(examplePkg);
+
+    // when
+    const data = await validatePackageJson('package.json', GrowiPluginType.TEMPLATE);
+
+    // then
+    expect(data).not.toBeNull();
+  });
+
+  describe('should throw an GrowiPluginValidationError', () => {
+
+    it("when the pkg does not have 'growiPlugin' directive", async() => {
+      // setup
+      mocks.importPackageJsonMock.mockResolvedValue({});
+
+      // when
+      const caller = async() => { await validatePackageJson('package.json') };
+
+      // then
+      await expect(caller).rejects.toThrow("The package.json does not have 'growiPlugin' directive.");
+    });
+
+    it("when the 'schemaVersion' is NaN", async() => {
+      // setup
+      mocks.importPackageJsonMock.mockResolvedValue({
+        growiPlugin: {
+          schemaVersion: 'foo',
+        },
+      });
+
+      // when
+      const caller = async() => { await validatePackageJson('package.json') };
+
+      // then
+      await expect(caller).rejects.toThrow("The growiPlugin directive must have a valid 'schemaVersion' directive.");
+    });
+
+    it("when the 'schemaVersion' is less than 4", async() => {
+      // setup
+      mocks.importPackageJsonMock.mockResolvedValue({
+        growiPlugin: {
+          schemaVersion: 3,
+        },
+      });
+
+      // when
+      const caller = async() => { await validatePackageJson('package.json') };
+
+      // then
+      await expect(caller).rejects.toThrow("The growiPlugin directive must have a valid 'schemaVersion' directive.");
+    });
+
+    it("when the 'types' directive does not exist", async() => {
+      // setup
+      mocks.importPackageJsonMock.mockResolvedValue({
+        growiPlugin: {
+          schemaVersion: 4,
+        },
+      });
+
+      // when
+      const caller = async() => { await validatePackageJson('package.json') };
+
+      // then
+      await expect(caller).rejects.toThrow("The growiPlugin directive does not have 'types' directive.");
+    });
+
+    it("when the 'types' directive does not have expected plugin type", async() => {
+      // setup
+      mocks.importPackageJsonMock.mockResolvedValue({
+        growiPlugin: {
+          schemaVersion: 4,
+          types: [GrowiPluginType.TEMPLATE],
+        },
+      });
+
+      // when
+      const caller = async() => { await validatePackageJson('package.json', GrowiPluginType.SCRIPT) };
+
+      // then
+      await expect(caller).rejects.toThrow("The growiPlugin directive does not have expected plugin type in 'types' directive.");
+    });
+  });
+
 });

+ 1 - 1
packages/pluginkit/src/server/utils/v4/package-json/validate.ts

@@ -33,7 +33,7 @@ export const validatePackageJson = async(projectDirRoot: string, expectedPluginT
     data.expectedPluginType = expectedPluginType;
 
     if (!types.includes(expectedPluginType)) {
-      throw new GrowiPluginValidationError("The growiPlugin directive does not have 'types' directive.", data);
+      throw new GrowiPluginValidationError("The growiPlugin directive does not have expected plugin type in 'types' directive.", data);
     }
   }
 

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


+ 1 - 0
packages/pluginkit/test/fixtures/example-package/template1/package.json

@@ -1,6 +1,7 @@
 {
   "name": "example-package-template1",
   "version": "1.0.0",
+  "main": "index.js",
   "growiPlugin": {
     "schemaVersion": "4",
     "types": [

+ 1 - 0
packages/pluginkit/tsconfig.json

@@ -9,6 +9,7 @@
     ],
     "baseUrl": ".",
     "paths": {
+      "^/*": ["./*"],
       "~/*": ["./src/*"]
     }
   },