-
Notifications
You must be signed in to change notification settings - Fork 484
Expand file tree
/
Copy pathjson-with-typed-arrays.test.ts
More file actions
129 lines (109 loc) · 4.42 KB
/
Copy pathjson-with-typed-arrays.test.ts
File metadata and controls
129 lines (109 loc) · 4.42 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { jsonEncodeObjectWithTypedArraysAsRegularArrays as encode } from '../../utils/json-with-typed-arrays';
describe('jsonEncodeObjectWithTypedArraysAsRegularArrays', function () {
it('encodes primitives the same as JSON.stringify', function () {
expect(encode(42)).toBe('42');
expect(encode('hello')).toBe('"hello"');
expect(encode(null)).toBe('null');
expect(encode(true)).toBe('true');
});
it('encodes a plain object the same as JSON.stringify', function () {
const obj = { a: 1, b: 'two', c: [1, 2, 3], d: { nested: true } };
expect(encode(obj)).toBe(JSON.stringify(obj));
});
it('encodes a top-level typed array as a regular array of numbers', function () {
const arr = new Int32Array([1, 2, 3, 4]);
expect(encode(arr)).toBe('[1,2,3,4]');
});
it('encodes an empty typed array as an empty array', function () {
expect(encode(new Uint8Array())).toBe('[]');
});
it('handles all typed array variants', function () {
expect(encode(new Int8Array([1, -2]))).toBe('[1,-2]');
expect(encode(new Uint8Array([1, 2]))).toBe('[1,2]');
expect(encode(new Uint8ClampedArray([1, 2]))).toBe('[1,2]');
expect(encode(new Int16Array([1, -2]))).toBe('[1,-2]');
expect(encode(new Uint16Array([1, 2]))).toBe('[1,2]');
expect(encode(new Int32Array([1, -2]))).toBe('[1,-2]');
expect(encode(new Uint32Array([1, 2]))).toBe('[1,2]');
expect(encode(new Float32Array([1.5]))).toBe('[1.5]');
expect(encode(new Float64Array([1.5]))).toBe('[1.5]');
});
it('encodes typed arrays nested inside an object', function () {
const obj = {
name: 'data',
values: new Int32Array([10, 20, 30]),
};
expect(encode(obj)).toBe('{"name":"data","values":[10,20,30]}');
});
it('encodes typed arrays nested inside an array', function () {
const arr = [1, new Uint8Array([2, 3]), 4];
expect(encode(arr)).toBe('[1,[2,3],4]');
});
it('encodes deeply nested typed arrays', function () {
const obj = {
a: {
b: {
c: [
{ d: new Float32Array([1.5, 2.5]) },
{ e: new Int16Array([7, 8]) },
],
},
},
};
expect(encode(obj)).toBe('{"a":{"b":{"c":[{"d":[1.5,2.5]},{"e":[7,8]}]}}}');
});
it('does not mutate the original object', function () {
const typedArr = new Int32Array([1, 2, 3]);
const inner = { values: typedArr, label: 'x' };
const obj = { inner, count: 2 };
const originalKeys = Object.keys(obj);
const innerKeys = Object.keys(inner);
encode(obj);
expect(obj.inner).toBe(inner);
expect(inner.values).toBe(typedArr);
expect(inner.label).toBe('x');
expect(obj.count).toBe(2);
expect(Object.keys(obj)).toEqual(originalKeys);
expect(Object.keys(inner)).toEqual(innerKeys);
});
it('does not mutate the original array', function () {
const typedArr = new Int32Array([1, 2, 3]);
const arr = [1, typedArr, 3];
encode(arr);
expect(arr[1]).toBe(typedArr);
expect(arr).toEqual([1, typedArr, 3]);
});
it('leaves DataView alone (encoded as a regular object)', function () {
// DataView is excluded from typed-array handling and falls through
// to the regular object path. JSON.stringify on a DataView produces "{}".
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
expect(encode(view)).toBe(JSON.stringify(view));
});
it('handles null values inside objects and arrays', function () {
const obj = { a: null, b: [null, 1, null], c: new Int32Array([5]) };
expect(encode(obj)).toBe('{"a":null,"b":[null,1,null],"c":[5]}');
});
it('handles an empty object and an empty array', function () {
expect(encode({})).toBe('{}');
expect(encode([])).toBe('[]');
});
it('preserves array element order with typed arrays interleaved', function () {
const arr = [
new Uint8Array([1]),
'middle',
new Uint8Array([2]),
42,
new Uint8Array([3]),
];
expect(encode(arr)).toBe('[[1],"middle",[2],42,[3]]');
});
it('encodes the same shared typed array twice when it appears in two places', function () {
const shared = new Int32Array([7, 8, 9]);
const obj = { first: shared, second: shared };
expect(encode(obj)).toBe('{"first":[7,8,9],"second":[7,8,9]}');
});
});