aboutsummaryrefslogtreecommitdiffstats
path: root/src/jsUtils.test.ts
diff options
context:
space:
mode:
authorLibravatar Vijay A <vraravam@users.noreply.github.com>2022-05-15 01:44:18 -0500
committerLibravatar Vijay Aravamudhan <vraravam@users.noreply.github.com>2022-05-15 19:05:55 -0500
commit082b64b6c40fcab3ecf303f53ce83e7753894a32 (patch)
tree92626869949cc671effce89cfbcd52092243de5c /src/jsUtils.test.ts
parentfix: revert "Typescript conversion" (#153) (diff)
downloadferdium-app-082b64b6c40fcab3ecf303f53ce83e7753894a32.tar.gz
ferdium-app-082b64b6c40fcab3ecf303f53ce83e7753894a32.tar.zst
ferdium-app-082b64b6c40fcab3ecf303f53ce83e7753894a32.zip
Extract utility functions for JSON parsing
Diffstat (limited to 'src/jsUtils.test.ts')
-rw-r--r--src/jsUtils.test.ts96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/jsUtils.test.ts b/src/jsUtils.test.ts
new file mode 100644
index 000000000..651caee5f
--- /dev/null
+++ b/src/jsUtils.test.ts
@@ -0,0 +1,96 @@
1import * as jsUtils from './jsUtils'
2
3describe('jsUtils', () => {
4 describe('ifUndefinedString', () => {
5 it('returns the default value for undefined input', () => {
6 const result = jsUtils.ifUndefinedString(undefined, 'abc');
7 expect(result).toEqual('abc');
8 });
9
10 it('returns the default value for null input', () => {
11 const result = jsUtils.ifUndefinedString(null, 'abc');
12 expect(result).toEqual('abc');
13 });
14
15 it('returns the non-default input value for regular string input', () => {
16 const result = jsUtils.ifUndefinedString('some random string', 'abc');
17 expect(result).toEqual('some random string');
18 });
19 });
20
21 describe('ifUndefinedBoolean', () => {
22 it('returns the default value for undefined input', () => {
23 const result = jsUtils.ifUndefinedBoolean(undefined, false);
24 expect(result).toEqual(false);
25 });
26
27 it('returns the default value for null input', () => {
28 const result = jsUtils.ifUndefinedBoolean(null, true);
29 expect(result).toEqual(true);
30 });
31
32 it('returns the non-default input value for regular boolean input', () => {
33 const result = jsUtils.ifUndefinedBoolean(true, false);
34 expect(result).toEqual(true);
35 });
36 });
37
38 describe('ifUndefinedNumber', () => {
39 it('returns the default value for undefined input', () => {
40 const result = jsUtils.ifUndefinedNumber(undefined, 123);
41 expect(result).toEqual(123);
42 });
43
44 it('returns the default value for null input', () => {
45 const result = jsUtils.ifUndefinedNumber(null, 234);
46 expect(result).toEqual(234);
47 });
48
49 it('returns the non-default input value for regular Number input', () => {
50 const result = jsUtils.ifUndefinedNumber(1234, 5678);
51 expect(result).toEqual(1234);
52 });
53 });
54
55 describe('convertToJSON', () => {
56 it('returns undefined for undefined input', () => {
57 const result = jsUtils.convertToJSON(undefined);
58 expect(result).toEqual(undefined);
59 });
60
61 it('returns null for null input', () => {
62 const result = jsUtils.convertToJSON(null);
63 expect(result).toEqual(null);
64 });
65
66 it('returns the object for the object input', () => {
67 const result = jsUtils.convertToJSON(['a', 'b']);
68 expect(result).toEqual(['a', 'b']);
69 });
70
71 it('returns the parsed JSON for the string input', () => {
72 const result1 = jsUtils.convertToJSON('{"a":"b","c":"d"}');
73 expect(result1).toEqual({a: 'b', c: 'd'});
74
75 const result2 = jsUtils.convertToJSON('[{"a":"b"},{"c":"d"}]');
76 expect(result2).toEqual([{a: 'b'}, {c: 'd'}]);
77 });
78 });
79
80 describe('cleanseJSObject', () => {
81 xit('throws error for undefined input', () => {
82 const result = jsUtils.cleanseJSObject(undefined);
83 expect(result).toThrow();
84 });
85
86 xit('throws error for null input', () => {
87 const result = jsUtils.cleanseJSObject(null);
88 expect(result).toThrow();
89 });
90
91 it('returns cloned object for valid input', () => {
92 const result = jsUtils.cleanseJSObject([{a: 'b'}, {c: 'd'}]);
93 expect(result).toEqual([{a: 'b'}, {c: 'd'}]);
94 });
95 });
96});