Repository navigation
Expand file tree
/
Copy pathDependencyGraphRegenerator.test.ts
More file actions
164 lines (128 loc) · 6.13 KB
/
Copy pathDependencyGraphRegenerator.test.ts
File metadata and controls
164 lines (128 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { asPartialMock } from '@shared/stub-utilities.factory';
import { mocked } from 'jest-mock';
import { DependencyGraphRegenerator } from './DependencyGraphRegenerator';
import { HtmlDataExtractor } from './HtmlDataExtractor';
import { TemplateProcessor } from './TemplateProcessor';
import * as fs from 'fs';
jest.mock('fs');
jest.mock('path', () => ({
...jest.requireActual('path'),
join: (...args: string[]) => args.join('/'),
}));
describe('DependencyGraphRegenerator', () => {
let target: DependencyGraphRegenerator;
let mockExtractor: HtmlDataExtractor;
let mockProcessor: TemplateProcessor;
const mockFsExistsSync = mocked(fs.existsSync);
const mockFsReadFileSync = mocked(fs.readFileSync);
const mockFsWriteFileSync = mocked(fs.writeFileSync);
let mockExit: jest.SpyInstance;
let mockConsoleError: jest.SpyInstance;
let mockConsoleLog: jest.SpyInstance;
let mockConsoleWarn: jest.SpyInstance;
beforeEach(() => {
jest.clearAllMocks();
mockExtractor = asPartialMock<HtmlDataExtractor>({
extractArray: jest.fn(),
extractGlobalDefinitions: jest.fn(),
});
mockProcessor = asPartialMock<TemplateProcessor>({
injectServiceData: jest.fn(),
injectGlobalDefinitions: jest.fn(),
});
target = new DependencyGraphRegenerator(mockExtractor, mockProcessor);
mockExit = jest.spyOn(process, 'exit').mockImplementation((() => {}) as any);
mockConsoleError = jest.spyOn(console, 'error').mockImplementation(() => {});
mockConsoleLog = jest.spyOn(console, 'log').mockImplementation(() => {});
mockConsoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.spyOn(process, 'cwd').mockReturnValue('/mock/cwd');
});
it('should exit if template is missing', () => {
mockFsExistsSync.mockImplementation(((path: any) => !path.includes('template')) as any);
target.execute();
expect(mockConsoleError).toHaveBeenCalledWith(expect.stringContaining('Template not found'));
expect(mockExit).toHaveBeenCalledWith(1);
});
it('should extract existing data if output file exists', () => {
mockFsExistsSync.mockReturnValue(true);
mockFsReadFileSync.mockImplementation(((path: any) => {
if (path.includes('template')) return 'template';
return 'existing-content';
}) as any);
mocked(mockExtractor.extractArray).mockImplementation((_, key) => {
if (key === 'BUCHUNG_DATA') return '[1]';
return null;
});
mocked(mockExtractor.extractGlobalDefinitions).mockReturnValue('{def:1}');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mocked(mockProcessor.injectServiceData).mockImplementation((html, key, data) => html);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mocked(mockProcessor.injectGlobalDefinitions).mockImplementation((html, data) => html);
target.execute();
expect(mockExtractor.extractArray).toHaveBeenCalledWith('existing-content', 'BUCHUNG_DATA');
expect(mockExtractor.extractGlobalDefinitions).toHaveBeenCalledWith('existing-content');
expect(mockConsoleLog).toHaveBeenCalledWith('Preserved GLOBAL_DEFINITIONS.');
});
it('should inject preserved data into template', () => {
mockFsExistsSync.mockReturnValue(true);
mockFsReadFileSync.mockImplementation(((path: any) => {
if (path.includes('template')) return 'template-content';
return 'existing-content';
}) as any);
mocked(mockExtractor.extractArray).mockImplementation((_, key) => {
if (key === 'BUCHUNG_DATA') return '[1]';
return null;
});
mocked(mockProcessor.injectServiceData).mockReturnValue('template-with-buchung');
mocked(mockProcessor.injectGlobalDefinitions).mockReturnValue('final-html');
target.execute();
expect(mockProcessor.injectServiceData).toHaveBeenCalledWith('template-content', 'BUCHUNG', '[1]');
expect(mockProcessor.injectGlobalDefinitions).toHaveBeenCalledWith('template-with-buchung', '{}');
expect(mockFsWriteFileSync).toHaveBeenCalledWith(expect.stringContaining('dependency-graph.html'), 'final-html');
});
it('should warn if GLOBAL_DEFINITIONS placeholder missing', () => {
mockFsExistsSync.mockReturnValue(true);
mockFsReadFileSync.mockImplementation(((path: any) => {
if (path.includes('template')) return ''; // empty template
return '';
}) as any);
mocked(mockExtractor.extractArray).mockReturnValue(null);
mocked(mockExtractor.extractGlobalDefinitions).mockReturnValue(null);
mocked(mockProcessor.injectServiceData).mockImplementation((h) => h);
mocked(mockProcessor.injectGlobalDefinitions).mockImplementation((h) => h);
target.execute();
expect(mockConsoleWarn).toHaveBeenCalledWith('Could not find placeholder for GLOBAL_DEFINITIONS in template.');
});
it('should not warn if template has placeholder', () => {
mockFsExistsSync.mockReturnValue(true);
mockFsReadFileSync.mockImplementation(((path: any) => {
if (path.includes('template')) return 'let GLOBAL_DEFINITIONS = {};';
return '';
}) as any);
mocked(mockExtractor.extractArray).mockReturnValue(null);
mocked(mockExtractor.extractGlobalDefinitions).mockReturnValue(null);
mocked(mockProcessor.injectServiceData).mockImplementation((h) => h);
mocked(mockProcessor.injectGlobalDefinitions).mockImplementation((h) => h);
target.execute();
expect(mockConsoleWarn).not.toHaveBeenCalled();
});
it('should catch errors and exit', () => {
mockFsExistsSync.mockImplementation(() => {
throw new Error('FAIL');
});
target.execute();
expect(mockConsoleError).toHaveBeenCalledWith('Error regenerating HTML graph:', expect.any(Error));
expect(mockExit).toHaveBeenCalledWith(1);
});
it('should count preserved services correctly', () => {
mockFsExistsSync.mockReturnValue(true);
mockFsReadFileSync.mockReturnValue('');
const keysFound = ['BUCHUNG_DATA', 'REISEN_DATA'];
mocked(mockExtractor.extractArray).mockImplementation((_, key) => {
if (keysFound.includes(key)) return '[]';
return null;
});
target.execute();
expect(mockConsoleLog).toHaveBeenCalledWith(expect.stringContaining('Preserved data for 2 services.'));
});
});