|
| 1 | +import { cosmiconfig } from 'cosmiconfig'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { getConfig } from '../../src/config'; |
| 4 | +import type { MainConfig } from '../../src/types'; |
| 5 | + |
| 6 | +// Mock the cosmiconfig module |
| 7 | +vi.mock('cosmiconfig', () => ({ |
| 8 | + cosmiconfig: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +// Mock the logger |
| 12 | +vi.mock('../../src/logger', () => ({ |
| 13 | + logger: { |
| 14 | + debug: vi.fn(), |
| 15 | + warn: vi.fn(), |
| 16 | + error: vi.fn(), |
| 17 | + info: vi.fn(), |
| 18 | + success: vi.fn(), |
| 19 | + progress: vi.fn(), |
| 20 | + divider: vi.fn(), |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +describe('config', () => { |
| 25 | + // Common mocks |
| 26 | + const mockLoad = vi.fn(); |
| 27 | + const mockSearch = vi.fn(); |
| 28 | + const mockExplorer = { load: mockLoad, search: mockSearch }; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + // Reset mocks before each test |
| 32 | + vi.clearAllMocks(); |
| 33 | + |
| 34 | + // Setup the default cosmiconfig mock |
| 35 | + (cosmiconfig as unknown as ReturnType<typeof vi.fn>).mockReturnValue( |
| 36 | + mockExplorer, |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('getConfig', () => { |
| 41 | + it('should load config from specific file when provided', async () => { |
| 42 | + // Setup the mock to return a valid config |
| 43 | + const configFile = 'custom.config.mjs'; |
| 44 | + const mockConfig: MainConfig = { |
| 45 | + langs: { |
| 46 | + 'en-US': { name: 'English' }, |
| 47 | + fr: { name: 'French' }, |
| 48 | + }, |
| 49 | + docsRoot: 'content/en', |
| 50 | + }; |
| 51 | + mockLoad.mockResolvedValue({ config: mockConfig, filepath: configFile }); |
| 52 | + |
| 53 | + // Call the function with a specific config file |
| 54 | + const result = await getConfig({ config: configFile }); |
| 55 | + |
| 56 | + // Assertions |
| 57 | + expect(cosmiconfig).toHaveBeenCalledWith( |
| 58 | + 'translation', |
| 59 | + expect.any(Object), |
| 60 | + ); |
| 61 | + expect(mockLoad).toHaveBeenCalledWith(configFile); |
| 62 | + expect(result).toEqual(mockConfig); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should search for config when no specific file is provided', async () => { |
| 66 | + // Setup the mock to return a valid config |
| 67 | + const mockConfig: MainConfig = { |
| 68 | + langs: { |
| 69 | + 'en-US': { name: 'English' }, |
| 70 | + fr: { name: 'French' }, |
| 71 | + }, |
| 72 | + docsRoot: 'content/en', |
| 73 | + }; |
| 74 | + mockSearch.mockResolvedValue({ |
| 75 | + config: mockConfig, |
| 76 | + filepath: 'translation.config.mjs', |
| 77 | + }); |
| 78 | + |
| 79 | + // Call the function without a specific config file |
| 80 | + const result = await getConfig({}); |
| 81 | + |
| 82 | + // Assertions |
| 83 | + expect(cosmiconfig).toHaveBeenCalledWith( |
| 84 | + 'translation', |
| 85 | + expect.any(Object), |
| 86 | + ); |
| 87 | + expect(mockSearch).toHaveBeenCalled(); |
| 88 | + expect(result).toEqual(mockConfig); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return empty config when no config file is found', async () => { |
| 92 | + // Setup the mock to return null (no config found) |
| 93 | + mockSearch.mockResolvedValue(null); |
| 94 | + |
| 95 | + // Call the function |
| 96 | + const result = await getConfig({}); |
| 97 | + |
| 98 | + // Assertions |
| 99 | + expect(result).toEqual({}); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should handle errors when loading config file', async () => { |
| 103 | + // Setup the mock to throw an error |
| 104 | + const configFile = 'broken.config.mjs'; |
| 105 | + const error = new Error('Config file not found'); |
| 106 | + mockLoad.mockRejectedValue(error); |
| 107 | + |
| 108 | + // Mock process.exit to prevent the test from actually exiting |
| 109 | + const mockExit = vi |
| 110 | + .spyOn(process, 'exit') |
| 111 | + .mockImplementation(() => undefined as never); |
| 112 | + |
| 113 | + // Call the function with a specific config file that will error |
| 114 | + await getConfig({ config: configFile }); |
| 115 | + |
| 116 | + // Assertions |
| 117 | + expect(mockExit).toHaveBeenCalledWith(1); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should handle targetLanguage option correctly', async () => { |
| 121 | + const mockConfig: MainConfig = { |
| 122 | + langs: { |
| 123 | + 'en-US': { name: 'English' }, |
| 124 | + fr: { name: 'French' }, |
| 125 | + }, |
| 126 | + docsRoot: 'content/en', |
| 127 | + }; |
| 128 | + mockSearch.mockResolvedValue({ |
| 129 | + config: mockConfig, |
| 130 | + filepath: 'translation.config.mjs', |
| 131 | + }); |
| 132 | + |
| 133 | + // Call with targetLanguage option |
| 134 | + const result = await getConfig({ targetLanguage: 'fr' }); |
| 135 | + |
| 136 | + // Assertions |
| 137 | + expect(result).toEqual(mockConfig); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments