From cf5f24a1fac10e16258741d744ee5550aaf92fa2 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela <90895835+vishvamsinh28@users.noreply.github.com> Date: Tue, 9 Jul 2024 20:23:18 +0530 Subject: [PATCH] feat: add tests for Index and build pages script (#3044) Co-authored-by: Ansh Goyal --- scripts/build-pages.js | 2 ++ scripts/index.js | 4 +++- tests/build-pages.test.js | 48 +++++++++++++++++++++++++++++++++++++++ tests/index.test.js | 35 ++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/build-pages.test.js create mode 100644 tests/index.test.js diff --git a/scripts/build-pages.js b/scripts/build-pages.js index 826f2a0ed58..48b3553e96b 100644 --- a/scripts/build-pages.js +++ b/scripts/build-pages.js @@ -56,3 +56,5 @@ function copyAndRenameFiles(srcDir, targetDir) { } copyAndRenameFiles(SRC_DIR, TARGET_DIR); + +module.exports = {copyAndRenameFiles,capitalizeJsxTags} \ No newline at end of file diff --git a/scripts/index.js b/scripts/index.js index 9ee392a8b32..caa507751ad 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -17,4 +17,6 @@ async function start() { await buildFinanceInfoList(); } -start(); \ No newline at end of file +module.exports = start; + +start(); diff --git a/tests/build-pages.test.js b/tests/build-pages.test.js new file mode 100644 index 00000000000..65947e9f629 --- /dev/null +++ b/tests/build-pages.test.js @@ -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 = '
Hello
'; + const output = '
Hello
'; + expect(capitalizeJsxTags(input)).toBe(output); + }); + + test('should not capitalize non-JSX tags', () => { + const input = '
Hello
'; + const output = '
Hello
'; + 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 = '
Hello
'; + 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('
Hello
'); + expect(nestedTargetFile).toBe('
Hello
'); + }); +}); \ No newline at end of file diff --git a/tests/index.test.js b/tests/index.test.js new file mode 100644 index 00000000000..78e2c216958 --- /dev/null +++ b/tests/index.test.js @@ -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(); + }); +});