Repository: busyorg/busy
Pull request #1981 (2018-06-20 10:11:59)

refactor: update jsonParse function
### Changes

* Update `jsonParse` function to return null in case of error.
* Update `getAppData` to use string JSON metadata to match real world data.
* Add `jsonParse` tests.




Patch:
diff --git a/src/client/helpers/formatter.js b/src/client/helpers/formatter.js
index 07129c75..a15c8bdb 100644
--- a/src/client/helpers/formatter.js
+++ b/src/client/helpers/formatter.js
@@ -1,11 +1,9 @@
-export const jsonParse = str => {
+export function jsonParse(input) {
   try {
-    return jsonParse(JSON.parse(str));
+    return JSON.parse(input);
   } catch (e) {
-    return str;
+    return null;
   }
-};
+}
 
 export const epochToUTC = epochTimestamp => new Date(0).setUTCSeconds(epochTimestamp);
-
-export default jsonParse;


Test patch:
diff --git a/src/client/helpers/__tests__/formatter.js b/src/client/helpers/__tests__/formatter.js
new file mode 100644
index 00000000..4e9b2e64
--- /dev/null
+++ b/src/client/helpers/__tests__/formatter.js
@@ -0,0 +1,31 @@
+import { jsonParse } from '../formatter';
+
+describe('jsonParse', () => {
+  it('should return undefined on undefined input', () => {
+    expect(jsonParse(undefined)).toBeNull();
+
+    expect(jsonParse('{')).toBeNull();
+
+    expect(jsonParse('hello world')).toBeNull();
+  });
+
+  it('should return parsed JSON', () => {
+    expect(jsonParse('{}')).toEqual({});
+
+    expect(jsonParse('"hello world"')).toEqual('hello world');
+
+    expect(jsonParse(42)).toEqual(42);
+
+    expect(jsonParse('24')).toEqual(24);
+
+    expect(jsonParse('{ "test": "ok" }')).toEqual({
+      test: 'ok',
+    });
+
+    expect(jsonParse('[2, 4, 55]')).toEqual([2, 4, 55]);
+
+    expect(jsonParse('{ "arr": [2, 5] }')).toEqual({
+      arr: [2, 5],
+    });
+  });
+});
diff --git a/src/client/helpers/__tests__/postHelpers.js b/src/client/helpers/__tests__/postHelpers.js
index 0ec26fbc..db3ef014 100644
--- a/src/client/helpers/__tests__/postHelpers.js
+++ b/src/client/helpers/__tests__/postHelpers.js
@@ -2,32 +2,32 @@ import { getAppData, getContentImages } from '../postHelpers';
 
 describe('getAppData', () => {
   it('should return an empty object when post does not contain an app', () => {
-    const post = { json_metadata: {} };
+    const post = { json_metadata: '{}' };
     expect(getAppData(post)).toEqual({});
   });
 
   it('should return an empty object if app is defined but empty', () => {
-    const post = { json_metadata: { app: '' } };
+    const post = { json_metadata: '{ "app": "" }' };
     expect(getAppData(post)).toEqual({});
   });
 
   it('should return an empty object if app is not on the white list', () => {
-    const post = { json_metadata: { app: 'thisappshouldneverbeonthewhitelist12356' } };
+    const post = { json_metadata: '{ "app": "thisappshouldneverbeonthewhitelist12356" }' };
     expect(getAppData(post)).toEqual({});
   });
 
   it('should return an object with the appName and version', () => {
-    const post = { json_metadata: { app: 'busy/1.2.4' } };
+    const post = { json_metadata: '{ "app": "busy/1.2.4" }' };
     expect(getAppData(post)).toEqual({ appName: 'Busy', version: '1.2.4' });
   });
 
   it('should return an object with the appName and empty version if version is absent', () => {
-    const post = { json_metadata: { app: 'busy' } };
+    const post = { json_metadata: '{ "app": "busy" }' };
     expect(getAppData(post)).toEqual({ appName: 'Busy', version: '' });
   });
 
   it('should handle more app parameters without failing, eg. busy/1.2/other', () => {
-    const post = { json_metadata: { app: 'busy/1.2.3/something' } };
+    const post = { json_metadata: '{ "app": "busy/1.2.3/something" }' };
     expect(getAppData(post)).toEqual({ appName: 'Busy', version: '1.2.3' });
   });
 });