| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import {$objectProperties} from "~/services/utils/objectProperties";
- import {AnyJson} from "~/types/interfaces";
- describe('cloneAndFlatten()', () => {
- it('should throw an error if args is not an object', () =>
- expect(() => $objectProperties.cloneAndFlatten(String as AnyJson)).toThrow()
- );
- it('should return same values for flat objects', () =>
- expect($objectProperties.cloneAndFlatten({ foo: 1, bar: 'baz'})).toStrictEqual({ foo: 1, bar: 'baz'})
- );
- it('should copy null values', () =>
- expect($objectProperties.cloneAndFlatten({ foo: null })).toStrictEqual({ foo: null })
- );
- it('should clone the input', () => {
- let object = { foo: 1, bar: 'baz'};
- let flatObject = $objectProperties.cloneAndFlatten(object);
- expect(flatObject).not.toBe(object)
- flatObject.foo = 2;
- expect(object.foo).toEqual(1)
- });
- it('should flatten nested objects', () =>
- expect($objectProperties.cloneAndFlatten({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } })).toStrictEqual({ a: 1, 'b.c': 2, 'd.e': 3, 'd.f.g': 4, 'd.f.h': 5 })
- );
- it('should not flatten arrays', () =>
- expect($objectProperties.cloneAndFlatten({ a: [1, 2, 3] })).toStrictEqual({ a: [1, 2, 3]})
- );
- it('should not flatten strings', () =>
- expect($objectProperties.cloneAndFlatten({ a: "hello, world" })).toStrictEqual({ a: "hello, world" })
- );
- it('should not flatten dates', () => {
- let d = new Date();
- expect($objectProperties.cloneAndFlatten({ a: d })).toStrictEqual({ a: d })
- });
- it('should not flatten excluded properties', () =>
- expect($objectProperties.cloneAndFlatten({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } }, ['d'])).toStrictEqual({ a: 1, 'b.c': 2, d: { e: 3, f: { g: 4, h: 5 } } })
- );
- });
- describe('cloneAndNest()', () => {
- it('should throw an error if args is not an object', () =>
- expect(() => $objectProperties.cloneAndNest(String as AnyJson)).toThrow()
- );
- it('should return same values for flat objects', () =>
- expect($objectProperties.cloneAndNest({ foo: 1, bar: 'baz'})).toStrictEqual({ foo: 1, bar: 'baz'})
- );
- it('should copy null values', () =>
- expect($objectProperties.cloneAndNest({ foo: null })).toStrictEqual({ foo: null })
- );
- it('should clone the input', () => {
- let object = { foo: 1, bar: 'baz'};
- let nestedObject = $objectProperties.cloneAndNest(object);
- expect(nestedObject).not.toBe(object)
- nestedObject.foo = 2;
- expect(object.foo).toEqual(1)
- });
- it('should nest flattened objects', () =>
- expect($objectProperties.cloneAndNest({ a: 1, 'b.c': 2, 'd.e': 3, 'd.f.g': 4, 'd.f.h': 5 })).toStrictEqual({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } })
- );
- it('should not error on null nested objects', () =>
- expect($objectProperties.cloneAndNest({ a: null, 'a.b': null })).toStrictEqual({ a: null })
- );
- });
|