2023-03-21 07:34:30 -07:00
|
|
|
import { jsonParse, jsonStringify, deepCopy } from '@/utils';
|
2022-10-24 03:48:16 -07:00
|
|
|
|
|
|
|
describe('jsonParse', () => {
|
|
|
|
it('parses JSON', () => {
|
|
|
|
expect(jsonParse('[1, 2, 3]')).toEqual([1, 2, 3]);
|
|
|
|
expect(jsonParse('{ "a": 1 }')).toEqual({ a: 1 });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('optionally throws `errorMessage', () => {
|
|
|
|
expect(() => {
|
|
|
|
jsonParse('', { errorMessage: 'Invalid JSON' });
|
|
|
|
}).toThrow('Invalid JSON');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('optionally returns a `fallbackValue`', () => {
|
|
|
|
expect(jsonParse('', { fallbackValue: { foo: 'bar' } })).toEqual({ foo: 'bar' });
|
|
|
|
});
|
|
|
|
});
|
2022-10-18 04:33:31 -07:00
|
|
|
|
2023-03-21 07:34:30 -07:00
|
|
|
describe('jsonStringify', () => {
|
2023-03-29 12:10:19 -07:00
|
|
|
const source: any = { a: 1, b: 2, d: new Date(1680089084200), r: new RegExp('^test$', 'ig') };
|
2023-03-21 07:34:30 -07:00
|
|
|
source.c = source;
|
|
|
|
|
|
|
|
it('should throw errors on circular references by default', () => {
|
|
|
|
expect(() => jsonStringify(source)).toThrow('Converting circular structure to JSON');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should break circular references when requested', () => {
|
|
|
|
expect(jsonStringify(source, { replaceCircularRefs: true })).toEqual(
|
2023-03-29 12:10:19 -07:00
|
|
|
'{"a":1,"b":2,"d":"2023-03-29T11:24:44.200Z","r":{},"c":"[Circular Reference]"}',
|
2023-03-21 07:34:30 -07:00
|
|
|
);
|
|
|
|
});
|
2023-03-27 07:22:59 -07:00
|
|
|
|
|
|
|
it('should not detect duplicates as circular references', () => {
|
|
|
|
const y = { z: 5 };
|
|
|
|
const x = [y, y, { y }];
|
|
|
|
expect(jsonStringify(x, { replaceCircularRefs: true })).toEqual(
|
|
|
|
'[{"z":5},{"z":5},{"y":{"z":5}}]',
|
|
|
|
);
|
|
|
|
});
|
2023-03-21 07:34:30 -07:00
|
|
|
});
|
|
|
|
|
2022-10-18 04:33:31 -07:00
|
|
|
describe('deepCopy', () => {
|
|
|
|
it('should deep copy an object', () => {
|
2022-11-02 09:44:12 -07:00
|
|
|
const serializable = {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
toJSON: () => 'x:1,y:2',
|
|
|
|
};
|
2022-10-18 04:33:31 -07:00
|
|
|
const object = {
|
|
|
|
deep: {
|
|
|
|
props: {
|
|
|
|
list: [{ a: 1 }, { b: 2 }, { c: 3 }],
|
|
|
|
},
|
|
|
|
arr: [1, 2, 3],
|
|
|
|
},
|
2022-11-02 09:44:12 -07:00
|
|
|
serializable,
|
2022-10-18 04:33:31 -07:00
|
|
|
arr: [
|
|
|
|
{
|
|
|
|
prop: {
|
|
|
|
list: ['a', 'b', 'c'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
func: () => {},
|
2022-11-02 09:44:12 -07:00
|
|
|
date: new Date(1667389172201),
|
2022-10-18 04:33:31 -07:00
|
|
|
undef: undefined,
|
|
|
|
nil: null,
|
|
|
|
bool: true,
|
|
|
|
num: 1,
|
|
|
|
};
|
|
|
|
const copy = deepCopy(object);
|
|
|
|
expect(copy).not.toBe(object);
|
|
|
|
expect(copy.arr).toEqual(object.arr);
|
|
|
|
expect(copy.arr).not.toBe(object.arr);
|
2022-11-02 09:44:12 -07:00
|
|
|
expect(copy.date).toBe('2022-11-02T11:39:32.201Z');
|
|
|
|
expect(copy.serializable).toBe(serializable.toJSON());
|
2022-10-18 04:33:31 -07:00
|
|
|
expect(copy.deep.props).toEqual(object.deep.props);
|
|
|
|
expect(copy.deep.props).not.toBe(object.deep.props);
|
|
|
|
});
|
2022-10-28 06:25:44 -07:00
|
|
|
|
|
|
|
it('should avoid max call stack in case of circular deps', () => {
|
|
|
|
const object: Record<string, any> = {
|
|
|
|
deep: {
|
|
|
|
props: {
|
|
|
|
list: [{ a: 1 }, { b: 2 }, { c: 3 }],
|
|
|
|
},
|
|
|
|
arr: [1, 2, 3],
|
|
|
|
},
|
|
|
|
arr: [
|
|
|
|
{
|
|
|
|
prop: {
|
|
|
|
list: ['a', 'b', 'c'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
func: () => {},
|
2022-11-02 09:44:12 -07:00
|
|
|
date: new Date(1667389172201),
|
2022-10-28 06:25:44 -07:00
|
|
|
undef: undefined,
|
|
|
|
nil: null,
|
|
|
|
bool: true,
|
|
|
|
num: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
object.circular = object;
|
|
|
|
object.deep.props.circular = object;
|
2022-11-02 09:44:12 -07:00
|
|
|
object.deep.arr.push(object);
|
2022-10-28 06:25:44 -07:00
|
|
|
|
|
|
|
const copy = deepCopy(object);
|
|
|
|
expect(copy).not.toBe(object);
|
|
|
|
expect(copy.arr).toEqual(object.arr);
|
|
|
|
expect(copy.arr).not.toBe(object.arr);
|
2022-11-02 09:44:12 -07:00
|
|
|
expect(copy.date).toBe('2022-11-02T11:39:32.201Z');
|
|
|
|
expect(copy.deep.props.circular).toBe(copy);
|
|
|
|
expect(copy.deep.props.circular).not.toBe(object);
|
|
|
|
expect(copy.deep.arr.slice(-1)[0]).toBe(copy);
|
|
|
|
expect(copy.deep.arr.slice(-1)[0]).not.toBe(object);
|
2022-10-28 06:25:44 -07:00
|
|
|
});
|
2022-10-18 04:33:31 -07:00
|
|
|
});
|