From b5b6e4401d5fc2bbc6029e0a5a0677915e60088d Mon Sep 17 00:00:00 2001 From: guerler Date: Mon, 7 Oct 2024 11:19:04 +0300 Subject: [PATCH] Add utility test for value parser --- src/utilities/parseDefaults.js | 8 +++++++- src/utilities/parseDefaults.test.js | 13 +++++++++++++ src/utilities/parseIncoming.js | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 src/utilities/parseDefaults.test.js diff --git a/src/utilities/parseDefaults.js b/src/utilities/parseDefaults.js index 5f90852..1a4039b 100644 --- a/src/utilities/parseDefaults.js +++ b/src/utilities/parseDefaults.js @@ -1,7 +1,13 @@ export function parseDefaults(inputs) { let dv = {}; if (inputs) { - inputs.forEach((input) => (dv[input.name] = input.value)); + inputs.forEach((input) => { + if (input.name) { + dv[input.name] = input.value || null; + } else { + console.debug("Warning: Detected input element with no name."); + } + }); } return dv; } diff --git a/src/utilities/parseDefaults.test.js b/src/utilities/parseDefaults.test.js new file mode 100644 index 0000000..ac546a4 --- /dev/null +++ b/src/utilities/parseDefaults.test.js @@ -0,0 +1,13 @@ +import { parseDefaults } from "./parseDefaults.js"; + +test("parse defaults", () => { + expect( + parseDefaults([ + { name: "a", value: "b" }, + { name: "c", value: "d" }, + { name: "e", value: undefined }, + { name: "f" }, + { value: "g" }, + ]), + ).toEqual({ a: "b", c: "d", "e": null, "f": null }); +}); diff --git a/src/utilities/parseIncoming.js b/src/utilities/parseIncoming.js index 6264936..5ba17eb 100644 --- a/src/utilities/parseIncoming.js +++ b/src/utilities/parseIncoming.js @@ -7,7 +7,7 @@ export function parseIncoming(config) { const root = incoming.root || config?.root || "/"; const visualizationId = incoming.visualization_id; const visualizationPlugin = incoming.visualization_plugin; - const visualizationTitle = incoming.visualization_title || config?.title || "New Chart"; + const visualizationTitle = incoming.visualization_title || config?.title || "Unnamed Visualization"; // parse chart dict let visualizationConfig = incoming.visualization_config || config;