blob: e7415676521c90292452728267309c6b1c07ea0b [file]
/********************************************************************************
* Copyright © 2018 Mettenmeier GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import { FormControl, Validators, FormGroup, FormArray } from '@angular/forms';
import { FormUtil } from '@shared/utils/form.util';
describe('PicklistComponent', () => {
it('should validatedAllFormFields', () => {
const formGroup: FormGroup = new FormGroup({
functionName: new FormControl('', Validators.required),
lsDistrict: new FormControl('')
});
FormUtil.validate(formGroup);
// assert that errors exist
expect(formGroup.controls.functionName.errors).toBeTruthy();
});
it('should validate sub FormGroups', () => {
const formGroup: FormGroup = new FormGroup({
functionName: new FormControl('', Validators.required),
subFormGroup: new FormGroup({
id: new FormControl('')
})
});
FormUtil.validate(formGroup);
// assert that errors exist
expect(formGroup.controls.functionName.errors).toBeTruthy();
});
it('should validate all FormGroups', () => {
const formGroup: FormGroup = new FormGroup({
functionName: new FormControl('', Validators.required),
subFormGroup: new FormGroup({
id: new FormControl('')
})
});
FormUtil.validate(formGroup);
// assert that errors exist
expect(formGroup.controls.functionName.errors).toBeTruthy();
});
it('shouldn´t validate all FormArrays', () => {
const formGroup: FormGroup = new FormGroup({
functionName: new FormControl('', Validators.required),
subFormArray: new FormArray([])
});
FormUtil.validate(formGroup);
// assert that errors exist
expect(formGroup.controls.functionName.errors).toBeTruthy();
});
it('should mark the formgroup as pristine', () => {
const formGroup: FormGroup = new FormGroup({
id: new FormControl(''),
functionName: new FormControl('', Validators.required)
});
formGroup.markAsTouched();
FormUtil.markAsPristineAndUntouched(formGroup);
expect(formGroup.touched).toBeFalsy();
});
it('should mark the formgroup as pristine', () => {
const formGroup: FormGroup = new FormGroup({
id: new FormControl(''),
functionName: new FormControl('', Validators.required)
});
formGroup.markAsDirty();
FormUtil.markAsPristineAndUntouched(formGroup);
expect(formGroup.pristine).toBeTruthy();
});
it('should add 5 days to 31.12.2018 and expect 04.01.2019', () => {
const formGroup: FormGroup = new FormGroup({
functionName: new FormControl('', Validators.required),
validTo: new FormControl('')
});
const date = new Date('2018-12-31');
FormUtil.setInitialDate(formGroup);
FormUtil.setDefaultReportDate(formGroup, 'validTo', 5, date);
expect(formGroup.controls.validTo.value).toEqual({ day: 5, month: 1, year: 2019 });
});
it('should patch validFrom and validTo dates to 01.01.20xx and 31.12.20xx', () => {
const formGroup: FormGroup = new FormGroup({
functionName: new FormControl('', Validators.required),
validFrom: new FormControl(''),
validTo: new FormControl('')
});
const date = new Date();
FormUtil.setInitialDate(formGroup);
expect(formGroup.controls.validFrom.value).toEqual({ day: 1, month: 1, year: date.getFullYear() + 1 });
expect(formGroup.controls.validTo.value).toEqual({ day: 31, month: 12, year: date.getFullYear() + 1 });
});
it('should patch validTo date 31.12.20xx', () => {
const formGroup: FormGroup = new FormGroup({
functionName: new FormControl('', Validators.required),
validFrom: new FormControl(''),
validTo: new FormControl('')
});
const date = new Date();
FormUtil.setDefaultEndOfNextYear(formGroup, 'validTo');
expect(formGroup.controls.validTo.value).toEqual({ day: 31, month: 12, year: date.getFullYear() + 1 });
});
describe('setDefaultDate', () => {
it('should set validFrom and date values to the current date and validTo to current + 15 years', () => {
const formGroup: FormGroup = new FormGroup({
functionName: new FormControl('', Validators.required),
validFrom: new FormControl(''),
validTo: new FormControl(''),
date: new FormControl('')
});
const date = new Date();
FormUtil.setDefaultDate(formGroup, 'validFrom');
FormUtil.setDefaultDate(formGroup, 'validTo');
FormUtil.setDefaultDate(formGroup, 'date');
expect(formGroup.controls.validFrom.value).toEqual({
day: date.getDate(), month: date.getMonth() + 1, year: date.getFullYear()
});
expect(formGroup.controls.validTo.value).toEqual({
day: date.getDate(), month: date.getMonth() + 1, year: date.getFullYear() + 15
});
expect(formGroup.controls.date.value).toEqual({
day: date.getDate(), month: date.getMonth() + 1, year: date.getFullYear()
});
});
});
describe('addZero()', () => {
it('should add a leading zero for number 2', () => {
expect(FormUtil.addZero(2)).toBe('02');
});
it('should add a leading zero for number 9', () => {
expect(FormUtil.addZero(9)).toBe('09');
});
it('shouldn´t add a leading zero to number 20', () => {
expect(FormUtil.addZero(20)).toBe('20');
});
it('should cut off the first two numbers', () => {
expect(FormUtil.addZero(1234)).toBe('34');
});
});
});