From b8601170ee89f6feb53d1736dbb8b05709c542af Mon Sep 17 00:00:00 2001 From: Fawzi Abdulfattah Date: Sun, 15 Jan 2023 03:45:05 +0200 Subject: [PATCH] Improve the stringified function and unit test it Signed-off-by: Fawzi Abdulfattah --- js/src/functions.js | 8 +++----- test/javascript/functions.test.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 test/javascript/functions.test.js diff --git a/js/src/functions.js b/js/src/functions.js index f817950c8f..8575818200 100644 --- a/js/src/functions.js +++ b/js/src/functions.js @@ -10,7 +10,7 @@ /** * general function, usually for data manipulation pages - * + * @test-module Functions */ var Functions = {}; @@ -3989,13 +3989,11 @@ Functions.getCellValue = function (td) { * @return {string} */ Functions.stringifyJSON = function (json, replacer = null, space = 0) { - let stringifiedJSON = json; try { - stringifiedJSON = JSON.stringify(JSON.parse(json), replacer, space); + return JSON.stringify(JSON.parse(json), replacer, space); } catch (e) { - // will be returned as it's + return json; } - return stringifiedJSON; }; /** diff --git a/test/javascript/functions.test.js b/test/javascript/functions.test.js new file mode 100644 index 0000000000..6ef397f780 --- /dev/null +++ b/test/javascript/functions.test.js @@ -0,0 +1,17 @@ +/* eslint-env node, jest */ + +const Functions = require('phpmyadmin/functions'); + +describe('Functions', () => { + describe('Testing stringifyJSON', function() { + test('Should return the stringified JSON input', () => { + const stringifiedJSON = Functions.stringifyJSON('{ "lang": "php"}', null, 4); + expect(stringifiedJSON).toEqual('{\n "lang": "php"\n}'); + }); + + test('Should return the input as it is', () => { + const stringifiedJSON = Functions.stringifyJSON('{ "name": "notvalid}'); + expect(stringifiedJSON).toEqual('{ "name": "notvalid}'); + }); + }); +});