|
1 | 1 | import { join } from 'path';
|
| 2 | +import type { Editor } from 'mem-fs-editor'; |
2 | 3 | import { existsSync, readFileSync } from 'fs';
|
3 | 4 |
|
4 | 5 | import { getWebappPath, FileName } from '@sap-ux/project-access';
|
@@ -36,64 +37,61 @@ describe('getVariantNamespace', () => {
|
36 | 37 | const mockWebappPath = '/test/project/webapp';
|
37 | 38 | const mockManifestPath = join(mockWebappPath, FileName.ManifestAppDescrVar);
|
38 | 39 |
|
| 40 | + let mockFs: Editor; |
| 41 | + let mockFsExists: jest.Mock; |
| 42 | + let mockFsReadJSON: jest.Mock; |
| 43 | + |
39 | 44 | beforeAll(async () => {
|
40 | 45 | await initI18n();
|
41 | 46 | });
|
42 | 47 |
|
43 | 48 | beforeEach(() => {
|
44 | 49 | jest.clearAllMocks();
|
45 | 50 | mockGetWebappPath.mockResolvedValue(mockWebappPath);
|
| 51 | + |
| 52 | + mockFsExists = jest.fn(); |
| 53 | + mockFsReadJSON = jest.fn(); |
| 54 | + |
| 55 | + mockFs = { |
| 56 | + exists: mockFsExists, |
| 57 | + readJSON: mockFsReadJSON |
| 58 | + } as unknown as Editor; |
46 | 59 | });
|
47 | 60 |
|
48 | 61 | it('should return undefined for S4HC projects', async () => {
|
49 |
| - const result = await getVariantNamespace(mockPath, true); |
50 |
| - |
| 62 | + const result = await getVariantNamespace(mockPath, true, mockFs); |
51 | 63 | expect(result).toBeUndefined();
|
52 | 64 | expect(mockGetWebappPath).not.toHaveBeenCalled();
|
53 |
| - expect(mockExistsSync).not.toHaveBeenCalled(); |
54 | 65 | });
|
55 | 66 |
|
56 |
| - it('should return namespace from manifest.appdescr_variant file', async () => { |
57 |
| - const mockManifest = { |
58 |
| - namespace: 'apps/workcenter/appVariants/customer.app.variant' |
59 |
| - }; |
| 67 | + it('should return namespace from memory', async () => { |
| 68 | + const mockManifest = { namespace: 'apps/workcenter/appVariants/customer.app.variant' }; |
| 69 | + mockFsExists.mockReturnValue(true); |
| 70 | + mockFsReadJSON.mockReturnValue(mockManifest); |
60 | 71 |
|
61 |
| - mockExistsSync.mockReturnValue(true); |
62 |
| - mockReadFileSync.mockReturnValue(JSON.stringify(mockManifest)); |
| 72 | + const result = await getVariantNamespace(mockPath, false, mockFs); |
63 | 73 |
|
64 |
| - const result = await getVariantNamespace(mockPath, false); |
65 |
| - |
66 |
| - expect(result).toBe('apps/workcenter/appVariants/customer.app.variant'); |
67 |
| - expect(mockGetWebappPath).toHaveBeenCalledWith(mockPath); |
68 |
| - expect(mockExistsSync).toHaveBeenCalledWith(mockManifestPath); |
69 |
| - expect(mockReadFileSync).toHaveBeenCalledWith(mockManifestPath, 'utf-8'); |
| 74 | + expect(result).toBe(mockManifest.namespace); |
| 75 | + expect(mockGetWebappPath).toHaveBeenCalledWith(mockPath, mockFs); |
| 76 | + expect(mockFsExists).toHaveBeenCalledWith(mockManifestPath); |
70 | 77 | });
|
71 | 78 |
|
72 |
| - it('should return undefined when manifest file does not exist', async () => { |
73 |
| - mockExistsSync.mockReturnValue(false); |
74 |
| - |
75 |
| - const result = await getVariantNamespace(mockPath, false); |
76 |
| - |
| 79 | + it('should return undefined when memory file does not exist', async () => { |
| 80 | + mockFsExists.mockReturnValue(false); |
| 81 | + const result = await getVariantNamespace(mockPath, false, mockFs); |
77 | 82 | expect(result).toBeUndefined();
|
78 |
| - expect(mockGetWebappPath).toHaveBeenCalledWith(mockPath); |
79 |
| - expect(mockExistsSync).toHaveBeenCalledWith(mockManifestPath); |
80 |
| - expect(mockReadFileSync).not.toHaveBeenCalled(); |
81 | 83 | });
|
82 | 84 |
|
83 |
| - it('should handle JSON parsing errors gracefully', async () => { |
84 |
| - mockExistsSync.mockReturnValue(true); |
85 |
| - mockReadFileSync.mockReturnValue('invalid json content'); |
| 85 | + it('should handle errors gracefully', async () => { |
| 86 | + mockFsExists.mockImplementation(() => { |
| 87 | + throw new Error('Memory filesystem error'); |
| 88 | + }); |
86 | 89 |
|
87 |
| - const result = await getVariantNamespace(mockPath, false); |
| 90 | + const result = await getVariantNamespace(mockPath, false, mockFs); |
88 | 91 |
|
89 | 92 | expect(result).toBeUndefined();
|
90 |
| - expect(mockGetWebappPath).toHaveBeenCalledWith(mockPath); |
91 |
| - expect(mockExistsSync).toHaveBeenCalledWith(mockManifestPath); |
92 |
| - expect(mockReadFileSync).toHaveBeenCalledWith(mockManifestPath, 'utf-8'); |
93 | 93 | expect(DeploymentGenerator.logger.debug).toHaveBeenCalledWith(
|
94 |
| - t('debug.lrepNamespaceNotFound', { |
95 |
| - error: 'Unexpected token \'i\', "invalid json content" is not valid JSON' |
96 |
| - }) |
| 94 | + t('debug.lrepNamespaceNotFound', { error: 'Memory filesystem error' }) |
97 | 95 | );
|
98 | 96 | });
|
99 | 97 | });
|
0 commit comments