forked from MetaMask/utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.test.ts
243 lines (203 loc) · 6.59 KB
/
misc.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import type { RuntimeObject } from '.';
import {
isNonEmptyArray,
isNullOrUndefined,
isObject,
hasProperty,
getKnownPropertyNames,
isPlainObject,
calculateNumberSize,
isASCII,
calculateStringSize,
} from '.';
describe('miscellaneous', () => {
describe('isNonEmptyArray', () => {
it('identifies non-empty arrays', () => {
[[1], [1, 2], [1, 2, 3, 4, 5]].forEach((nonEmptyArray) => {
expect(isNonEmptyArray<unknown>(nonEmptyArray)).toBe(true);
});
});
it('identifies empty arrays', () => {
expect(isNonEmptyArray<unknown>([])).toBe(false);
});
});
describe('isNullOrUndefined', () => {
it('identifies null and undefined', () => {
expect(isNullOrUndefined(null)).toBe(true);
expect(isNullOrUndefined(undefined)).toBe(true);
});
it('identifies non-nullish values', () => {
[false, 1, 0, -1, '', [], () => undefined].forEach(
(nonNullOrUndefinedValue) => {
expect(isNullOrUndefined(nonNullOrUndefinedValue)).toBe(false);
},
);
});
});
describe('isObject', () => {
class Foo {}
it('identifies object values', () => {
[new Foo(), {}, Promise.resolve(), new Error()].forEach((objectValue) => {
expect(isObject(objectValue)).toBe(true);
});
});
it('identifies non-object values', () => {
[
Symbol('foo'),
[],
() => undefined,
Promise.resolve.bind(Promise),
1,
null,
undefined,
'a',
].forEach((nonObjectValue) => {
expect(isObject(nonObjectValue)).toBe(false);
});
});
});
describe('hasProperty', () => {
const symbol = Symbol('bar');
const getNonEnumerable = (
key: string | number | symbol,
value: unknown = 'foo',
): RuntimeObject => {
const obj = {};
Object.defineProperty(obj, key, {
value,
enumerable: false,
});
return obj;
};
it('returns `true` for enumerable properties', () => {
(
[
[{ a: 1 }, 'a'],
[{ [symbol]: 1 }, symbol],
[{ two: 'b' }, 'two'],
[{ a: 1, two: 'b', c: 'x' }, 'c'],
] as const
).forEach(([objectValue, property]) => {
expect(hasProperty(objectValue, property)).toBe(true);
});
});
it('returns `true` for non-enumerable properties', () => {
(
[
[getNonEnumerable('a'), 'a'],
[getNonEnumerable(symbol), symbol],
[getNonEnumerable(2), 2],
] as const
).forEach(([objectValue, property]) => {
expect(hasProperty(objectValue, property)).toBe(true);
});
});
it('returns `false` for missing properties', () => {
(
[
[{}, 'a'],
[{ a: 1 }, 'b'],
] as const
).forEach(([objectValue, property]) => {
expect(hasProperty(objectValue, property)).toBe(false);
});
});
});
describe('getKnownPropertyNames', () => {
it('returns the own property names of the object', () => {
const object = { foo: 'bar', baz: 'qux' };
expect(getKnownPropertyNames(object)).toStrictEqual(['foo', 'baz']);
});
it('does not return inherited properties', () => {
const superObject = { foo: 'bar' };
const object = Object.create(superObject);
expect(getKnownPropertyNames(object)).toStrictEqual([]);
});
});
describe('isPlainObject', () => {
it('should return true for a plain object', () => {
const somePlainObject = {
someKey: 'someValue',
};
expect(isPlainObject(somePlainObject)).toBe(true);
});
it('should return false if function is passed', () => {
const someFunction = (someArg: string) => {
return someArg;
};
expect(isPlainObject(someFunction)).toBe(false);
});
it('should return false if Set object is passed', () => {
const someSet = new Set();
someSet.add('something');
expect(isPlainObject(someSet)).toBe(false);
});
it('should return false if an exception is thrown', () => {
const someObject = { something: 'anything' };
jest.spyOn(Object, 'getPrototypeOf').mockImplementationOnce(() => {
throw new Error();
});
expect(isPlainObject(someObject)).toBe(false);
});
});
describe('isASCII', () => {
it('should return true for "A" which is the ASCII character', () => {
expect(isASCII('A')).toBe(true);
});
it('should return false for "Š" which is not the ASCII character', () => {
expect(isASCII('Š')).toBe(false);
});
});
describe('calculateStringSize', () => {
it('should return 96 for a size of ASCII string', () => {
const str =
'!"#$%&\'()*+,-./0123456789:;<=>?' +
'@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]' +
'^_`abcdefghijklmnopqrstuvwxyz{|}~';
expect(calculateStringSize(str)).toBe(96);
});
it('should return 10 for a size of UTF8 string', () => {
const str = 'šđćčž';
expect(calculateStringSize(str)).toBe(10);
});
it('should return 15 for a size of mixed, ASCII and UTF8 string', () => {
const str = 'ašbđcćdčež';
expect(calculateStringSize(str)).toBe(15);
});
it('should return 10 for a size of special characters', () => {
const str = '"\\\n\r\t';
expect(calculateStringSize(str)).toBe(10);
});
});
describe('calculateNumberSize', () => {
it('should return 3 for a "100" number size', () => {
expect(calculateNumberSize(100)).toBe(3);
});
it('should return 4 for a "-100" number size', () => {
expect(calculateNumberSize(-100)).toBe(4);
});
it('should return 4 for a "-0.3" number size', () => {
expect(calculateNumberSize(-0.3)).toBe(4);
});
it('should return 7 for a "-123.45" number size', () => {
expect(calculateNumberSize(-123.45)).toBe(7);
});
it('should return 5 for a "0.0000000005" number size', () => {
// Because the number provided here will be changed to exponential notation
// 5e-10 by default
expect(calculateNumberSize(0.0000000005)).toBe(5);
});
it('should return 16 for a "9007199254740991" number size', () => {
expect(calculateNumberSize(9007199254740991)).toBe(16);
});
it('should return 17 for a "-9007199254740991" number size', () => {
expect(calculateNumberSize(-9007199254740991)).toBe(17);
});
it('should return 1 for a "0" number size', () => {
expect(calculateNumberSize(0)).toBe(1);
});
it('should return 15 for a "100000.00000008" number size', () => {
expect(calculateNumberSize(100000.00000008)).toBe(15);
});
});
});