aboutsummaryrefslogtreecommitdiffstats
path: root/test/jsUtils.test.ts
blob: 8ef69b46fa63fe9360aa2a428b5da3298f2db341 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import * as jsUtils from '../src/jsUtils';

describe('jsUtils', () => {
  describe('ifUndefinedString', () => {
    it('returns the default value for undefined input', () => {
      const result = jsUtils.ifUndefinedString(undefined, 'abc');
      expect(result).toEqual('abc');
    });

    it('returns the default value for null input', () => {
      const result = jsUtils.ifUndefinedString(null, 'abc');
      expect(result).toEqual('abc');
    });

    it('returns the non-default input value for regular string input', () => {
      const result = jsUtils.ifUndefinedString('some random string', 'abc');
      expect(result).toEqual('some random string');
    });
  });

  describe('ifUndefined<string>', () => {
    it('returns the default value for undefined input', () => {
      const result = jsUtils.ifUndefined<string>(undefined, 'abc');
      expect(result).toEqual('abc');
    });

    it('returns the default value for null input', () => {
      const result = jsUtils.ifUndefined<string>(null, 'abc');
      expect(result).toEqual('abc');
    });

    it('returns the non-default input value for regular string input', () => {
      const result = jsUtils.ifUndefined<string>('some random string', 'abc');
      expect(result).toEqual('some random string');
    });
  });

  describe('ifUndefined<boolean>', () => {
    it('returns the default value for undefined input', () => {
      const result = jsUtils.ifUndefined<boolean>(undefined, false);
      expect(result).toEqual(false);
    });

    it('returns the default value for null input', () => {
      const result = jsUtils.ifUndefined<boolean>(null, true);
      expect(result).toEqual(true);
    });

    it('returns the non-default input value for regular boolean input', () => {
      const result = jsUtils.ifUndefined<boolean>(true, false);
      expect(result).toEqual(true);
    });
  });

  describe('ifUndefined<number>', () => {
    it('returns the default value for undefined input', () => {
      const result = jsUtils.ifUndefined<number>(undefined, 123);
      expect(result).toEqual(123);
    });

    it('returns the default value for null input', () => {
      const result = jsUtils.ifUndefined<number>(null, 234);
      expect(result).toEqual(234);
    });

    it('returns the non-default input value for regular Number input', () => {
      const result = jsUtils.ifUndefined<number>(1234, 5678);
      expect(result).toEqual(1234);
    });
  });

  describe('convertToJSON', () => {
    it('returns undefined for undefined input', () => {
      const result = jsUtils.convertToJSON(undefined);
      expect(result).toEqual(undefined);
    });

    it('returns null for null input', () => {
      const result = jsUtils.convertToJSON(null);
      expect(result).toEqual(null);
    });

    it('returns the object for the object input', () => {
      const result = jsUtils.convertToJSON(['a', 'b']);
      expect(result).toEqual(['a', 'b']);
    });

    it('returns the parsed JSON for the string input', () => {
      const result1 = jsUtils.convertToJSON('{"a":"b","c":"d"}');
      expect(result1).toEqual({ a: 'b', c: 'd' });

      const result2 = jsUtils.convertToJSON('[{"a":"b"},{"c":"d"}]');
      expect(result2).toEqual([{ a: 'b' }, { c: 'd' }]);
    });
  });

  describe('cleanseJSObject', () => {
    xit('throws error for undefined input', () => {
      const result = jsUtils.cleanseJSObject(undefined);
      expect(result).toThrow();
    });

    xit('throws error for null input', () => {
      const result = jsUtils.cleanseJSObject(null);
      expect(result).toThrow();
    });

    it('returns cloned object for valid input', () => {
      const result = jsUtils.cleanseJSObject([{ a: 'b' }, { c: 'd' }]);
      expect(result).toEqual([{ a: 'b' }, { c: 'd' }]);
    });
  });
});