1 | import toSource from './tosource';
|
2 |
|
3 |
|
4 | describe('toSource', () => {
|
5 | it('works on kitchen sink', () => {
|
6 | const date = new Date();
|
7 | const a = undefined;
|
8 | function foo(bar: any) {
|
9 | console.log('woo! a is ' + a);
|
10 | console.log('and bar is ' + bar);
|
11 | }
|
12 | const bar = () => 45;
|
13 | const v = toSource([
|
14 | 0,
|
15 | -0,
|
16 | 4,
|
17 | 5,
|
18 | 6,
|
19 | 'hello',
|
20 | {
|
21 | 0: 1,
|
22 | a: 2,
|
23 | b: 3,
|
24 | '1': 4,
|
25 | if: 5,
|
26 | yes: true,
|
27 | no: false,
|
28 | nan: NaN,
|
29 | infinity: Infinity,
|
30 | undefined: undefined,
|
31 | null: null,
|
32 | foo,
|
33 | bar,
|
34 | map: new Map<any, any>([
|
35 | ['hello', 45],
|
36 | [45, 'hello'],
|
37 | ]),
|
38 | set: new Set(['hello', 45]),
|
39 | },
|
40 | /we$/gi,
|
41 | new RegExp('/w/e/', 'ig'),
|
42 | /\/w\/e\
|
43 | date,
|
44 | new Date('Wed, 09 Aug 1995 00:00:00 GMT'),
|
45 | ]);
|
46 |
|
47 | expect(v).toEqual(
|
48 | `[ 0,
|
49 | -0,
|
50 | 4,
|
51 | 5,
|
52 | 6,
|
53 | "hello",
|
54 | { 0:1,
|
55 | 1:4,
|
56 | a:2,
|
57 | b:3,
|
58 | "if":5,
|
59 | yes:true,
|
60 | no:false,
|
61 | nan:NaN,
|
62 | infinity:Infinity,
|
63 | "undefined":undefined,
|
64 | "null":null,
|
65 | foo:function foo(bar) {
|
66 | console.log('woo! a is ' + a);
|
67 | console.log('and bar is ' + bar);
|
68 | },
|
69 | bar:() => 45,
|
70 | map:new Map([ [ "hello",
|
71 | 45 ],
|
72 | [ 45,
|
73 | "hello" ] ]),
|
74 | set:new Set([ "hello",
|
75 | 45 ]) },
|
76 | /we$/gi,
|
77 | /\\/w\\/e\\//gi,
|
78 | /\\/w\\/e\\//gim,
|
79 | new Date(${date.getTime()}),
|
80 | new Date(807926400000) ]`,
|
81 | );
|
82 | });
|
83 |
|
84 | it('zero', () => {
|
85 | expect(toSource(-0)).toEqual('-0');
|
86 | expect(toSource(0)).toEqual('0');
|
87 | });
|
88 |
|
89 | it('sparse array', () => {
|
90 | expect(toSource([1, , ,], undefined, false)).toEqual('[1,,]');
|
91 | });
|
92 |
|
93 | it('sparse array 2', () => {
|
94 | expect(toSource([1, , , 3], undefined, false)).toEqual('[1,,,3]');
|
95 | });
|
96 |
|
97 | it('negative Infinity', () => {
|
98 | expect(toSource(-Infinity)).toEqual('-Infinity');
|
99 | });
|
100 |
|
101 | it('filters parameter', () => {
|
102 |
|
103 | expect(
|
104 | toSource([4, 5, 6, { bar: 3 }], function numbersToStrings(value) {
|
105 | return typeof value === 'number' ? '<' + value + '>' : value;
|
106 | }),
|
107 | ).toEqual(`[ "<4>",\n "<5>",\n "<6>",\n { bar:"<3>" } ]`);
|
108 | });
|
109 |
|
110 | it('generates with no indent', () => {
|
111 | expect(toSource([4, 5, 6, { bar: 3 }], undefined, false)).toEqual(
|
112 | '[4,5,6,{bar:3}]',
|
113 | );
|
114 | });
|
115 |
|
116 | it('handles circular reference', () => {
|
117 | const object: any = { a: 1, b: 2 };
|
118 | object.c = object;
|
119 |
|
120 | expect(toSource(object)).toEqual(
|
121 | '{ a:1,\n' + ' b:2,\n' + ' c:{$circularReference:1} }',
|
122 | );
|
123 | });
|
124 | it('allows multiple references to the same object', () => {
|
125 |
|
126 | const foo = {};
|
127 | const object = { a: foo, b: foo };
|
128 |
|
129 | expect(toSource(object)).toEqual('{ a:{},\n' + ' b:{} }');
|
130 | });
|
131 | });
|