Skip to content

Commit

Permalink
tests (date value & input): added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelez committed Jan 11, 2021
1 parent efdd557 commit c05e0fd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Expand Up @@ -2,7 +2,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { DateInputComponent } from './date-input.component';
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { FormBuilder, FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { KnoraDate, KnoraPeriod } from '@dasch-swiss/dsp-js';
import { JDNDatepickerDirective } from '../../jdn-datepicker-directive/jdndatepicker.directive';
import { MatFormFieldModule } from '@angular/material/form-field';
Expand Down Expand Up @@ -46,6 +46,35 @@ class TestHostComponent implements OnInit {
}
}

/**
* Test host component to simulate parent component.
*/
@Component({
template: `
<div [formGroup]="form">
<mat-form-field>
<dsp-date-input #dateInput [formControlName]="'date'" [valueRequiredValidator]="false"></dsp-date-input>
</mat-form-field>
</div>`
})
class NoValueRequiredTestHostComponent implements OnInit {

@ViewChild('dateInput') dateInputComponent: DateInputComponent;

form: FormGroup;

constructor(private _fb: FormBuilder) {
}

ngOnInit(): void {

this.form = this._fb.group({
date: new FormControl(null)
});

}
}

describe('DateInputComponent', () => {
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
Expand Down Expand Up @@ -261,4 +290,48 @@ describe('DateInputComponent', () => {

});

it('should mark the form\'s validity correctly', () => {
expect(testHostComponent.dateInputComponent.valueRequiredValidator).toBe(true);
expect(testHostComponent.dateInputComponent.form.valid).toBe(true);

testHostComponent.dateInputComponent.startDateControl.setValue(null);

testHostComponent.dateInputComponent._handleInput();

expect(testHostComponent.dateInputComponent.form.valid).toBe(false);
});

});

describe('NoValueRequiredTestHostComponent', () => {
let testHostComponent: NoValueRequiredTestHostComponent;
let testHostFixture: ComponentFixture<NoValueRequiredTestHostComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
ReactiveFormsModule,
MatFormFieldModule,
MatInputModule,
MatDatepickerModule,
MatCheckboxModule,
MatJDNConvertibleCalendarDateAdapterModule,
BrowserAnimationsModule
],
declarations: [DateInputComponent, NoValueRequiredTestHostComponent, JDNDatepickerDirective]
})
.compileComponents();
}));

beforeEach(() => {
testHostFixture = TestBed.createComponent(NoValueRequiredTestHostComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();

expect(testHostComponent).toBeTruthy();
});

it('should recieve the propagated valueRequiredValidator from the parent component', () => {
expect(testHostComponent.dateInputComponent.valueRequiredValidator).toBe(false);

This comment has been minimized.

Copy link
@tobiasschweizer

tobiasschweizer Jan 11, 2021

Contributor

maybe you could add the same test case as above in "'should mark the form's validity correctly'", however this time it is valid.

});
});
Expand Up @@ -648,7 +648,12 @@ describe('DateValueComponent', () => {
});

it('should not create an empty value', () => {
expect(testHostComponent.inputValueComponent.getNewValue()).toEqual(false);
expect(testHostComponent.inputValueComponent.getNewValue()).toBe(false);
expect(testHostComponent.inputValueComponent.form.valid).toBe(true);
});

it('should propagate valueRequiredValidator to child component', () => {
expect(testHostComponent.inputValueComponent.valueRequiredValidator).toBe(false);
});

});
Expand Down

0 comments on commit c05e0fd

Please sign in to comment.