Skip to content

Commit 809cee2

Browse files
committed
feat(job-scheduler): cria evento de erro e sucesso
Cria `output` de sucesso e erro ao realizar um agendamento fixes DTHFUI-8099
1 parent 7fe25b5 commit 809cee2

File tree

3 files changed

+77
-12
lines changed

3 files changed

+77
-12
lines changed

projects/templates/src/lib/components/po-page-job-scheduler/po-page-job-scheduler-base.component.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AbstractControl } from '@angular/forms';
2-
import { Input, Directive, OnDestroy } from '@angular/core';
2+
import { Input, Directive, OnDestroy, Output, EventEmitter } from '@angular/core';
33

44
import { PoBreadcrumb, PoDynamicFormField, PoStepperOrientation } from '@po-ui/ng-components';
55

@@ -219,6 +219,25 @@ export class PoPageJobSchedulerBaseComponent implements OnDestroy {
219219
*/
220220
@Input('p-before-send') beforeSendAction: (model: PoJobSchedulerInternal) => PoJobSchedulerInternal;
221221

222+
/**
223+
* @optional
224+
*
225+
* @description
226+
*
227+
* Evento disparado ao concluir o processo de agendamento com sucesso.
228+
*/
229+
@Output('p-success') success = new EventEmitter<any>();
230+
231+
/**
232+
* @optional
233+
*
234+
* @description
235+
*
236+
* Evento disparado ao ocorrer um erro impossibilitando a conclusão do agendamento.
237+
* Para este evento será passado como parâmetro os detalhes do erro.
238+
*/
239+
@Output('p-error') error = new EventEmitter<any>();
240+
222241
model: PoJobSchedulerInternal = new PoPageJobSchedulerInternal();
223242

224243
private _subscription = new Subscription();

projects/templates/src/lib/components/po-page-job-scheduler/po-page-job-scheduler.component.spec.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
1+
import {
2+
ComponentFixture,
3+
discardPeriodicTasks,
4+
fakeAsync,
5+
flush,
6+
flushMicrotasks,
7+
TestBed,
8+
tick,
9+
waitForAsync
10+
} from '@angular/core/testing';
211
import { RouterTestingModule } from '@angular/router/testing';
312

4-
import { Observable, of } from 'rxjs';
13+
import { delay, Observable, of, throwError } from 'rxjs';
514

615
import { changeBrowserInnerWidth } from './../../util-test/util-expect.spec';
716
import { getObservable } from '../../util-test/util-expect.spec';
@@ -433,17 +442,49 @@ describe('PoPageJobSchedulerComponent:', () => {
433442
});
434443
});
435444

436-
it(`emitSuccessMessage: should call 'poNotification.success' with message and call 'resetJobSchedulerForm'`, async () => {
437-
const message = 'msgSuccess';
445+
it('should emit success event, show notification, and reset form on successful save', fakeAsync(() => {
446+
const model = {
447+
periodicity: 'always',
448+
firstExecution: new Date(),
449+
firstExecutionHour: '23:55:00',
450+
recurrent: true
451+
};
438452

453+
const parameters = [''];
454+
const successSpy = spyOn(component.success, 'emit').and.callThrough();
439455
spyOn(component['poNotification'], 'success');
440456
spyOn(component, <any>'resetJobSchedulerForm');
457+
spyOn(component['poPageJobSchedulerService'], 'createResource').and.returnValue(getObservable(parameters));
441458

442-
await component['emitSuccessMessage'](message, of());
459+
component['save'](model, null);
460+
461+
tick(50);
443462

444-
expect(component['poNotification'].success).toHaveBeenCalledWith(message);
445463
expect(component['resetJobSchedulerForm']).toHaveBeenCalled();
446-
});
464+
expect(component['poNotification'].success).toHaveBeenCalled();
465+
expect(successSpy).toHaveBeenCalled();
466+
467+
discardPeriodicTasks();
468+
}));
469+
470+
it('should emit error if there is an error in the API return', fakeAsync(() => {
471+
const model = {
472+
periodicity: 'always',
473+
firstExecution: new Date(),
474+
firstExecutionHour: '23:55:00',
475+
recurrent: true
476+
};
477+
478+
const errorSpy = spyOn(component.error, 'emit').and.callThrough();
479+
spyOn(component['poPageJobSchedulerService'], 'createResource').and.returnValue(throwError(() => {}));
480+
481+
component['save'](model, null);
482+
483+
tick(50);
484+
expect(errorSpy).toHaveBeenCalled();
485+
486+
discardPeriodicTasks();
487+
}));
447488

448489
it(`getParametersByProcess: should call 'getParametersByProcess' with process and set 'component.parameters'
449490
with 'parameters'`, fakeAsync(() => {

projects/templates/src/lib/components/po-page-job-scheduler/po-page-job-scheduler.component.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,15 @@ export class PoPageJobSchedulerComponent extends PoPageJobSchedulerBaseComponent
222222
});
223223
}
224224

225-
private async emitSuccessMessage(msgSuccess: any, saveOperation: Observable<any>) {
226-
await saveOperation.toPromise();
227-
this.poNotification.success(msgSuccess);
228-
this.resetJobSchedulerForm();
225+
private emitSuccessMessage(msgSuccess: any, saveOperation: Observable<any>) {
226+
saveOperation.subscribe({
227+
next: () => {
228+
this.success.emit();
229+
this.poNotification.success(msgSuccess);
230+
this.resetJobSchedulerForm();
231+
},
232+
error: e => this.error.emit(e)
233+
});
229234
}
230235

231236
private getParametersByProcess(process: any) {

0 commit comments

Comments
 (0)