Skip to content
Snippets Groups Projects
Commit 94ea70e6 authored by Tifenn Guillas's avatar Tifenn Guillas
Browse files

Add tests for datetime componenent

parent f7a8474f
No related branches found
No related tags found
2 merge requests!813.1,!69Tests integration
......@@ -114,8 +114,8 @@
[label]="attribute.form_label"
[operator]="attribute.operator"
[criterion]="getCriterion(attribute.id)"
(add)="add($event)"
(delete)="delete($event)">
(addCriterion)="add($event)"
(deleteCriterion)="delete($event)">
</app-datetime>
</div>
<div *ngSwitchCase="'json'">
......
......@@ -27,11 +27,11 @@
</div>
<div class="col-2 text-center">
<button class="btn btn-outline-success" *ngIf="!date.disabled || !hh.disabled || !mm.disabled"
[hidden]="!isValidFields" (click)="addCriterion()">
[hidden]="!isValidFields" (click)="emitAdd()">
<span class="fas fa-plus fa-fw"></span>
</button>
<button class="btn btn-outline-danger" *ngIf="date.disabled && hh.disabled && mm.disabled"
(click)="deleteCriterion()">
(click)="emitDelete()">
<span class="fa fa-times fa-fw"></span>
</button>
</div>
......
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { BsDatepickerModule, isDateValid } from 'ngx-bootstrap';
import { NgSelectModule } from '@ng-select/ng-select';
import { FormsModule, ReactiveFormsModule, FormControl } from '@angular/forms';
import { DatetimeComponent } from './datetime.component';
import { FieldCriterion } from '../../../store/model';
describe('[Search][Criteria][SearchType] Component: DatetimeComponent', () => {
let component: DatetimeComponent;
let fixture: ComponentFixture<DatetimeComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [DatetimeComponent],
imports: [BsDatepickerModule.forRoot(), NgSelectModule, FormsModule, ReactiveFormsModule]
});
fixture = TestBed.createComponent(DatetimeComponent);
component = fixture.componentInstance;
});
it('should create the component', () => {
expect(component).toBeTruthy();
});
it('#getDefault() should enable and not fill form if criterion not defined', () => {
const expectedFieldValue = '';
component.getDefault(undefined);
expect(component.date.value).toEqual(expectedFieldValue);
expect(component.date.enabled).toBeTruthy();
expect(component.hh.value).toBeNull();
expect(component.hh.enabled).toBeTruthy();
expect(component.mm.value).toBeNull();
expect(component.mm.enabled).toBeTruthy();
expect(component.isValidFields).toBeFalsy();
});
it('#getDefault() should fill and disable form if criterion is defined', () => {
const value = '2019-02-17 15:47';
const criterion = new FieldCriterion(1, '=', value);
const expectedDate = new Date('2019-02-17');
const expectedHour = '15';
const expectedMinute = '47';
component.getDefault(criterion);
expect(component.date.value).toEqual(expectedDate);
expect(component.date.disabled).toBeTruthy();
expect(component.hh.value).toEqual(expectedHour);
expect(component.hh.disabled).toBeTruthy();
expect(component.mm.value).toEqual(expectedMinute);
expect(component.mm.disabled).toBeTruthy();
expect(component.isValidFields).toBeTruthy();
});
it('#initTime(t) should return an array of string with 2 digits from 0 to t', () => {
const n = 10;
expect(component.initTime(n).length).toEqual(n);
expect(component.initTime(n)[5]).toEqual('05');
});
it('#change() should set #isValidFields to false if one or more fields are not defined', () => {
component.change();
expect(component.isValidFields).toBeFalsy();
component.hh = new FormControl('15');
component.change();
expect(component.isValidFields).toBeFalsy();
component.mm = new FormControl('47');
component.change();
expect(component.isValidFields).toBeFalsy();
component.date = new FormControl(new Date ('2019-02-17'));
component.change();
expect(component.isValidFields).toBeTruthy();
});
it('#change() should set #datetime with fields value when defined', () => {
component.date = new FormControl(new Date ('2019-02-17'));
component.hh = new FormControl('15');
component.mm = new FormControl('47');
component.change();
expect(component.datetime).toEqual(new Date ('2019-02-17 15:47'));
});
it('raises the add criterion event when clicked', () => {
component.id = 1;
const operator = '=';
component.operator = operator;
const datetime = new Date ('2019-02-17 15:47');
component.datetime = datetime;
component.hh = new FormControl('15');
component.mm = new FormControl('47');
const expectedCriterion = new FieldCriterion(component.id, operator, '2019-02-17 15:47');
component.addCriterion.subscribe((event: FieldCriterion) => expect(event).toEqual(expectedCriterion));
component.emitAdd();
});
it('raises the delete criterion event when clicked', () => {
component.id = 1;
component.deleteCriterion.subscribe((event: number) => expect(event).toEqual(1));
component.emitDelete();
});
});
......@@ -13,13 +13,12 @@ export class DatetimeComponent {
@Input() id: number;
@Input() operator: string;
@Input() label: string;
@Input() placeholder: string;
@Input()
set criterion(criterion: Criterion) {
this.getDefault(criterion);
}
@Output() add: EventEmitter<FieldCriterion> = new EventEmitter();
@Output() delete: EventEmitter<number> = new EventEmitter();
@Output() addCriterion: EventEmitter<FieldCriterion> = new EventEmitter();
@Output() deleteCriterion: EventEmitter<number> = new EventEmitter();
hours: string[] = [];
minutes: string[] = [];
......@@ -35,17 +34,17 @@ export class DatetimeComponent {
this.minutes = this.initTime(60);
}
addCriterion() {
emitAdd() {
const month = ('0' + (this.datetime.getMonth() + 1)).slice(-2);
const day = ('0' + (this.datetime.getDate())).slice(-2);
const date = this.datetime.getFullYear() + '-' + month + '-' + day;
const time = this.hh.value + ':' + this.mm.value;
const fd = new FieldCriterion(this.id, this.operator, date + ' ' + time);
this.add.emit(fd);
this.addCriterion.emit(fd);
}
deleteCriterion() {
this.delete.emit(this.id);
emitDelete() {
this.deleteCriterion.emit(this.id);
}
getDefault(criterion: Criterion): void {
......@@ -92,6 +91,7 @@ export class DatetimeComponent {
this.datetime.setHours(
this.hh.value,
this.mm.value);
this.datetime.setSeconds(0, 0);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment