Repository navigation
Expand file tree
/
Copy pathgenerateHtml.test.ts
More file actions
86 lines (70 loc) · 3.59 KB
/
Copy pathgenerateHtml.test.ts
File metadata and controls
86 lines (70 loc) · 3.59 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
import { generateHtml } from '@shared/visualServer/generateHtml';
import * as fs from 'fs';
import { mocked } from 'jest-mock';
jest.mock('fs');
jest.mock('path', () => ({
...jest.requireActual('path'),
join: (...args: string[]) => args.join('/'),
}));
describe('generateHtml', () => {
const mockFsExistsSync = mocked(fs.existsSync);
const mockFsReadFileSync = mocked(fs.readFileSync);
const mockFsWriteFileSync = mocked(fs.writeFileSync);
beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(console, 'log').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(process, 'cwd').mockReturnValue('/mock/cwd');
});
it('should create new files if they do not exist', () => {
mockFsExistsSync.mockReturnValue(false);
mockFsReadFileSync.mockReturnValue('<html>template</html>');
const dependencyGraph = [{ node: { containerName: 'ServiceA' }, dependencies: [] }];
generateHtml(dependencyGraph, 'ServiceA');
expect(mockFsWriteFileSync).toHaveBeenCalledWith('/mock/cwd/dependency-graph.json', expect.any(String));
const calls = mockFsWriteFileSync.mock.calls;
if (calls.length === 0) throw new Error('No calls to writeFileSync');
const writtenJson = JSON.parse(<string>calls[0][1]);
expect(writtenJson).toEqual({
definitions: {},
trees: [[{ node: { containerName: 'ServiceA' }, dependencies: [] }]],
});
expect(mockFsWriteFileSync).toHaveBeenCalledWith('/mock/cwd/dependency-graph.html', expect.stringContaining('<html>template</html>'));
});
it('should append to existing graph data', () => {
mockFsExistsSync.mockReturnValue(true);
const existingData = {
definitions: { 'REF:123': 'someStruct' },
trees: [[{ node: { containerName: 'ServiceB' }, dependencies: [] }]],
};
mockFsReadFileSync.mockImplementation(((path: any) => {
const p = String(path);
if (p.endsWith('.json')) return JSON.stringify(existingData);
if (p.endsWith('.html')) return '<html>existing</html>';
return '';
}) as any);
const dependencyGraph = [{ node: { containerName: 'ServiceA' }, dependencies: [] }];
generateHtml(dependencyGraph, 'ServiceA');
expect(mockFsWriteFileSync).toHaveBeenCalledWith('/mock/cwd/dependency-graph.json', expect.any(String));
const writtenJsonArgs = mockFsWriteFileSync.mock.calls.find((args) => typeof args[0] === 'string' && args[0].endsWith('.json'));
if (!writtenJsonArgs) throw new Error('Could not find call to writeFileSync with .json file');
const writtenJson = JSON.parse(writtenJsonArgs[1] as string);
expect(writtenJson.definitions).toEqual(existingData.definitions);
expect(writtenJson.trees).toHaveLength(2);
expect(writtenJson.trees[0]).toEqual(existingData.trees[0]);
expect(writtenJson.trees[1]).toEqual([{ node: { containerName: 'ServiceA' }, dependencies: [] }]);
});
it('should inject service data into HTML', () => {
mockFsExistsSync.mockReturnValue(true);
mockFsReadFileSync.mockImplementation(((path: any) => {
const p = String(path);
if (p.endsWith('.json')) return JSON.stringify({ definitions: {}, trees: [] });
if (p.endsWith('.html')) return 'let SERVICE_A_DATA = []; // {{ SERVICE_A_GRAPH_DATA }}';
return '';
}) as any);
const dependencyGraph = [{ node: { containerName: 'ServiceA' }, dependencies: [] }];
generateHtml(dependencyGraph, 'ServiceA');
expect(mockFsWriteFileSync).toHaveBeenCalledWith('/mock/cwd/dependency-graph.html', expect.stringContaining('let SERVICE_A_DATA = ['));
});
});