-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.js
181 lines (170 loc) · 2.74 KB
/
test.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
/* eslint-disable id-blacklist, no-console */
const qi = require('./index');
describe('stringify()', () => {
it('should color highlight basic types', () => {
const result = qi.stringify({
a: undefined,
b: null,
c: 0,
d: 42,
e: '42',
f: false,
g: true,
h: /pizza/,
i: [1, 2],
j: { x: 1, y: 2 },
});
expect(result).toMatchSnapshot();
});
it('should print contructor names for classes', () => {
class Pizza {
constructor(options) {
this.name = options.name;
}
}
const result = qi.stringify({
x: new Pizza({ name: 'Quattro Formaggi' }),
});
expect(result).toMatchSnapshot();
});
it('should collapse functions longer than one line', () => {
function fn(x) {
return x * 2;
}
const result = qi.stringify({
a: fn,
c: x => x * 2,
});
expect(result).toMatchSnapshot();
});
it('should collapse arrays with more than 30 items', () => {
const result = qi.stringify({
a: [1, 2],
b: [
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,
],
});
expect(result).toMatchSnapshot();
});
it('should collapse objects with more than 30 keys', () => {
const result = qi.stringify({
a: { k1: 0, k2: 0 },
b: {
k1: 0,
k2: 0,
k3: 0,
k4: 0,
k5: 0,
k6: 0,
k7: 0,
k8: 0,
k9: 0,
k10: 0,
k11: 0,
k12: 0,
k13: 0,
k14: 0,
k15: 0,
k16: 0,
k17: 0,
k18: 0,
k19: 0,
k20: 0,
k21: 0,
k22: 0,
k23: 0,
k24: 0,
k25: 0,
k26: 0,
k27: 0,
k28: 0,
k29: 0,
k30: 0,
k31: 0,
},
});
expect(result).toMatchSnapshot();
});
it('should stringify objects without constructor', () => {
const result = qi.stringify({
a: Object.create(null),
});
expect(result).toMatchSnapshot();
});
it('should accept custom max items', () => {
const result = qi.stringify(
{
a: { k1: 0, k2: 0 },
b: {
k1: 0,
k2: 0,
k3: 0,
k4: 0,
},
},
{ maxItems: 3 }
);
expect(result).toMatchSnapshot();
});
});
describe('print()', () => {
const console$log = console.log;
beforeEach(() => {
console.log = jest.fn();
});
afterEach(() => {
console.log = console$log;
});
it('should print an object', () => {
qi.print({
a: 42,
});
expect(console.log).toBeCalledWith(expect.stringMatching('42'));
});
it('should accept custom max items', () => {
qi.print(
{
a: { k1: 0, k2: 0 },
b: {
k1: 0,
k2: 0,
k3: 0,
k4: 0,
},
},
{ maxItems: 3 }
);
expect(console.log).toBeCalledWith(expect.stringMatching('Object \\{4\\}'));
});
});