Skip to content

Commit

Permalink
Merged in DSC-828 (pull request DSpace#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Negretti authored and LucaGiamminonni committed Dec 2, 2022
2 parents a0a7dbd + 6dcada2 commit 730fb8b
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<input required #string="ngModel" type="number" name="number-value-{{index}}" class="form-control" id="number-value-{{index}}" [ngModel]="value" (ngModelChange)="setValue($event)"/>
<div *ngIf="string.invalid && (string.dirty || string.touched)"
class="alert alert-danger validation-error">
<div *ngIf="string.errors.required">
{{'process.new.parameter.number.required' | translate}}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';

import { FormsModule } from '@angular/forms';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { By } from '@angular/platform-browser';
import { NumberValueInputComponent } from './number-value-input.component';
import { TranslateLoaderMock } from '../../../../../shared/mocks/translate-loader.mock';

describe('NumberValueInputComponent', () => {
let component: NumberValueInputComponent;
let fixture: ComponentFixture<NumberValueInputComponent>;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
FormsModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslateLoaderMock
}
})],
declarations: [NumberValueInputComponent],
providers: [
]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(NumberValueInputComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should not show a validation error if the input field was left untouched but left empty', () => {
const validationError = fixture.debugElement.query(By.css('.validation-error'));
expect(validationError).toBeFalsy();
});

it('should show a validation error if the input field was touched but left empty', fakeAsync(() => {
component.value = '';
fixture.detectChanges();
tick();

const input = fixture.debugElement.query(By.css('input'));
input.triggerEventHandler('blur', null);

fixture.detectChanges();

const validationError = fixture.debugElement.query(By.css('.validation-error'));
expect(validationError).toBeTruthy();
}));

it('should not show a validation error if the input field was touched but not left empty', fakeAsync(() => {
component.value = 'testValue';
fixture.detectChanges();
tick();

const input = fixture.debugElement.query(By.css('input'));
input.triggerEventHandler('blur', null);

fixture.detectChanges();

const validationError = fixture.debugElement.query(By.css('.validation-error'));
expect(validationError).toBeFalsy();
}));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Component, Optional, Input } from '@angular/core';
import { ValueInputComponent } from '../value-input.component';
import { ControlContainer, NgForm } from '@angular/forms';
import { controlContainerFactory } from '../../../process-form.component';

/**
* Represents the user inputted value of a numeric parameter
*/
@Component({
selector: 'ds-number-value-input',
templateUrl: './number-value-input.component.html',
styleUrls: ['./number-value-input.component.scss'],
viewProviders: [ { provide: ControlContainer,
useFactory: controlContainerFactory,
deps: [[new Optional(), NgForm]] } ]
})
export class NumberValueInputComponent extends ValueInputComponent<string> {
/**
* The current value of the string
*/
value: string;

/**
* Initial value of the field
*/
@Input() initialValue;

ngOnInit() {
this.value = this.initialValue;
}

setValue(value) {
this.value = value;
this.updateValue.emit(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div [ngSwitch]="parameter?.type">
<ds-string-value-input *ngSwitchCase="parameterTypes.STRING" [initialValue]="initialValue" (updateValue)="updateValue.emit($event)" [index]="index"></ds-string-value-input>
<ds-number-value-input *ngSwitchCase="parameterTypes.NUMBER" [initialValue]="initialValue" (updateValue)="updateValue.emit($event)" [index]="index"></ds-number-value-input>
<ds-string-value-input *ngSwitchCase="parameterTypes.OUTPUT" [initialValue]="initialValue" (updateValue)="updateValue.emit($event)" [index]="index"></ds-string-value-input>
<ds-date-value-input *ngSwitchCase="parameterTypes.DATE" [initialValue]="initialValue" (updateValue)="updateValue.emit($event)" [index]="index"></ds-date-value-input>
<ds-file-value-input *ngSwitchCase="parameterTypes.FILE" (updateValue)="updateValue.emit($event)" [index]="index"></ds-file-value-input>
Expand Down
4 changes: 3 additions & 1 deletion src/app/process-page/process-page.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ProcessDetailFieldComponent } from './detail/process-detail-field/proce
import { ProcessBreadcrumbsService } from './process-breadcrumbs.service';
import { ProcessBreadcrumbResolver } from './process-breadcrumb.resolver';
import { ProcessFormComponent } from './form/process-form.component';
import { NumberValueInputComponent } from './form/process-parameters/parameter-value-input/number-value-input/number-value-input.component';

@NgModule({
imports: [
Expand All @@ -37,7 +38,8 @@ import { ProcessFormComponent } from './form/process-form.component';
ProcessOverviewComponent,
ProcessDetailComponent,
ProcessDetailFieldComponent,
ProcessFormComponent
ProcessFormComponent,
NumberValueInputComponent
],
providers: [
ProcessBreadcrumbResolver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export enum ScriptParameterType {
STRING = 'String',
DATE = 'date',
NUMBER = 'Integer',
BOOLEAN = 'boolean',
FILE = 'InputStream',
OUTPUT = 'OutputStream'
Expand Down

0 comments on commit 730fb8b

Please sign in to comment.