[ADF-2849] Search Date Range - Set the format of the date from config (#3288)

* format date chosen from the datePicker's calendar

* format date on focusout event

* fix tests & some code refactoring

* more validation messages

* unit tests

* fix typecast error

* move "dateFormat" to be part of the "date range" widget settings block

* fix error on Moment

"...Type 'moment.Moment' is not assignable to type 'moment.Moment'. Two different types with this name exist, but they are unrelated.
          Property 'isLocal' is missing in type 'Moment'..."

* moment - use old version

* change script - use recent version of moment
This commit is contained in:
Suzana Dirla
2018-05-10 16:27:31 +03:00
committed by Eugenio Romano
parent b66154773a
commit 5f004c9972
7 changed files with 157 additions and 17 deletions

View File

@@ -15,26 +15,84 @@
* limitations under the License.
*/
import { SearchDateRangeComponent } from './search-date-range.component';
import moment from 'moment-es6';
import { CustomMomentDateAdapter, SearchDateRangeComponent } from './search-date-range.component';
import { Observable } from 'rxjs/Observable';
declare let moment: any;
describe('SearchDateRangeComponent', () => {
let component: SearchDateRangeComponent;
let fromDate = '2016-10-16';
let toDate = '2017-10-16';
const localeFixture = 'it';
const dateFormatFixture = 'DD-MMM-YY';
const buildAdapter = (): CustomMomentDateAdapter => {
const dateAdapter = new CustomMomentDateAdapter(null);
dateAdapter.customDateFormat = null;
return dateAdapter;
};
const buildUserPreferences = (): any => {
const userPreferences = {
userPreferenceStatus: { LOCALE: localeFixture },
select: (property) => {
return Observable.of(userPreferences.userPreferenceStatus[property]);
}
};
return userPreferences;
};
const theDateAdapter = <any> buildAdapter();
beforeEach(() => {
component = new SearchDateRangeComponent();
component = new SearchDateRangeComponent(theDateAdapter, buildUserPreferences());
});
it('should setup form elements on init', () => {
component.ngOnInit();
expect(component.form).toBeDefined();
expect(component.from).toBeDefined();
expect(component.to).toBeDefined();
expect(component.form).toBeDefined();
});
it('should setup locale from userPreferencesService', () => {
spyOn(component, 'setLocale').and.stub();
component.ngOnInit();
expect(component.setLocale).toHaveBeenCalledWith(localeFixture);
});
it('should setup the format of the date from configuration', () => {
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
component.ngOnInit();
expect(theDateAdapter.customDateFormat).toBe(dateFormatFixture);
});
it('should setup form control with formatted valid date on change', () => {
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
component.ngOnInit();
const inputString = '20.feb.18';
const momentFromInput = moment(inputString, dateFormatFixture);
expect(momentFromInput.isValid()).toBeTruthy();
component.onChangedHandler({ srcElement: { value: inputString }}, component.from);
expect(component.from.value).toEqual(momentFromInput);
});
it('should NOT setup form control with invalid date on change', () => {
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
component.ngOnInit();
const inputString = '20.f.18';
const momentFromInput = moment(inputString, dateFormatFixture);
expect(momentFromInput.isValid()).toBeFalsy();
component.onChangedHandler({ srcElement: { value: inputString }}, component.from);
expect(component.from.value).not.toEqual(momentFromInput);
});
it('should reset form', () => {
component.ngOnInit();
component.form.setValue({ from: fromDate, to: toDate });