forked from asyncapi/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-tools.test.js
98 lines (80 loc) · 3.65 KB
/
build-tools.test.js
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
const axios = require('axios');
const { resolve } = require('path');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const { tagsData, manualTools, mockConvertedData, mockExtractData } = require('./fixtures/buildToolsData');
const { buildTools } = require('../scripts/build-tools.ts');
jest.mock('axios');
jest.mock('../scripts/tools/categorylist', () => ({
categoryList: [
{ name: 'Category1', description: 'Description for Category1' },
{ name: 'Category2', description: 'Description for Category2' }
]
}));
jest.mock('../scripts/tools/tags-color', () => ({
languagesColor: [
{ name: 'JavaScript', color: 'bg-[#f1e05a]', borderColor: 'border-[#f1e05a]' },
{ name: 'Python', color: 'bg-[#3572A5]', borderColor: 'border-[#3572A5]' }
],
technologiesColor: [
{ name: 'React', color: 'bg-[#61dafb]', borderColor: 'border-[#61dafb]' },
{ name: 'Node.js', color: 'bg-[#68a063]', borderColor: 'border-[#68a063]' }
]
}));
describe('buildTools', () => {
const testDir = path.join(String(os.tmpdir()), 'test_config');
const toolsPath = resolve(testDir, 'tools.json');
const tagsPath = resolve(testDir, 'all-tags.json');
const automatedToolsPath = resolve(testDir, 'tools-automated.json');
const manualToolsPath = resolve(testDir, 'tools-manual.json');
let consoleErrorMock;
beforeAll(() => {
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {});
fs.ensureDirSync(testDir);
fs.outputFileSync(manualToolsPath, JSON.stringify(manualTools));
fs.outputFileSync(automatedToolsPath, JSON.stringify({}));
fs.outputFileSync(toolsPath, JSON.stringify({}));
fs.outputFileSync(tagsPath, JSON.stringify({}));
process.env.GITHUB_TOKEN = 'mockToken';
});
afterAll(() => {
fs.removeSync(testDir);
consoleErrorMock.mockRestore();
});
beforeEach(() => {
jest.clearAllMocks();
});
it('should extract, convert, combine tools, and write to file', async () => {
axios.get.mockResolvedValue({ data: mockExtractData });
await buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath);
const automatedToolsContent = JSON.parse(fs.readFileSync(automatedToolsPath, 'utf8'));
const combinedToolsContent = JSON.parse(fs.readFileSync(toolsPath, 'utf8'));
const tagsContent = JSON.parse(fs.readFileSync(tagsPath, 'utf8'));
expect(Object.keys(automatedToolsContent)).toEqual(Object.keys(mockConvertedData));
expect(automatedToolsContent.Category1.description).toEqual(mockConvertedData.Category1.description);
expect(automatedToolsContent.Category2.description).toEqual(mockConvertedData.Category2.description);
expect(combinedToolsContent).toHaveProperty('Category1');
expect(combinedToolsContent).toHaveProperty('Category2');
expect(combinedToolsContent.Category1.description).toEqual(mockConvertedData.Category1.description);
expect(combinedToolsContent.Category2.description).toEqual(mockConvertedData.Category2.description);
expect(tagsContent).toEqual(tagsData);
});
it('should handle getData error', async () => {
axios.get.mockRejectedValue(new Error('Extract error'));
try {
await buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath);
} catch (err) {
expect(err.message).toContain('Extract error');
}
});
it('should handle file write errors', async () => {
axios.get.mockResolvedValue({ data: mockExtractData });
const invalidPath = path.resolve(os.tmpdir(), 'invalid_dir', 'tools.json');
try {
await buildTools(invalidPath, manualToolsPath, toolsPath, tagsPath);
} catch (err) {
expect(err.message).toMatch(/ENOENT|EACCES/);
}
});
});