-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datepicker): NgbDateNativeUTCAdapter
- Loading branch information
1 parent
a20959c
commit f847791
Showing
4 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/datepicker/adapters/ngb-date-native-utc-adapter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {NgbDateNativeUTCAdapter} from './ngb-date-native-utc-adapter'; | ||
|
||
describe('ngb-date-native-utc model adapter', () => { | ||
let adapter: NgbDateNativeUTCAdapter; | ||
|
||
beforeEach(() => { adapter = new NgbDateNativeUTCAdapter(); }); | ||
|
||
describe('fromModel', () => { | ||
|
||
it('should convert invalid and incomplete values to null', () => { | ||
expect(adapter.fromModel(null)).toBeNull(); | ||
expect(adapter.fromModel(undefined)).toBeNull(); | ||
expect(adapter.fromModel(<any>'')).toBeNull(); | ||
expect(adapter.fromModel(<any>'s')).toBeNull(); | ||
expect(adapter.fromModel(<any>2)).toBeNull(); | ||
expect(adapter.fromModel(<any>{})).toBeNull(); | ||
expect(adapter.fromModel(<any>{year: 2017, month: 10})).toBeNull(); | ||
}); | ||
|
||
it('should convert valid date', | ||
() => { expect(adapter.fromModel(new Date(Date.UTC(2016, 4, 1)))).toEqual({year: 2016, month: 5, day: 1}); }); | ||
}); | ||
|
||
describe('toModel', () => { | ||
|
||
it('should convert invalid and incomplete values to null', () => { | ||
expect(adapter.toModel(null)).toBeNull(); | ||
expect(adapter.toModel(undefined)).toBeNull(); | ||
expect(adapter.toModel(<any>'')).toBeNull(); | ||
expect(adapter.toModel(<any>'s')).toBeNull(); | ||
expect(adapter.toModel(<any>2)).toBeNull(); | ||
expect(adapter.toModel(<any>{})).toBeNull(); | ||
expect(adapter.toModel(<any>new Date())).toBeNull(); | ||
}); | ||
|
||
it('should convert a valid date', | ||
() => { expect(adapter.toModel({year: 2016, month: 10, day: 15})).toEqual(new Date(Date.UTC(2016, 9, 15))); }); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {Injectable} from '@angular/core'; | ||
import {NgbDateAdapter} from './ngb-date-adapter'; | ||
import {NgbDateStruct} from '../ngb-date-struct'; | ||
|
||
@Injectable() | ||
export class NgbDateNativeUTCAdapter extends NgbDateAdapter<Date> { | ||
fromModel(date: Date): NgbDateStruct { | ||
return (date && date.getUTCFullYear) ? | ||
{year: date.getUTCFullYear(), month: date.getUTCMonth() + 1, day: date.getUTCDate()} : | ||
null; | ||
} | ||
|
||
toModel(date: NgbDateStruct): Date { | ||
return date && date.year && date.month ? new Date(Date.UTC(date.year, date.month - 1, date.day)) : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters