forked from asyncapi/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-pages.test.js
58 lines (47 loc) · 2.1 KB
/
build-pages.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
const fs = require('fs');
const path = require('path');
const { capitalizeJsxTags, copyAndRenameFiles, ensureDirectoryExists } = require('../scripts/build-pages.ts');
describe('capitalizeJsxTags', () => {
test('should capitalize JSX tags', () => {
const input = '<table><tr><td>Hello</td></tr></table>';
const output = '<Table><Tr><Td>Hello</Td></Tr></Table>';
expect(capitalizeJsxTags(input)).toBe(output);
});
test('should not capitalize non-JSX tags', () => {
const input = '<div>Hello</div>';
const output = '<div>Hello</div>';
expect(capitalizeJsxTags(input)).toBe(output);
});
});
describe('copyAndRenameFiles', () => {
const TEST_DIR = 'test';
const SRC_DIR = path.join(TEST_DIR, 'src');
const TARGET_DIR = path.join(TEST_DIR, 'target');
beforeAll(() => {
fs.mkdirSync(TEST_DIR, { recursive: true });
fs.mkdirSync(SRC_DIR, { recursive: true });
fs.mkdirSync(TARGET_DIR, { recursive: true });
const fileContent = '<table><tr><td>Hello</td></tr></table>';
fs.writeFileSync(path.join(SRC_DIR, 'test.md'), fileContent, 'utf8');
fs.mkdirSync(path.join(SRC_DIR, 'nested'), { recursive: true });
fs.writeFileSync(path.join(SRC_DIR, 'nested', 'nested.md'), fileContent, 'utf8');
});
afterAll(() => {
fs.rmSync(TEST_DIR, { recursive: true, force: true });
});
test('should copy and rename files correctly', () => {
copyAndRenameFiles(SRC_DIR, TARGET_DIR);
const targetFile = fs.readFileSync(path.join(TARGET_DIR, 'test.mdx'), 'utf8');
const nestedTargetFile = fs.readFileSync(path.join(TARGET_DIR, 'nested', 'nested.mdx'), 'utf8');
expect(targetFile).toBe('<Table><Tr><Td>Hello</Td></Tr></Table>');
expect(nestedTargetFile).toBe('<Table><Tr><Td>Hello</Td></Tr></Table>');
});
test('should create a directory if it does not exist', () => {
const NEW_TEST_DIR = 'testDir';
expect(fs.existsSync(NEW_TEST_DIR)).toBe(false);
ensureDirectoryExists(NEW_TEST_DIR);
expect(fs.existsSync(NEW_TEST_DIR)).toBe(true);
// delete the test directory after the test
fs.rmSync(NEW_TEST_DIR, { recursive: true, force: true });
});
});