Improve the stringified function and unit test it

Signed-off-by: Fawzi Abdulfattah <iifawzie@gmail.com>
This commit is contained in:
Fawzi Abdulfattah 2023-01-15 03:45:05 +02:00
parent 59a5f9b6b6
commit b8601170ee
No known key found for this signature in database
GPG Key ID: 197FA14E280B8A96
2 changed files with 20 additions and 5 deletions

View File

@ -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;
};
/**

View File

@ -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}');
});
});
});