From 7da1c3a96e2657d61f8fdd6555ee31be7410e617 Mon Sep 17 00:00:00 2001 From: Jan Macku Date: Tue, 30 Aug 2022 00:19:38 +0200 Subject: [PATCH] fix: Handle correctly issues with an empty body (#34) * fix: Handle correctly issues with an empty body Fixes: #33 * Update fixtures/blank/issue.js Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com> Co-authored-by: Stefan Buck Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com> --- fixtures/blank/expected.json | 1 + fixtures/blank/form.yml | 9 +++++++ fixtures/blank/issue.js | 5 ++++ index.js | 2 +- test.spec.js | 50 ++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 fixtures/blank/expected.json create mode 100644 fixtures/blank/form.yml create mode 100644 fixtures/blank/issue.js diff --git a/fixtures/blank/expected.json b/fixtures/blank/expected.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/fixtures/blank/expected.json @@ -0,0 +1 @@ +{} diff --git a/fixtures/blank/form.yml b/fixtures/blank/form.yml new file mode 100644 index 0000000..cb5bf9f --- /dev/null +++ b/fixtures/blank/form.yml @@ -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 diff --git a/fixtures/blank/issue.js b/fixtures/blank/issue.js new file mode 100644 index 0000000..7448de4 --- /dev/null +++ b/fixtures/blank/issue.js @@ -0,0 +1,5 @@ +module.exports = { + issue: { + body: null, + }, +}; diff --git a/index.js b/index.js index 0468164..d679020 100644 --- a/index.js +++ b/index.js @@ -39,7 +39,7 @@ async function run(env, eventPayload, fs, core) { } let result; - const body = eventPayload.issue.body; + const body = eventPayload.issue.body || ''; const idMapping = getIDsFromIssueTemplate(form); function toKey(str) { diff --git a/test.spec.js b/test.spec.js index 7c71167..13585be 100644 --- a/test.spec.js +++ b/test.spec.js @@ -106,3 +106,53 @@ it("multiple paragraphs", () => { run(env, eventPayload, fs, core); }); + +it("blank", () => { + const expectedOutput = require("./fixtures/blank/expected.json"); + const expectedOutputJson = JSON.stringify(expectedOutput, null, 2); + + // mock ENV + const env = { + HOME: "", + }; + + // mock event payload + const eventPayload = require("./fixtures/blank/issue"); + + // mock fs + const fs = { + readFileSync(path, encoding) { + expect(path).toBe(""); + expect(encoding).toBe("utf8"); + return readFileSync("fixtures/blank/form.yml", "utf-8"); + }, + writeFileSync(path, content) { + expect(path).toBe("/issue-parser-result.json"); + expect(content).toBe(expectedOutputJson); + }, + }; + + // mock core + const core = { + getInput(inputName) { + expect(inputName).toBe("template-path"); + return ""; + }, + 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); +});