@angular/material/core#MAT_DATE_FORMATS TypeScript Examples
The following examples show how to use
@angular/material/core#MAT_DATE_FORMATS.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: dxc-date-input.module.ts From halstack-angular with Apache License 2.0 | 6 votes |
@NgModule({
declarations: [DxcDateInputComponent],
imports: [
CommonModule,
DxcTextInputModule,
FormsModule,
DxcBoxModule,
MdePopoverModule,
MatDayjsDateModule,
MatDatepickerModule,
],
exports: [DxcDateInputComponent],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
})
export class DxcDateInputModule {}
Example #2
Source File: moment-js-datepicker.component.ts From matx-angular with MIT License | 6 votes |
@Component({
selector: 'app-moment-js-datepicker',
templateUrl: './moment-js-datepicker.component.html',
styleUrls: ['./moment-js-datepicker.component.scss'],
providers: [
// `MomentDateAdapter` and `MAT_MOMENT_DATE_FORMATS` can be automatically provided by importing
// `MatMomentDateModule` in your applications root module. We provide it at the component level
// here, due to limitations of our example generation script.
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS},
],
})
export class MomentJsDatepickerComponent implements OnInit {
// Datepicker takes `Moment` objects instead of `Date` objects.
date = new FormControl(moment([2017, 0, 1]));
constructor() { }
ngOnInit() {
}
}
Example #3
Source File: different-locale-datepicker.component.ts From matx-angular with MIT License | 6 votes |
@Component({
selector: 'app-different-locale-datepicker',
templateUrl: './different-locale-datepicker.component.html',
styleUrls: ['./different-locale-datepicker.component.scss'],
providers: [
// The locale would typically be provided on the root module of your application. We do it at
// the component level here, due to limitations of our example generation script.
{provide: MAT_DATE_LOCALE, useValue: 'ja-JP'},
// `MomentDateAdapter` and `MAT_MOMENT_DATE_FORMATS` can be automatically provided by importing
// `MatMomentDateModule` in your applications root module. We provide it at the component level
// here, due to limitations of our example generation script.
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS},
],
})
export class DifferentLocaleDatepickerComponent {
constructor(private adapter: DateAdapter<any>) {}
french() {
this.adapter.setLocale('fr');
}
}
Example #4
Source File: custom-datepicker.component.ts From matx-angular with MIT License | 6 votes |
@Component({
selector: 'app-custom-datepicker',
templateUrl: './custom-datepicker.component.html',
styleUrls: ['./custom-datepicker.component.scss'],
providers: [
// `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
// application's root module. We provide it at the component level here, due to limitations of
// our example generation script.
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
})
export class CustomDatepickerComponent implements OnInit {
date = new FormControl(moment());
constructor() { }
ngOnInit() {
}
}
Example #5
Source File: year-view.ts From ngx-mat-datetime-picker with MIT License | 6 votes |
constructor(private _changeDetectorRef: ChangeDetectorRef,
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats,
@Optional() public _dateAdapter: NgxMatDateAdapter<D>,
@Optional() private _dir?: Directionality) {
if (!this._dateAdapter) {
throw createMissingDateImplError('NgxMatDateAdapter');
}
if (!this._dateFormats) {
throw createMissingDateImplError('MAT_DATE_FORMATS');
}
this._activeDate = this._dateAdapter.today();
}
Example #6
Source File: month-view.ts From ngx-mat-datetime-picker with MIT License | 6 votes |
constructor(private _changeDetectorRef: ChangeDetectorRef,
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats,
@Optional() public _dateAdapter: NgxMatDateAdapter<D>,
@Optional() private _dir?: Directionality) {
if (!this._dateAdapter) {
throw createMissingDateImplError('NgxMatDateAdapter');
}
if (!this._dateFormats) {
throw createMissingDateImplError('MAT_DATE_FORMATS');
}
this._activeDate = this._dateAdapter.today();
}
Example #7
Source File: datetime-input.ts From ngx-mat-datetime-picker with MIT License | 6 votes |
constructor(
private _elementRef: ElementRef<HTMLInputElement>,
@Optional() public _dateAdapter: NgxMatDateAdapter<D>,
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats,
@Optional() private _formField: MatFormField) {
if (!this._dateAdapter) {
throw createMissingDateImplError('NgxMatDateAdapter');
}
if (!this._dateFormats) {
throw createMissingDateImplError('MAT_DATE_FORMATS');
}
// Update the displayed date when the locale changes.
this._localeSubscription = _dateAdapter.localeChanges.subscribe(() => {
this.value = this.value;
});
}
Example #8
Source File: calendar.ts From ngx-mat-datetime-picker with MIT License | 6 votes |
constructor(_intl: MatDatepickerIntl,
@Optional() private _dateAdapter: NgxMatDateAdapter<D>,
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats,
private _changeDetectorRef: ChangeDetectorRef) {
if (!this._dateAdapter) {
throw createMissingDateImplError('NgxDateAdapter');
}
if (!this._dateFormats) {
throw createMissingDateImplError('MAT_DATE_FORMATS');
}
this._intlChanges = _intl.changes.subscribe(() => {
_changeDetectorRef.markForCheck();
this.stateChanges.next();
});
}
Example #9
Source File: native-date.module.ts From ngx-mat-datetime-picker with MIT License | 5 votes |
@NgModule({
imports: [NgxNativeDateModule],
providers: [{ provide: MAT_DATE_FORMATS, useValue: NGX_MAT_NATIVE_DATE_FORMATS }],
})
export class NgxMatNativeDateModule { }
Example #10
Source File: calendar.ts From ngx-mat-datetime-picker with MIT License | 5 votes |
constructor(private _intl: MatDatepickerIntl,
@Inject(forwardRef(() => NgxMatCalendar)) public calendar: NgxMatCalendar<D>,
@Optional() private _dateAdapter: NgxMatDateAdapter<D>,
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats,
changeDetectorRef: ChangeDetectorRef) {
this.calendar.stateChanges.subscribe(() => changeDetectorRef.markForCheck());
}
Example #11
Source File: team-reports-search-filter.component.ts From fyle-mobile-app with MIT License | 5 votes |
@Component({
selector: 'app-team-reports-search-filter',
templateUrl: './team-reports-search-filter.component.html',
styleUrls: ['./team-reports-search-filter.component.scss'],
providers: [
{ provide: DateAdapter, useClass: AppDateAdapter },
{ provide: MAT_DATE_FORMATS, useValue: APP_DATE_FORMATS },
],
})
export class TeamReportsSearchFilterComponent implements OnInit {
@Input() filters: Partial<{
state: string;
date: string;
customDateStart: Date;
customDateEnd: Date;
sortParam: string;
sortDir: string;
}>;
fg: FormGroup;
constructor(private fb: FormBuilder, private popoverController: PopoverController) {}
ngOnInit() {
this.fg = this.fb.group({
state: [this.filters && this.filters.state],
date: [this.filters && this.filters.date],
customDateStart: [this.filters && this.filters.customDateStart],
customDateEnd: [this.filters && this.filters.customDateEnd],
});
this.fg.validator = this.customDatevalidator;
}
customDatevalidator(formGroup: FormGroup) {
if (
formGroup.value.date &&
formGroup.value.date === 'CUSTOMDATE' &&
(formGroup.controls.customDateStart.value === null || formGroup.controls.customDateEnd.value === null)
) {
return {
error: 'custom date input is required when custom dates are selected',
};
}
}
save() {
if (this.fg.value.date !== 'CUSTOMDATE') {
this.fg.controls.customDateStart.reset();
this.fg.controls.customDateEnd.reset();
}
this.popoverController.dismiss({
filters: this.fg.value,
});
}
cancel() {
this.popoverController.dismiss();
}
clearAll() {
this.fg.reset();
}
}
Example #12
Source File: moment-adapter.module.ts From ngx-mat-datetime-picker with MIT License | 5 votes |
@NgModule({
imports: [NgxMomentDateModule],
providers: [{ provide: MAT_DATE_FORMATS, useValue: NGX_MAT_MOMENT_FORMATS }],
})
export class NgxMatMomentModule { }
Example #13
Source File: ngx-mat-datefns-date-adapter.module.ts From ngx-mat-datefns-date-adapter with MIT License | 5 votes |
@NgModule({
imports: [NgxDateFnsDateModule],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: NGX_MAT_DATEFNS_DATE_FORMATS },
{ provide: NGX_MAT_DATEFNS_LOCALES, useValue: [] },
],
})
export class NgxMatDateFnsDateModule {}
Example #14
Source File: my-reports-search-filter.component.ts From fyle-mobile-app with MIT License | 5 votes |
@Component({
selector: 'app-my-reports-search-filter',
templateUrl: './my-reports-search-filter.component.html',
styleUrls: ['./my-reports-search-filter.component.scss'],
providers: [
{ provide: DateAdapter, useClass: AppDateAdapter },
{ provide: MAT_DATE_FORMATS, useValue: APP_DATE_FORMATS },
],
})
export class MyReportsSearchFilterComponent implements OnInit {
@Input() filters: Partial<{
state: string;
date: string;
customDateStart: Date;
customDateEnd: Date;
sortParam: string;
sortDir: string;
}>;
fg: FormGroup;
constructor(private fb: FormBuilder, private popoverController: PopoverController) {}
ngOnInit() {
this.fg = this.fb.group({
state: [this.filters && this.filters.state],
date: [this.filters && this.filters.date],
customDateStart: [this.filters && this.filters.customDateStart],
customDateEnd: [this.filters && this.filters.customDateEnd],
});
this.fg.validator = this.customDatevalidator;
}
customDatevalidator(formGroup: FormGroup) {
if (
formGroup.value.date &&
formGroup.value.date === 'CUSTOMDATE' &&
(formGroup.controls.customDateStart.value === null || formGroup.controls.customDateEnd.value === null)
) {
return {
error: 'custom date input is required when custom dates are selected',
};
}
}
save() {
if (this.fg.value.date !== 'CUSTOMDATE') {
this.fg.controls.customDateStart.reset();
this.fg.controls.customDateEnd.reset();
}
this.popoverController.dismiss({
filters: this.fg.value,
});
}
cancel() {
this.popoverController.dismiss();
}
clearAll() {
this.fg.reset();
}
}
Example #15
Source File: corporate-card-expenses-search-filter.component.ts From fyle-mobile-app with MIT License | 5 votes |
@Component({
selector: 'app-corporate-card-expenses-search-filter',
templateUrl: './corporate-card-expenses-search-filter.component.html',
styleUrls: ['./corporate-card-expenses-search-filter.component.scss'],
providers: [
{ provide: DateAdapter, useClass: AppDateAdapter },
{ provide: MAT_DATE_FORMATS, useValue: APP_DATE_FORMATS },
],
})
export class CorporateCardExpensesSearchFilterComponent implements OnInit {
@Input() filters: Partial<{
date: string;
customDateStart: Date;
customDateEnd: Date;
sortParam: string;
sortDir: string;
}>;
fg: FormGroup;
constructor(private fb: FormBuilder, private popoverController: PopoverController) {}
ngOnInit() {
this.fg = this.fb.group({
date: [this.filters && this.filters.date],
customDateStart: [this.filters && this.filters.customDateStart],
customDateEnd: [this.filters && this.filters.customDateEnd],
});
this.fg.validator = this.customDatevalidator;
}
customDatevalidator(formGroup: FormGroup) {
if (
formGroup.value.date &&
formGroup.value.date === 'CUSTOMDATE' &&
(formGroup.controls.customDateStart.value === null || formGroup.controls.customDateEnd.value === null)
) {
return {
error: 'custom date input is required when custom dates are selected',
};
}
}
save() {
if (this.fg.value.date !== 'CUSTOMDATE') {
this.fg.controls.customDateStart.reset();
this.fg.controls.customDateEnd.reset();
}
this.popoverController.dismiss({
filters: this.fg.value,
});
}
cancel() {
this.popoverController.dismiss();
}
clearAll() {
this.fg.reset();
}
}
Example #16
Source File: wizard-field.component.ts From geonetwork-ui with GNU General Public License v2.0 | 4 votes |
@Component({
selector: 'gn-ui-wizard-field',
templateUrl: './wizard-field.component.html',
styleUrls: ['./wizard-field.component.css'],
providers: [
{ provide: MAT_DATE_LOCALE, useValue: getLangFromBrowser() },
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS],
},
{ provide: MAT_DATE_FORMATS, useValue: MY_FORMATS },
],
})
export class WizardFieldComponent implements AfterViewInit, OnDestroy {
@Input() wizardFieldConfig: WizardFieldModel
@ViewChild('searchText') searchText: TextInputComponent
@ViewChild('chips') chips: ChipsInputComponent
@ViewChild('textArea') textArea: TextAreaComponent
@ViewChild('dropdown') dropdown: DropdownSelectorComponent
subs = new Subscription()
get wizardFieldType(): typeof WizardFieldType {
return WizardFieldType
}
get dropdownChoices(): any {
return this.wizardFieldConfig.options
}
get wizardFieldData() {
const data = this.wizardService.getWizardFieldData(
this.wizardFieldConfig.id
)
switch (this.wizardFieldConfig.type) {
case WizardFieldType.TEXT: {
return data || ''
}
case WizardFieldType.CHIPS: {
return data ? JSON.parse(data) : []
}
case WizardFieldType.TEXT_AREA: {
return data || ''
}
case WizardFieldType.DATA_PICKER: {
return data ? new Date(Number(data)) : new Date()
}
case WizardFieldType.DROPDOWN: {
return data ? JSON.parse(data) : this.dropdownChoices[1]
}
}
}
constructor(private wizardService: WizardService) {}
ngAfterViewInit() {
this.initializeListeners()
}
ngOnDestroy() {
this.subs.unsubscribe()
}
private initializeListeners() {
switch (this.wizardFieldConfig.type) {
case WizardFieldType.TEXT: {
this.initializeTextInputListener()
break
}
case WizardFieldType.CHIPS: {
this.initializeChipsListener()
break
}
case WizardFieldType.TEXT_AREA: {
this.initializeTextAreaListener()
return
}
case WizardFieldType.DATA_PICKER: {
this.initializeDateInput()
return
}
case WizardFieldType.DROPDOWN: {
this.initializeDropdownListener()
return
}
}
}
private initializeTextInputListener() {
this.subs.add(
this.searchText.valueChange.subscribe((value) => {
this.wizardService.onWizardWizardFieldDataChanged(
this.wizardFieldConfig.id,
value
)
})
)
}
private initializeChipsListener() {
this.subs.add(
this.chips.itemsChange.subscribe((items) => {
this.wizardService.onWizardWizardFieldDataChanged(
this.wizardFieldConfig.id,
JSON.stringify(items)
)
})
)
}
private initializeTextAreaListener() {
this.subs.add(
this.textArea.valueChange.subscribe((value) => {
this.wizardService.onWizardWizardFieldDataChanged(
this.wizardFieldConfig.id,
value
)
})
)
}
initializeDateInput() {
const time = this.wizardService.getWizardFieldData(
this.wizardFieldConfig.id
)
if (!time) {
this.wizardService.onWizardWizardFieldDataChanged(
this.wizardFieldConfig.id,
new Date().valueOf()
)
}
}
onDateChange(event: MatDatepickerInputEvent<Date>) {
this.wizardService.onWizardWizardFieldDataChanged(
this.wizardFieldConfig.id,
event.value.valueOf()
)
}
private initializeDropdownListener() {
this.subs.add(
this.dropdown.selectValue.subscribe((value) => {
this.wizardService.onWizardWizardFieldDataChanged(
this.wizardFieldConfig.id,
value
)
})
)
}
}
Example #17
Source File: dxc-date-input.component.spec.ts From halstack-angular with Apache License 2.0 | 4 votes |
describe("DxcDate", () => {
const newMockDate = new Date("1995/12/03");
const newValue = "03-12-1995";
test("should render dxc-date", async () => {
const { getByText } = await render(DxcDateInputComponent, {
componentProperties: { label: "test-date" },
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
expect(getByText("test-date"));
});
test("The input´s value is the same as the one received as a parameter", async () => {
const onChange = jest.fn();
const onBlur = jest.fn();
await render(DxcDateInputComponent, {
componentProperties: {
label: "test-input",
value: "03-12-1995",
onChange: {
emit: onChange,
} as any,
onBlur: {
emit: onBlur,
} as any,
},
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
const input = <HTMLInputElement>screen.getByRole("textbox");
const calendarIcon = screen.getByRole("calendarIcon");
input.focus();
expect(screen.getByDisplayValue("03-12-1995"));
fireEvent.click(calendarIcon);
expect(screen.getByText("DECEMBER 1995"));
expect(
screen.getByText("3").classList.contains("mat-calendar-body-selected")
).toBeTruthy();
});
test("dxc-date value change and default format", async () => {
const onChange = jest.fn();
await render(DxcDateInputComponent, {
componentProperties: {
label: "test-input",
onChange: {
emit: onChange,
} as any,
},
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
const input = <HTMLInputElement>screen.getByRole("textbox");
input.focus();
fireEvent.input(input, { target: { value: newValue } });
expect(onChange).toHaveBeenCalledWith({
value: newValue,
error: undefined,
date: newMockDate,
});
expect(screen.getByDisplayValue(newValue));
});
test("dxc-date change value twice as uncontrolled", async () => {
const onChange = jest.fn();
await render(DxcDateInputComponent, {
componentProperties: {
label: "test-input",
defaultValue: "22-10-1998",
onChange: {
emit: onChange,
} as any,
},
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
expect(screen.getByDisplayValue("22-10-1998"));
const input = <HTMLInputElement>screen.getByRole("textbox");
input.focus();
fireEvent.input(input, { target: { value: newValue } });
expect(onChange).toHaveBeenCalledWith({
value: newValue,
error: undefined,
date: newMockDate,
});
expect(screen.getByDisplayValue(newValue));
input.focus();
fireEvent.input(input, { target: { value: "04-10-1996" } });
expect(onChange).toHaveBeenCalledWith({
value: "04-10-1996",
error: undefined,
date: new Date("1996/10/04"),
});
expect(screen.getByDisplayValue("04-10-1996"));
});
test("Calendar´s value is the same as the input´s date if it´s right (Depending on the format)", async () => {
const onChange = jest.fn();
const onBlur = jest.fn();
await render(DxcDateInputComponent, {
componentProperties: {
label: "test-input",
format: "YYYY/MM/DD",
onChange: {
emit: onChange,
} as any,
onBlur: {
emit: onBlur,
} as any,
},
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
const input = <HTMLInputElement>screen.getByRole("textbox");
const calendarIcon = screen.getByRole("calendarIcon");
input.focus();
fireEvent.input(input, { target: { value: "1995/12/03" } });
expect(onChange).toHaveBeenCalledWith({
value: "1995/12/03",
error: undefined,
date: newMockDate,
});
input.focus();
expect(screen.getByDisplayValue("1995/12/03"));
fireEvent.click(calendarIcon);
waitFor(() => {
expect(screen.getByText("DECEMBER 1995"));
});
waitFor(() => {
expect(
screen.getByText("3").classList.contains("mat-calendar-body-selected")
).toBeTruthy();
});
});
test("dxc-date invalid value", async () => {
const onChange = jest.fn();
const invalidValue = "03-12-199_";
await render(DxcDateInputComponent, {
componentProperties: {
label: "test-input",
onChange: {
emit: onChange,
} as any,
},
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
const input = <HTMLInputElement>screen.getByRole("textbox");
input.focus();
fireEvent.input(input, { target: { value: invalidValue } });
expect(onChange).toHaveBeenCalledWith({
value: invalidValue,
error: undefined,
date: undefined,
});
});
test("onChange function is called when the user selects from the calendar", async () => {
const onChange = jest.fn();
await render(DxcDateInputComponent, {
componentProperties: {
label: "test-input",
value: newValue,
onChange: {
emit: onChange,
} as any,
},
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
const calendarIcon = screen.getByRole("calendarIcon");
fireEvent.click(calendarIcon);
fireEvent.click(screen.getByText("4"));
waitFor(() => {
expect(onChange).toHaveBeenCalledWith({
value: "04-12-1995",
date: new Date("04/12/1995"),
});
});
});
test("onChange function is called when the user selects from the calendar, the stringValue received by the function is the date with the correct format", async () => {
const onChange = jest.fn();
await render(DxcDateInputComponent, {
componentProperties: {
label: "test-input",
value: "12-03-1995",
format: "MM-DD-YYYY",
onChange: {
emit: onChange,
} as any,
},
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
const calendarIcon = screen.getByRole("calendarIcon");
fireEvent.click(calendarIcon);
expect(screen.getByText("DECEMBER 1995"));
expect(
screen.getByText("3").classList.contains("mat-calendar-body-selected")
).toBeTruthy();
fireEvent.click(screen.getByText("4"));
waitFor(() => {
expect(onChange).toHaveBeenCalledWith({
value: "12-04-1995",
date: new Date("04/12/1995"),
});
});
});
test("If the user types something, if controlled and without onChange, the value doesn´t change", async () => {
const onChange = jest.fn();
await render(DxcDateInputComponent, {
componentProperties: {
label: "test-input",
value: newValue,
onChange: {
emit: onChange,
} as any,
},
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
const calendarIcon = screen.getByRole("calendarIcon");
fireEvent.click(calendarIcon);
expect(screen.getByText("DECEMBER 1995"));
expect(
screen.getByText("3").classList.contains("mat-calendar-body-selected")
).toBeTruthy();
fireEvent.click(screen.getByText("4"));
waitFor(() => {
expect(onChange).toHaveBeenCalledWith({
value: "04-12-1995",
date: new Date("04/12/1995"),
});
});
fireEvent.click(calendarIcon);
expect(screen.getByText("DECEMBER 1995"));
waitFor(() => {
expect(
screen.getByText("3").classList.contains("mat-calendar-body-selected")
).toBeTruthy();
});
});
test("controlled dxc-date", async () => {
const onChange = jest.fn();
const onBlur = jest.fn();
await render(DxcDateInputComponent, {
componentProperties: {
label: "test-input",
value: "03-12-1995",
defaultValue: "12-04-1995",
onChange: {
emit: onChange,
} as any,
onBlur: {
emit: onBlur,
} as any,
},
imports: [
MatDayjsDateModule,
MatNativeDateModule,
MatInputModule,
MatDatepickerModule,
MdePopoverModule,
DxcBoxModule,
CommonModule,
DxcTextInputModule,
],
providers: [
{ provide: MAT_DATE_FORMATS, useValue: DAYJS_DATE_FORMATS },
{
provide: DateAdapter,
useClass: DayjsDateAdapter,
deps: [MAT_DATE_LOCALE, Platform],
},
],
});
expect(screen.getByDisplayValue("03-12-1995"));
const input = <HTMLInputElement>screen.getByRole("textbox");
input.focus();
fireEvent.input(input, { target: { value: "03-10-1996" } });
expect(onChange).toHaveBeenCalledWith({
value: "03-10-1996",
error: undefined,
date: new Date("1996/10/03"),
});
expect(screen.getByDisplayValue("03-12-1995"));
fireEvent.blur(input);
waitFor(() => {
expect(onBlur).toHaveBeenCalledWith({
error: undefined,
value: "03-12-1995",
date: new Date("1995/12/03"),
});
});
});
});