Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test to textToP() #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion bin/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ const MarkdownIt = require("markdown-it");
const md = new MarkdownIt();
const { parse } = require("node-html-parser");

const textToP = (input) => {
if (input === null || input.length <= 0) {
return console.log("There is no input");
}

return input
.split(/\r?\n/)
.map((elem) => `<p>${elem}</p>`)
.join("\n");
};

const syntaxHighlight = (body, head) => {
if (body.querySelector("pre") != null) {
head.appendChild(
Expand Down Expand Up @@ -41,5 +52,6 @@ const textToPMd = (input) => {
module.exports = {
textToPMd,
syntaxHighlight,
cliValid
cliValid,
textToP
};
47 changes: 36 additions & 11 deletions bin/markdown.test.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,81 @@
/* global require test expect describe */
const { textToPMd, cliValid } = require("./markdown");
const { textToPMd, cliValid, textToP } = require("./markdown");

describe("P tag parser test", () => {
test("Test with null or empty file input", () => {
const nullInput = textToP(null);
const emptyStringInput = textToP("");

expect(nullInput).toBeUndefined();
expect(emptyStringInput).toBeUndefined();
});

test("Test one line input", () => {
const input = textToP("This is text input");
expect(input).toMatch(/<p>This is text input/);
});

test("Test with multiple lines input", () => {
const multiLineInput = textToP(`Open Source
OSDSSD
It is all about contribution to open source community

There is one empty line above
`);
expect(multiLineInput).not.toBeNull();
});
});

describe("test user's valid CLI", () => {
test("No commands were passed", () => {
let nullInput = cliValid(null);
const nullInput = cliValid(null);

expect(nullInput).toBeFalsy();
});

test("If user did not pass --input or --config flags", () => {
let styleSheetAdd = {
const styleSheetAdd = {
stylesheet: true
};

let command = cliValid(styleSheetAdd);
const command = cliValid(styleSheetAdd);

expect(command).toBeFalsy();
});

test("If user passed --input or --config flags", () => {
let inputCLI = {
const inputCLI = {
i: true,
input: true,
config: true,
c: true
};
console.log(inputCLI);

let commandInput = cliValid(inputCLI);
const commandInput = cliValid(inputCLI);

expect(commandInput).toBeTruthy();
});
});

describe("Markdown parser to HTML tests", () => {
test("markdown Test with null or empty file input", () => {
let nullInput = textToPMd(null);
let emptyStringInput = textToPMd("");
const nullInput = textToPMd(null);
const emptyStringInput = textToPMd("");

expect(nullInput).toBeUndefined();
expect(emptyStringInput).toBeUndefined();
});

test("markdown Test one line input", () => {
let h1Input = textToPMd("# This is heading 1");
let input = textToPMd("This is text input");
const h1Input = textToPMd("# This is heading 1");
const input = textToPMd("This is text input");
expect(h1Input).toMatch(/<h1>This is heading 1/);
expect(input).toMatch(/<p>This is text input/);
});

test("markdown Test with multiple lines input", () => {
let multiLineInput = textToPMd(`# Open Source
const multiLineInput = textToPMd(`# Open Source
## OSDSSD
It is all about contribution to open source community

Expand Down
18 changes: 9 additions & 9 deletions bin/osdssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const yargs = require("yargs");
const fs = require("fs");
const path = require("path");
const { textToPMd, syntaxHighlight, cliValid } = require("./markdown");
const { textToPMd, syntaxHighlight, cliValid, textToP } = require("./markdown");
const { parse } = require("node-html-parser");

const addingDataToHTMLFile = (parsedHtml, data, isMDfile) => {
Expand All @@ -27,13 +27,6 @@ const addingDataToHTMLFile = (parsedHtml, data, isMDfile) => {
parsedHtml.querySelector("title").set_content(title);
};

const textToP = (input) => {
return input
.split(/\r?\n/)
.map((elem) => `<p>${elem}</p>`)
.join("\n");
};

const getTitle = (input) => {
return input.split(/\r?\n/).slice(0, 1);
};
Expand Down Expand Up @@ -98,7 +91,9 @@ require("yargs")
const command = yargs.argv;

// moved if condition to here and added config identify opt
if (cliValid(command) === false) process.exit(1);
if (cliValid(command) === false) {
console.log("invalid CLI");
}

let fileOrDir;
let outputDir;
Expand Down Expand Up @@ -210,3 +205,8 @@ fs.readFile(path.join(__dirname, "htmlTemplate.html"), "utf-8", (err, html) => {
});
}
});

// eslint-disable-next-line no-undef
module.exports = {
textToP
};