-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathformatComplexDataStructure.spec.js
More file actions
159 lines (132 loc) · 3.5 KB
/
formatComplexDataStructure.spec.js
File metadata and controls
159 lines (132 loc) · 3.5 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* @flow */
import React from 'react';
import formatComplexDataStructure from './formatComplexDataStructure';
jest.mock('./formatReactElementNode.js', () => node =>
`<${node.displayName} />`
);
const createFakeReactElement = (tagName = 'Foo') =>
React.createElement(tagName, {}, null);
const options = { tabStop: 2 };
describe('formatComplexDataStructure', () => {
it('should format an object', () => {
const fixture = { a: 1, b: { c: 'ccc' } };
expect(
formatComplexDataStructure(fixture, 'foo', false, 0, options)
).toEqual(
`{
a: 1,
b: {
c: 'ccc'
}
}`
);
});
it('should format inline an object', () => {
const fixture = { a: 1, b: { c: 'ccc' } };
expect(
formatComplexDataStructure(fixture, 'foo', true, 0, options)
).toEqual("{a: 1, b: {c: 'ccc'}}");
});
it('should format an empty object', () => {
expect(formatComplexDataStructure({}, 'foo', false, 0, options)).toEqual(
'{}'
);
});
it('should order the object keys', () => {
const fixture = { b: { d: 'ddd', c: 'ccc' }, a: 1 };
expect(
formatComplexDataStructure(fixture, 'foo', false, 0, options)
).toEqual(
`{
a: 1,
b: {
c: 'ccc',
d: 'ddd'
}
}`
);
});
it('should format an array', () => {
const fixture = [1, '2', true, false, null];
expect(
formatComplexDataStructure(fixture, 'foo', false, 0, options)
).toEqual(
`[
1,
'2',
true,
false,
null
]`
);
});
it('should format inline an array ', () => {
const fixture = [1, '2', true, false, null];
expect(
formatComplexDataStructure(fixture, 'foo', true, 0, options)
).toEqual("[1, '2', true, false, null]");
});
it('should format an object that contains a react element', () => {
const fixture = { a: createFakeReactElement('BarBar') };
expect(
formatComplexDataStructure(fixture, 'foo', false, 0, options)
).toEqual(
`{
a: <BarBar />
}`
);
});
it('should format an empty array', () => {
expect(formatComplexDataStructure([], 'foo', false, 0, options)).toEqual(
'[]'
);
});
it('should format an object that contains a date', () => {
const fixture = { a: new Date('2017-11-13T00:00:00.000Z') };
expect(
formatComplexDataStructure(fixture, 'foo', true, 0, options)
).toEqual(`{a: new Date('2017-11-13T00:00:00.000Z')}`);
});
it('should format an object that contains a regexp', () => {
const fixture = { a: /test/g };
expect(
formatComplexDataStructure(fixture, 'foo', true, 0, options)
).toEqual(`{a: /test/g}`);
});
it('should replace a function with noRefCheck', () => {
const fixture = {
a: function hello() {
return 1;
},
};
expect(
formatComplexDataStructure(fixture, 'foo', true, 0, options)
).toEqual('{a: function noRefCheck() {}}');
});
it('should format a function', () => {
const fixture = {
a: function hello() {
return 1;
},
};
expect(
formatComplexDataStructure(fixture, 'foo', true, 0, {
...options,
showFunctions: true,
})
).toEqual('{a: function hello() {return 1;}}');
});
it('should use the functionValue option', () => {
const fixture = {
a: function hello() {
return 1;
},
};
expect(
formatComplexDataStructure(fixture, 'foo', true, 0, {
...options,
functionValue: () => '<Test />',
})
).toEqual('{a: <Test />}');
});
});