فهرست منبع

add test and fix loadFromDB

Yuki Takei 11 ماه پیش
والد
کامیت
d5c5a4f2a2

+ 39 - 0
apps/app/src/server/service/config-manager/config-loader.spec.ts

@@ -0,0 +1,39 @@
+import type { RawConfigData } from '@growi/core/dist/interfaces';
+
+import type { ConfigKey, ConfigValues } from './config-definition';
+import { ConfigLoader } from './config-loader';
+
+const mockExec = vi.fn();
+const mockFind = vi.fn().mockReturnValue({ exec: mockExec });
+
+// Mock the Config model
+vi.mock('../../models/config', () => ({
+  Config: {
+    find: mockFind,
+  },
+}));
+
+describe('ConfigLoader', () => {
+  let configLoader: ConfigLoader;
+
+  beforeEach(async() => {
+    configLoader = new ConfigLoader();
+    vi.clearAllMocks();
+  });
+
+  describe('loadFromDB', () => {
+    describe('when doc.value is empty string', () => {
+      beforeEach(() => {
+        const mockDocs = [
+          { key: 'app:referrerPolicy' as ConfigKey, value: '' },
+        ];
+        mockExec.mockResolvedValue(mockDocs);
+      });
+
+      it('should return null for value', async() => {
+        const config: RawConfigData<ConfigKey, ConfigValues> = await configLoader.loadFromDB();
+        expect(config['app:referrerPolicy'].value).toBe(null);
+      });
+    });
+  });
+});

+ 1 - 1
apps/app/src/server/service/config-manager/config-loader.ts

@@ -44,7 +44,7 @@ export class ConfigLoader implements IConfigLoader<ConfigKey, ConfigValues> {
     for (const doc of docs) {
       dbConfig[doc.key as ConfigKey] = {
         definition: (doc.key in CONFIG_DEFINITIONS) ? CONFIG_DEFINITIONS[doc.key as ConfigKey] : undefined,
-        value: doc.value != null ? JSON.parse(doc.value) : null,
+        value: doc.value != null && doc.value !== '' ? JSON.parse(doc.value) : null,
       };
     }