Improve the stringified function and unit test it
Signed-off-by: Fawzi Abdulfattah <iifawzie@gmail.com>
This commit is contained in:
parent
59a5f9b6b6
commit
b8601170ee
@ -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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
17
test/javascript/functions.test.js
Normal file
17
test/javascript/functions.test.js
Normal 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}');
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user