diff --git a/tests/json.spec.ts b/tests/json.spec.ts new file mode 100644 index 0000000..54970e2 --- /dev/null +++ b/tests/json.spec.ts @@ -0,0 +1,33 @@ +import {JSONAttemptParse, JSONSanitize, JSONSerialize} from '../src'; + +describe('JSON Utilities', () => { + describe('JSONAttemptParse', () => { + it('parses valid JSON', () => { + expect(JSONAttemptParse('{"a":1}')).toEqual({ a: 1 }); + }); + it('returns original string on error', () => { + expect(JSONAttemptParse('not json')).toBe('not json'); + }); + }); + + describe('JSONSerialize', () => { + it('serializes objects', () => { + expect(JSONSerialize({ a: 1 })).toBe(JSON.stringify({ a: 1 })); + }); + it('leaves primitives as is', () => { + expect(JSONSerialize('test')).toBe('test'); + expect(JSONSerialize(123)).toBe(123); + }); + }); + + describe('JSONSanitize', () => { + it('stringifies objects', () => { + expect(JSONSanitize({ a: 1 })).toBe(JSON.stringify({ a: 1 })); + }); + it('does not throw on circular refs', () => { + const obj: any = {}; + obj.self = obj; + expect(() => JSONSanitize(obj)).not.toThrow(); + }); + }); +}); diff --git a/tests/object.spec.ts b/tests/object.spec.ts index 9fa7159..93a81f4 100644 --- a/tests/object.spec.ts +++ b/tests/object.spec.ts @@ -1,6 +1,5 @@ import { clean, deepCopy, deepMerge, dotNotation, encodeQuery, flattenObj, formData, includes, isEqual, mixin, - JSONAttemptParse, JSONSerialize, JSONSanitize } from '../src'; describe('Object utilities', () => { @@ -131,34 +130,4 @@ describe('Object utilities', () => { expect(c.bar()).toBe(2); }); }); - - describe('JSONAttemptParse', () => { - it('parses valid JSON', () => { - expect(JSONAttemptParse('{"a":1}')).toEqual({ a: 1 }); - }); - it('returns original string on error', () => { - expect(JSONAttemptParse('not json')).toBe('not json'); - }); - }); - - describe('JSONSerialize', () => { - it('serializes objects', () => { - expect(JSONSerialize({ a: 1 })).toBe(JSON.stringify({ a: 1 })); - }); - it('leaves primitives as is', () => { - expect(JSONSerialize('test')).toBe('test'); - expect(JSONSerialize(123)).toBe(123); - }); - }); - - describe('JSONSanitize', () => { - it('stringifies objects', () => { - expect(JSONSanitize({ a: 1 })).toBe(JSON.stringify({ a: 1 })); - }); - it('does not throw on circular refs', () => { - const obj: any = {}; - obj.self = obj; - expect(() => JSONSanitize(obj)).not.toThrow(); - }); - }); });