Commit a3d21ada authored by Tifenn Guillas's avatar Tifenn Guillas
Browse files

WIP: add tests

parent bf803465
......@@ -19,8 +19,8 @@
[placeholderMin]="attribute.placeholder_min"
[placeholderMax]="attribute.placeholder_max"
[criterion]="getCriterion(attribute.id)"
(add)="add($event)"
(delete)="delete($event)">
(addCriterion)="add($event)"
(deldeleteCriterionete)="delete($event)">
</app-between>
</div>
<div *ngSwitchCase="'select'">
......@@ -37,7 +37,6 @@
<div *ngSwitchCase="'select-multiple'">
<app-select-multiple class="criteria"
[id]="attribute.id"
[operator]="attribute.operator"
[label]="attribute.form_label"
[options]="getOptions(attribute.id)"
[criterion]="getCriterion(attribute.id)"
......@@ -67,12 +66,11 @@
<div *ngSwitchCase="'checkbox'">
<app-checkbox class="criteria"
[id]="attribute.id"
[operator]="attribute.operator"
[label]="attribute.form_label"
[options]="getOptions(attribute.id)"
[criterion]="getCriterion(attribute.id)"
(add)="add($event)"
(delete)="delete($event)">
(addCriterion)="add($event)"
(deleteCriterion)="delete($event)">
</app-checkbox>
</div>
<div *ngSwitchCase="'date'">
......
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule, FormControl } from '@angular/forms';
import { BetweenComponent } from './between.component';
import { BetweenCriterion } from '../../../store/model';
describe('[Search][Criteria][SearchType] Component: BetweenComponent', () => {
let component: BetweenComponent;
let fixture: ComponentFixture<BetweenComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [BetweenComponent],
imports: [FormsModule, ReactiveFormsModule]
});
fixture = TestBed.createComponent(BetweenComponent);
component = fixture.componentInstance;
});
it('should create the component', () => {
expect(component).toBeTruthy();
});
it('#getDefault() should not fill and enable form if criterion not defined', () => {
const expectedFieldValue = '';
component.getDefault(undefined);
expect(component.fieldMin.value).toEqual(expectedFieldValue);
expect(component.fieldMin.enabled).toBeTruthy();
expect(component.fieldMax.value).toEqual(expectedFieldValue);
expect(component.fieldMax.enabled).toBeTruthy();
});
it('#getDefault() should fill and disable form if criterion is defined', () => {
const min = '10';
const max = '20';
const criterion = new BetweenCriterion(1, min, max);
const expectedFieldMinValue = min;
const expectedFieldMaxValue = max;
component.getDefault(criterion);
expect(component.fieldMin.value).toEqual(expectedFieldMinValue);
expect(component.fieldMin.disabled).toBeTruthy();
expect(component.fieldMax.value).toEqual(expectedFieldMaxValue);
expect(component.fieldMax.disabled).toBeTruthy();
});
it('#getPlaceholderMin() should fill the placeholder if defined', () => {
const placeholder = '10';
component.placeholderMin = placeholder;
expect(component.getPlaceholderMin()).toEqual(placeholder);
});
it('#getPlaceholderMin() should not fill the placeholder if not defined', () => {
expect(component.getPlaceholderMin()).toEqual('');
});
it('#getPlaceholderMax() should fill the placeholder if defined', () => {
const placeholder = '10';
component.placeholderMax = placeholder;
expect(component.getPlaceholderMax()).toEqual(placeholder);
});
it('#getPlaceholderMax() should not fill the placeholder if not defined', () => {
expect(component.getPlaceholderMax()).toEqual('');
});
it('raises the add criterion event when clicked', () => {
component.id = 1;
const min = '10';
const max = '20';
component.fieldMin = new FormControl(min);
component.fieldMax = new FormControl(max);
const expectedCriterion = new BetweenCriterion(component.id, min, max);
component.addCriterion.subscribe((event: BetweenCriterion) => 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();
});
});
......@@ -17,19 +17,19 @@ export class BetweenComponent {
set criterion(criterion: Criterion) {
this.getDefault(criterion);
}
@Output() add: EventEmitter<BetweenCriterion> = new EventEmitter();
@Output() delete: EventEmitter<number> = new EventEmitter();
@Output() addCriterion: EventEmitter<BetweenCriterion> = new EventEmitter();
@Output() deleteCriterion: EventEmitter<number> = new EventEmitter();
fieldMin = new FormControl('');
fieldMax = new FormControl('');
addCriterion() {
emitAdd() {
const fd = new BetweenCriterion(this.id, this.fieldMin.value, this.fieldMax.value);
this.add.emit(fd);
this.addCriterion.emit(fd);
}
deleteCriterion() {
this.delete.emit(this.id);
emitDelete() {
this.deleteCriterion.emit(this.id);
}
getDefault(criterion: Criterion): void {
......
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule, FormControl } from '@angular/forms';
import { CheckboxComponent } from './checkbox.component';
import { SelectMultipleCriterion } from '../../../store/model';
import { Option } from '../../../../metamodel/model';
describe('[Search][Criteria][SearchType] Component: CheckboxComponent', () => {
let component: CheckboxComponent;
let fixture: ComponentFixture<CheckboxComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [CheckboxComponent],
imports: [FormsModule, ReactiveFormsModule]
});
fixture = TestBed.createComponent(CheckboxComponent);
component = fixture.componentInstance;
});
it('should create the component', () => {
expect(component).toBeTruthy();
});
it('#setCheckboxes() should set checkboxes', () => {
const options: Option[] = [
{label: 'One', value: 'one', display: 1},
{label: 'Two', value: 'two', display: 2},
{label: 'Three', value: 'three', display: 3}
];
component.setCheckboxes(options);
// console.log(component.checkboxes.get('0').value);
expect(component.checkboxes.length).toEqual(3);
});
// it('#getDefault() should not fill and enable form if criterion not defined', () => {
// const expectedFieldValue = '';
// component.getDefault(undefined);
// expect(component.fieldMin.value).toEqual(expectedFieldValue);
// expect(component.fieldMin.enabled).toBeTruthy();
// expect(component.fieldMax.value).toEqual(expectedFieldValue);
// expect(component.fieldMax.enabled).toBeTruthy();
// });
// it('#getDefault() should fill and disable form if criterion is defined', () => {
// const min = '10';
// const max = '20';
// const criterion = new BetweenCriterion(1, min, max);
// const expectedFieldMinValue = min;
// const expectedFieldMaxValue = max;
// component.getDefault(criterion);
// expect(component.fieldMin.value).toEqual(expectedFieldMinValue);
// expect(component.fieldMin.disabled).toBeTruthy();
// expect(component.fieldMax.value).toEqual(expectedFieldMaxValue);
// expect(component.fieldMax.disabled).toBeTruthy();
// });
// it('#getPlaceholderMin() should fill the placeholder if defined', () => {
// const placeholder = '10';
// component.placeholderMin = placeholder;
// expect(component.getPlaceholderMin()).toEqual(placeholder);
// });
// it('#getPlaceholderMin() should not fill the placeholder if not defined', () => {
// expect(component.getPlaceholderMin()).toEqual('');
// });
// it('#getPlaceholderMax() should fill the placeholder if defined', () => {
// const placeholder = '10';
// component.placeholderMax = placeholder;
// expect(component.getPlaceholderMax()).toEqual(placeholder);
// });
// it('#getPlaceholderMax() should not fill the placeholder if not defined', () => {
// expect(component.getPlaceholderMax()).toEqual('');
// });
// it('raises the add criterion event when clicked', () => {
// component.id = 1;
// const values: Option[] = [
// {label: 'One', value: 'one', display: 1},
// {label: 'Two', value: 'two', display: 2},
// {label: 'Three', value: 'three', display: 3}
// ];
// const expectedCriterion = new SelectMultipleCriterion(component.id, values);
// component.addCriterion.subscribe((event: SelectMultipleCriterion) => 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();
});
});
......@@ -11,7 +11,6 @@ import { Option } from '../../../../metamodel/model';
})
export class CheckboxComponent {
@Input() id: number;
@Input() operator: string;
@Input() label: string;
@Input()
set options(options: Option[]) {
......@@ -22,26 +21,26 @@ export class CheckboxComponent {
set criterion(criterion: Criterion) {
this.getDefault(criterion);
}
@Output() add: EventEmitter<SelectMultipleCriterion> = new EventEmitter();
@Output() delete: EventEmitter<number> = new EventEmitter();
@Output() addCriterion: EventEmitter<SelectMultipleCriterion> = new EventEmitter();
@Output() deleteCriterion: EventEmitter<number> = new EventEmitter();
checkboxes: FormArray;
checkboxesFormControls: {value: string, control: FormControl}[];
_options: Option[];
addCriterion() {
emitAdd() {
const values: Option[] = [];
this.checkboxes.controls.forEach((formControl, i) => {
if (formControl.value) {
values.push(this._options[i]);
}
});
const cb = new SelectMultipleCriterion(this.id, this.operator, values);
this.add.emit(cb);
const cb = new SelectMultipleCriterion(this.id, values);
this.addCriterion.emit(cb);
}
deleteCriterion() {
this.delete.emit(this.id);
emitDelete() {
this.deleteCriterion.emit(this.id);
}
setCheckboxes(options: Option[]) {
......
......@@ -11,7 +11,6 @@ import { Option } from '../../../../metamodel/model';
})
export class SelectMultipleComponent {
@Input() id: number;
@Input() operator: string;
@Input() label: string;
@Input() options: Option[];
@Input()
......@@ -25,7 +24,7 @@ export class SelectMultipleComponent {
addCriterion() {
const options = this.ms.value.map(optionValue => this.options.find(o => o.value === optionValue));
const ms = new SelectMultipleCriterion(this.id, this.operator, options);
const ms = new SelectMultipleCriterion(this.id, options);
this.add.emit(ms);
}
......
import { FieldCriterion } from './field-criterion.model';
describe('[Search] Model: FieldCriterion', () => {
it('should print criterion correctly', () => {
const value = 'test';
const operator = '=';
const criterion = new FieldCriterion(1, operator, value);
const expectedPrintedCriterion = operator + ' ' + value;
expect(criterion.printCriterion()).toEqual(expectedPrintedCriterion);
});
it('should print criterion url string correctly', () => {
const id = 1;
const value = 'test';
const operator = '=';
const criterion = new FieldCriterion(id, operator, value);
const expectedPrintedCriterion = id + '::' + operator + '::' + value;
expect(criterion.getCriterionStr()).toEqual(expectedPrintedCriterion);
});
});
import { JsonCriterion } from './json-criterion.model';
describe('[Search] Model: JsonCriterion', () => {
it('should print criterion correctly', () => {
const path = 'ext,kw';
const value = 'test';
const operator = '=';
const criterion = new JsonCriterion(1, path, operator, value);
const expectedPrintedCriterion = path + ' ' + operator + ' ' + value;
expect(criterion.printCriterion()).toEqual(expectedPrintedCriterion);
});
it('should print criterion url string correctly', () => {
const id = 1;
const path = 'ext,kw';
const value = 'test';
const operator = '=';
const criterion = new JsonCriterion(id, path, operator, value);
const expectedPrintedCriterion = id + '::js::' + path + '|' + operator + '|' + value;
expect(criterion.getCriterionStr()).toEqual(expectedPrintedCriterion);
});
});
import { SelectMultipleCriterion } from './select-multiple-criterion.model';
import { Option } from '../../../metamodel/model/option.model';
describe('[Search] Model: SelectMultipleCriterion', () => {
it('should print criterion correctly', () => {
const options: Option[] = [
{label: 'One', value: 'one', display: 1},
{label: 'Two', value: 'two', display: 2},
{label: 'Three', value: 'three', display: 3}
];
const criterion = new SelectMultipleCriterion(1, options);
const expectedPrintedCriterion = '[' + options.map(option => option.label).join(',') + ']';
expect(criterion.printCriterion()).toEqual(expectedPrintedCriterion);
});
it('should print criterion url string correctly', () => {
const id = 1;
const options: Option[] = [
{label: 'One', value: 'one', display: 1},
{label: 'Two', value: 'two', display: 2},
{label: 'Three', value: 'three', display: 3}
];
const criterion = new SelectMultipleCriterion(id, options);
const expectedPrintedCriterion = id + '::in::' + options.map(option => option.value).join('|');
expect(criterion.getCriterionStr()).toEqual(expectedPrintedCriterion);
});
});
......@@ -3,11 +3,9 @@ import { Option } from '../../../metamodel/model/option.model';
export class SelectMultipleCriterion extends Criterion {
options: Option[];
operator: string;
constructor(id: number, operator: string, options: Option[]) {
constructor(id: number, options: Option[]) {
super(id);
this.operator = operator;
this.options = options;
}
......
......@@ -107,7 +107,7 @@ export class SearchEffects {
case 'checkbox':
const msValues = params[2].split('|');
const options = attribute.options.filter(option => msValues.includes(option.value));
return new SelectMultipleCriterion(parseInt(params[0], 10), params[1], options);
return new SelectMultipleCriterion(parseInt(params[0], 10), options);
case 'json':
const [path, operator, value] = params[2].split('|');
return new JsonCriterion(parseInt(params[0], 10), path, operator, value);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment