import {JsonTransformerUtil} from './json-transformer.util'; describe('dateTransformer', () => { it('leaves numbers alone', () => { const someNumber = 42; expect(JsonTransformerUtil.transformDates(someNumber)).toBe(42); }); it('leaves booleans alone', () => { const theTruth = true; expect(JsonTransformerUtil.transformDates(theTruth)).toBeTrue(); }); it('handles null and undefined', () => { const totallyUndefined = undefined; const nullValue = null; expect(JsonTransformerUtil.transformDates(totallyUndefined)).toBeUndefined(); expect(JsonTransformerUtil.transformDates(nullValue)).toBeNull(); expect(JsonTransformerUtil.transformDates(nullValue)).toBeNull(); }); it('leaves non-date string alone', () => { const myString = 'I\'m a beautiful string with åll the speciæl charactørs!'; expect(JsonTransformerUtil.transformDates(myString)).toEqual('I\'m a beautiful string with åll the speciæl charactørs!'); }); it('transform a date string', () => { const myString = '2020-11-08T11:06:15.31'; const transformedValue = JsonTransformerUtil.transformDates(myString); expect(transformedValue.getTime).toBeDefined(); expect(transformedValue.toDateString()).toEqual('Sun Nov 08 2020'); }); it('transforms all entries in array', () => { const list = [{a: '2020-11-08T11:06:15.31'}, {q: '2020-11-08T11:06:15.31', a: 'some string that should not be touched'}]; const transformedValue = JsonTransformerUtil.transformDates(list); expect(transformedValue[0].a.toDateString()).toEqual('Sun Nov 08 2020'); expect(transformedValue[1].a).toEqual('some string that should not be touched'); expect(transformedValue[1].q.toDateString()).toEqual('Sun Nov 08 2020'); }); it('transforms all properties in object', () => { const obj = { a: { a: { nestedDate: '2020-11-08T11:06:15.31' }, b: 'should not be changed' }, b: 'should not change', date: '2020-11-08T11:06:15.31' }; const transformedValue = JsonTransformerUtil.transformDates(obj); expect(transformedValue.date.toDateString()).toEqual('Sun Nov 08 2020'); expect(transformedValue.b).toEqual('should not change'); expect(transformedValue.a.a.nestedDate.toDateString()).toEqual('Sun Nov 08 2020'); expect(transformedValue.a.b).toEqual('should not be changed'); }); });