config-loader.spec.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import type { RawConfigData } from '@growi/core/dist/interfaces';
  2. import type { ConfigKey, ConfigValues } from './config-definition';
  3. import { ConfigLoader } from './config-loader';
  4. const mockExec = vi.fn();
  5. const mockFind = vi.fn().mockReturnValue({ exec: mockExec });
  6. // Mock the Config model
  7. vi.mock('../../models/config', () => ({
  8. Config: {
  9. find: mockFind,
  10. },
  11. }));
  12. describe('ConfigLoader', () => {
  13. let configLoader: ConfigLoader;
  14. beforeEach(async () => {
  15. configLoader = new ConfigLoader();
  16. vi.clearAllMocks();
  17. });
  18. describe('loadFromDB', () => {
  19. describe('when doc.value is empty string', () => {
  20. beforeEach(() => {
  21. const mockDocs = [
  22. { key: 'app:referrerPolicy' as ConfigKey, value: '' },
  23. ];
  24. mockExec.mockResolvedValue(mockDocs);
  25. });
  26. it('should return null for value', async () => {
  27. const config: RawConfigData<ConfigKey, ConfigValues> =
  28. await configLoader.loadFromDB();
  29. expect(config['app:referrerPolicy'].value).toBe(null);
  30. });
  31. });
  32. describe('when doc.value is invalid JSON', () => {
  33. beforeEach(() => {
  34. const mockDocs = [
  35. { key: 'app:referrerPolicy' as ConfigKey, value: '{invalid:json' },
  36. ];
  37. mockExec.mockResolvedValue(mockDocs);
  38. });
  39. it('should return null for value', async () => {
  40. const config: RawConfigData<ConfigKey, ConfigValues> =
  41. await configLoader.loadFromDB();
  42. expect(config['app:referrerPolicy'].value).toBe(null);
  43. });
  44. });
  45. describe('when doc.value is valid JSON', () => {
  46. const validJson = { key: 'value' };
  47. beforeEach(() => {
  48. const mockDocs = [
  49. {
  50. key: 'app:referrerPolicy' as ConfigKey,
  51. value: JSON.stringify(validJson),
  52. },
  53. ];
  54. mockExec.mockResolvedValue(mockDocs);
  55. });
  56. it('should return parsed value', async () => {
  57. const config: RawConfigData<ConfigKey, ConfigValues> =
  58. await configLoader.loadFromDB();
  59. expect(config['app:referrerPolicy'].value).toEqual(validJson);
  60. });
  61. });
  62. describe('when doc.value is null', () => {
  63. beforeEach(() => {
  64. const mockDocs = [
  65. { key: 'app:referrerPolicy' as ConfigKey, value: null },
  66. ];
  67. mockExec.mockResolvedValue(mockDocs);
  68. });
  69. it('should return null for value', async () => {
  70. const config: RawConfigData<ConfigKey, ConfigValues> =
  71. await configLoader.loadFromDB();
  72. expect(config['app:referrerPolicy'].value).toBe(null);
  73. });
  74. });
  75. });
  76. });