From b9cd4d4ae8bf62443113690cad70db3fa2ae2bd9 Mon Sep 17 00:00:00 2001 From: "Gregor \"hrax\" Magdolen" Date: Sat, 21 Sep 2024 16:56:40 +0200 Subject: [PATCH] convert old basic JSONReport test --- ...orSpec.bak => JSONReportGenerator.spec.ts} | 85 ++++++++++--------- ...enerator.js => DocxReportGenerator.js.bak} | 0 src/generator/JSONReportGenerator.ts | 7 +- ...Generator.js => PDFReportGenerator.js.bak} | 0 4 files changed, 53 insertions(+), 39 deletions(-) rename spec/generator/{JSONReportGeneratorSpec.bak => JSONReportGenerator.spec.ts} (51%) rename src/generator/{DocxReportGenerator.js => DocxReportGenerator.js.bak} (100%) rename src/generator/{PDFReportGenerator.js => PDFReportGenerator.js.bak} (100%) diff --git a/spec/generator/JSONReportGeneratorSpec.bak b/spec/generator/JSONReportGenerator.spec.ts similarity index 51% rename from spec/generator/JSONReportGeneratorSpec.bak rename to spec/generator/JSONReportGenerator.spec.ts index dac56c3..4c2cfac 100644 --- a/spec/generator/JSONReportGeneratorSpec.bak +++ b/spec/generator/JSONReportGenerator.spec.ts @@ -1,9 +1,8 @@ -const JSONReportGenerator = require("../../modules/generator/JSONReportGenerator.js"); +import JSONReportGenerator from "../../src/generator/JSONReportGenerator.js"; +import { UpdateXMLScan } from "../../src/linter/UpdateXMLScan.js"; -const UpdateXMLScan = require("../../modules/linter/UpdateXMLScan.js"); - -xdescribe("JSONReportGenerator", () => { - let data = {} +describe("JSONReportGenerator", () => { + let data = {}; beforeAll(() => { data = { @@ -12,7 +11,8 @@ xdescribe("JSONReportGenerator", () => { title: "title", query: "query", changes: [ - ["change1", + [ + "change1", { targetName: "targetName", type: "type", @@ -21,7 +21,7 @@ xdescribe("JSONReportGenerator", () => { createdOn: "createdOn", updatedBy: "updatedBy", updatedOn: "updatedOn", - status: UpdateXMLScan.STATUS.ERROR, + status: "ERROR", action: "INSERT_OR_UPDATE", updates: "1", errorCount: "1", @@ -30,21 +30,24 @@ xdescribe("JSONReportGenerator", () => { targetTable: "targetTable", targetId: "targetId", reports: [ - ["script", { - messages: [ - { - line: 1, - column: 1, - severity: 2, - ruleId: "no-console", - message: "message" - } - ] - }] + [ + "script", { + messages: [ + { + line: 1, + column: 1, + severity: 2, + ruleId: "no-console", + message: "message" + } + ] + } + ] ] } ], - ["change2", + [ + "change2", { targetName: "targetName", type: "type", @@ -53,7 +56,7 @@ xdescribe("JSONReportGenerator", () => { createdOn: "createdOn", updatedBy: "updatedBy", updatedOn: "updatedOn", - status: UpdateXMLScan.STATUS.WARNING, + status: "WARNING", action: "INSERT_OR_UPDATE", updates: "1", errorCount: "0", @@ -62,36 +65,42 @@ xdescribe("JSONReportGenerator", () => { targetTable: "targetTable", targetId: "targetId", reports: [ - ["script", { - messages: [ - { - line: 1, - column: 1, - severity: 1, - ruleId: "no-console", - message: "message" - } - ] - }] + [ + "script", { + messages: [ + { + line: 1, + column: 1, + severity: 1, + ruleId: "no-console", + message: "message" + } + ] + } + ] ] } ] ], resources: { - "overview-resources": [{ - label: "Google", - link: "http://example.com" - }] + "overview-resources": [ + { + label: "Google", + link: "http://example.com" + } + ] } }; }); - it("builds document body", () => { + it("should build document body", () => { const generator = new JSONReportGenerator(); const document = generator.build(data); // Just check that output is string and 2nd line starts with 2 spaces - expect(document).toBeInstanceOf(String); - expect(document).toMatch(/^\{\n .*/); + expect(typeof document).toBe("string"); + expect(document).toMatch(/^\{\n {2}.*/); }); + + it.todo("should serialize map properties as Object"); }); \ No newline at end of file diff --git a/src/generator/DocxReportGenerator.js b/src/generator/DocxReportGenerator.js.bak similarity index 100% rename from src/generator/DocxReportGenerator.js rename to src/generator/DocxReportGenerator.js.bak diff --git a/src/generator/JSONReportGenerator.ts b/src/generator/JSONReportGenerator.ts index 308decb..72f6c88 100644 --- a/src/generator/JSONReportGenerator.ts +++ b/src/generator/JSONReportGenerator.ts @@ -7,7 +7,12 @@ export default class JSONReportGenerator extends AbstractReportGenerator { private padding = 2; build(data: unknown): string { - return JSON.stringify(data, null, this.padding); + return JSON.stringify(data, function(key: unknown, value: unknown) { + if (typeof value === "object" && value instanceof Map) { + return Object.fromEntries((value as Map).entries()); + } + return value; + }, this.padding); } extension(): string { diff --git a/src/generator/PDFReportGenerator.js b/src/generator/PDFReportGenerator.js.bak similarity index 100% rename from src/generator/PDFReportGenerator.js rename to src/generator/PDFReportGenerator.js.bak