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

fix: multi paragraphs parsing #15

Merged
merged 3 commits into from
Sep 12, 2021
Merged
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
4 changes: 4 additions & 0 deletions fixtures/multiple-paragraphs/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"textarea_one": "1st paragraph\n\n2nd paragraph",
"textarea_two": "1st paragraph\n2nd paragraph"
}
9 changes: 9 additions & 0 deletions fixtures/multiple-paragraphs/form.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body:
- type: textarea
id: textarea-one
attributes:
label: My textarea input
- type: textarea
id: textarea-two
attributes:
label: Another textarea input
10 changes: 10 additions & 0 deletions fixtures/multiple-paragraphs/issue-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### My textarea input

1st paragraph

2nd paragraph

### Another textarea input

1st paragraph
2nd paragraph
10 changes: 10 additions & 0 deletions fixtures/multiple-paragraphs/issue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { resolve } = require("path");
const { readFileSync } = require("fs");

const issueBodyPath = resolve(__dirname, "issue-body.md");

module.exports = {
issue: {
body: readFileSync(issueBodyPath, "utf-8"),
},
};
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ async function run(env, eventPayload, fs, core) {
.replace(/[^\w\s]/gi, "")
.replace(/\s/g, "_");
}
function toValue(val) {
if (typeof val !== "string") {
return val;
function toValue(input, ...extraLines) {
if (typeof input !== "string") {
return input;
}

const value = val.trim();
const value = [input, ...extraLines].join("\n\n").trim();

if (value.toLowerCase() === "_no response_") {
return "";
}
Expand Down
52 changes: 51 additions & 1 deletion test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { readFileSync } = require("fs");

const { run } = require(".");

it("Smoke test", () => {
it("smoke test", () => {
expect(run).toBeDefined();
expect(typeof run).toBe("function");
});
Expand Down Expand Up @@ -56,3 +56,53 @@ it("readme example", () => {

run(env, eventPayload, fs, core);
});

it("multiple paragraphs", () => {
const expectedOutput = require("./fixtures/multiple-paragraphs/expected.json");
const expectedOutputJson = JSON.stringify(expectedOutput, null, 2);

// mock ENV
const env = {
HOME: "<home path>",
};

// mock event payload
const eventPayload = require("./fixtures/multiple-paragraphs/issue");

// mock fs
const fs = {
readFileSync(path, encoding) {
expect(path).toBe("<template-path>");
expect(encoding).toBe("utf8");
return readFileSync("fixtures/multiple-paragraphs/form.yml", "utf-8");
},
writeFileSync(path, content) {
expect(path).toBe("<home path>/issue-parser-result.json");
expect(content).toBe(expectedOutputJson);
},
};

// mock core
const core = {
getInput(inputName) {
expect(inputName).toBe("template-path");
return "<template-path>";
},
setOutput(outputName, outputValue) {
if (outputName === "jsonString") {
expect(outputValue).toBe(expectedOutputJson);
return;
}

if (outputName.startsWith("issueparser_")) {
const key = outputName.substr("issueparser_".length);
expect(Object.keys(expectedOutput)).toContain(key);

expect(outputValue).toBe(expectedOutput[key]);
return;
}
},
};

run(env, eventPayload, fs, core);
});