• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {
16  deserializeStateObject,
17  isSerializedBigint,
18  serializeStateObject,
19} from './upload_utils';
20
21describe('isSerializedBigint', () => {
22  it('should return true for a valid serialized bigint', () => {
23    const value = {
24      __kind: 'bigint',
25      value: '1234567890',
26    };
27    expect(isSerializedBigint(value)).toBeTruthy();
28  });
29
30  it('should return false for a null value', () => {
31    expect(isSerializedBigint(null)).toBeFalsy();
32  });
33
34  it('should return false for a non-object value', () => {
35    expect(isSerializedBigint(123)).toBeFalsy();
36  });
37
38  it('should return false for a non-serialized bigint value', () => {
39    const value = {
40      __kind: 'not-bigint',
41      value: '1234567890',
42    };
43    expect(isSerializedBigint(value)).toBeFalsy();
44  });
45});
46
47describe('serializeStateObject', () => {
48  it('should serialize a simple object', () => {
49    const object = {
50      a: 1,
51      b: 2,
52      c: 3,
53    };
54    const expectedJson = `{"a":1,"b":2,"c":3}`;
55    expect(serializeStateObject(object)).toEqual(expectedJson);
56  });
57
58  it('should serialize a bigint', () => {
59    const object = {
60      a: 123456789123456789n,
61    };
62    const expectedJson = `{"a":{"__kind":"bigint","value":"123456789123456789"}}`;
63    expect(serializeStateObject(object)).toEqual(expectedJson);
64  });
65
66  it('should not serialize a non-serializable property', () => {
67    const object = {
68      a: 1,
69      b: 2,
70      c: 3,
71      nonSerializableState: 4,
72    };
73    const expectedJson = `{"a":1,"b":2,"c":3}`;
74    expect(serializeStateObject(object)).toEqual(expectedJson);
75  });
76});
77
78describe('deserializeStateObject', () => {
79  it('should deserialize a simple object', () => {
80    const json = `{"a":1,"b":2,"c":3}`;
81    const expectedObject = {
82      a: 1,
83      b: 2,
84      c: 3,
85    };
86    expect(deserializeStateObject(json)).toEqual(expectedObject);
87  });
88
89  it('should deserialize a bigint', () => {
90    const json = `{"a":{"__kind":"bigint","value":"123456789123456789"}}`;
91    const expectedObject = {
92      a: 123456789123456789n,
93    };
94    expect(deserializeStateObject(json)).toEqual(expectedObject);
95  });
96
97  it('should deserialize a null', () => {
98    const json = `{"a":null}`;
99    const expectedObject = {
100      a: null,
101    };
102    expect(deserializeStateObject(json)).toEqual(expectedObject);
103  });
104});
105