diff --git a/projects/angular2-jsonapi/src/converters/date/date.converter.spec.ts b/projects/angular2-jsonapi/src/converters/date/date.converter.spec.ts new file mode 100644 index 00000000..cdfa0718 --- /dev/null +++ b/projects/angular2-jsonapi/src/converters/date/date.converter.spec.ts @@ -0,0 +1,38 @@ +import { DateConverter } from './date.converter'; +import { parseISO } from 'date-fns'; + +describe('Date converter', () => { + const converter: DateConverter = new DateConverter(); + + describe('mask method', () => { + + it('Null stays null', () => { + const value = converter.mask(null); + expect(value).toBeNull(); + }); + + it ( 'string is transformed to Date object', () => { + const value = converter.mask('2019-11-11'); + expect(value instanceof Date).toBeTruthy(); + }); + + it ( 'empty string is transformed to Date object', () => { + const value = converter.mask(''); + expect(value instanceof Date).toBeTruthy(); + }); + }); + + describe('unmask method', () => { + + it('Null stays null', () => { + const value = converter.unmask(null); + expect(value).toBeNull(); + }); + + it ( 'Date to be transformed to string', () => { + const value = converter.unmask(parseISO('2019-11-11')); + expect(typeof value === 'string').toBeTruthy(); + }); + }); + +}); diff --git a/projects/angular2-jsonapi/src/converters/date/date.converter.ts b/projects/angular2-jsonapi/src/converters/date/date.converter.ts index c3d19226..92c23b35 100644 --- a/projects/angular2-jsonapi/src/converters/date/date.converter.ts +++ b/projects/angular2-jsonapi/src/converters/date/date.converter.ts @@ -11,6 +11,9 @@ export class DateConverter implements PropertyConverter { } unmask(value: any) { + if (value === null) { + return null; + } return value.toISOString(); } }