forked from cdk8s-team/cdk8s-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.test.ts
54 lines (44 loc) · 2.13 KB
/
util.test.ts
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
import { sanitizeValue } from '../src/_util';
describe('sanitizeValue', () => {
test('default options', () => {
expect(sanitizeValue(null)).toBe(undefined);
expect(sanitizeValue(undefined)).toBe(undefined);
expect(sanitizeValue({ })).toStrictEqual({ });
expect(sanitizeValue([])).toStrictEqual([]);
expect(sanitizeValue(1)).toBe(1);
expect(sanitizeValue({ hello: 123 })).toStrictEqual({ hello: 123 });
expect(sanitizeValue([1, 2, 3])).toStrictEqual([1, 2, 3]);
expect(sanitizeValue({ xoo: 123, foo: [] })).toStrictEqual({ xoo: 123, foo: [] });
expect(sanitizeValue({ xoo: { }, foo: { bar: { zoo: undefined, hey: { }, me: 123 } } }))
.toStrictEqual({ xoo: { }, foo: { bar: { hey: { }, me: 123 } } });
expect(sanitizeValue({ xoo: 123, foo: [1, 2, { foo: 123, bar: undefined, zoo: [] }, 3] }))
.toStrictEqual({ xoo: 123, foo: [1, 2, { foo: 123, zoo: [] }, 3] });
expect(sanitizeValue([1, 2, 3, [], { }, 4])).toStrictEqual([1, 2, 3, [], { }, 4]); // special case
expect(() => sanitizeValue(new Dummy())).toThrow(/can't render non-simple object of type 'Dummy'/);
});
test('filterEmptyArrays', () => {
const options = {
filterEmptyArrays: true,
};
expect(sanitizeValue([], options)).toBe(undefined);
expect(sanitizeValue({ foo: [], bar: [1, 2] }, options)).toStrictEqual({ bar: [1, 2] });
expect(sanitizeValue({ foo: { bar: [] } }, options)).toStrictEqual({ foo: { } });
});
test('filterEmptyObjects', () => {
const options = {
filterEmptyObjects: true,
};
expect(sanitizeValue({ }, options)).toBe(undefined);
expect(sanitizeValue({ foo: { }, bar: { hey: 'there' } }, options)).toStrictEqual({ bar: { hey: 'there' } });
expect(sanitizeValue({ foo: { bar: { } } }, options)).toStrictEqual(undefined);
});
test('sortKeys', () => {
const input = { zzz: 999, aaa: 111, nested: { foo: { zag: [1, 2, 3], bar: '1111' } } };
expect(str(sanitizeValue(input))).toMatchSnapshot();
expect(str(sanitizeValue(input, { sortKeys: false }))).toMatchSnapshot();
});
class Dummy { }
});
function str(obj: any) {
return JSON.stringify(obj, undefined, 2);
}