Skip to content

Commit 228a35f

Browse files
authored
Merge pull request #252 from frans-beech-it/242-correctly-conver-dates
Correctly convert dates
2 parents 2985578 + 8a84ac0 commit 228a35f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { DateConverter } from './date.converter';
2+
import { parseISO } from 'date-fns';
3+
4+
describe('Date converter', () => {
5+
const converter: DateConverter = new DateConverter();
6+
7+
describe('mask method', () => {
8+
9+
it('Null stays null', () => {
10+
const value = converter.mask(null);
11+
expect(value).toBeNull();
12+
});
13+
14+
it ( 'string is transformed to Date object', () => {
15+
const value = converter.mask('2019-11-11');
16+
expect(value instanceof Date).toBeTruthy();
17+
});
18+
19+
it ( 'empty string is transformed to Date object', () => {
20+
const value = converter.mask('');
21+
expect(value instanceof Date).toBeTruthy();
22+
});
23+
});
24+
25+
describe('unmask method', () => {
26+
27+
it('Null stays null', () => {
28+
const value = converter.unmask(null);
29+
expect(value).toBeNull();
30+
});
31+
32+
it ( 'Date to be transformed to string', () => {
33+
const value = converter.unmask(parseISO('2019-11-11'));
34+
expect(typeof value === 'string').toBeTruthy();
35+
});
36+
});
37+
38+
});

projects/angular2-jsonapi/src/converters/date/date.converter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export class DateConverter implements PropertyConverter {
1111
}
1212

1313
unmask(value: any) {
14+
if (value === null) {
15+
return null;
16+
}
1417
return value.toISOString();
1518
}
1619
}

0 commit comments

Comments
 (0)