Skip to content

Commit

Permalink
feat: add tests for Index and build pages script (asyncapi#3044)
Browse files Browse the repository at this point in the history
Co-authored-by: Ansh Goyal <anshgoyal1704@gmail.com>
  • Loading branch information
vishvamsinh28 and anshgoyalevil committed Jul 9, 2024
1 parent fa6e4c9 commit cf5f24a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
2 changes: 2 additions & 0 deletions scripts/build-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ function copyAndRenameFiles(srcDir, targetDir) {
}

copyAndRenameFiles(SRC_DIR, TARGET_DIR);

module.exports = {copyAndRenameFiles,capitalizeJsxTags}
4 changes: 3 additions & 1 deletion scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ async function start() {
await buildFinanceInfoList();
}

start();
module.exports = start;

start();
48 changes: 48 additions & 0 deletions tests/build-pages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const fs = require('fs');
const path = require('path');
const { capitalizeJsxTags, copyAndRenameFiles } = require('../scripts/build-pages');

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>');
});
});
35 changes: 35 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const rssFeed = require('../scripts/build-rss');
const buildPostList = require('../scripts/build-post-list');
const buildCaseStudiesList = require('../scripts/casestudies');
const buildAdoptersList = require('../scripts/adopters');
const buildFinanceInfoList = require('../scripts/finance');
const start = require('../scripts/index');

jest.mock('../scripts/build-rss');
jest.mock('../scripts/build-post-list');
jest.mock('../scripts/casestudies');
jest.mock('../scripts/adopters');
jest.mock('../scripts/finance');

describe('start function', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('should call all functions in the correct order', async () => {
await start();

expect(buildPostList).toHaveBeenCalled();

expect(rssFeed).toHaveBeenCalledWith(
'blog',
'AsyncAPI Initiative Blog RSS Feed',
'AsyncAPI Initiative Blog',
'rss.xml'
);

expect(buildCaseStudiesList).toHaveBeenCalled();
expect(buildAdoptersList).toHaveBeenCalled();
expect(buildFinanceInfoList).toHaveBeenCalled();
});
});

0 comments on commit cf5f24a

Please sign in to comment.