I am new to testing world and I have just started writing unit tests for an existing Angular 2 code. I have a function confirmDelete
which returns Obserable<boolean>
and internally uses ConfirmationService
of PrimeNG to get user's feedback on a popup.
Definition of the function is given below:
confirmDelete(): Observable<boolean> {
let confirmObservable = Observable.create((observer: Observer<boolean>) => {
this.confirmationService.confirm({
header: 'Delete Confirmation',
message: 'Do you really want to delete this record?',
accept: () => {
observer.next(true);
observer.complete();
},
reject: () => {
observer.next(false);
observer.complete();
}
});
});
return confirmObservable;
}
I want to write a unit test for this piece of code. I planned to write a Stub for the ConfirmationService
but because I am new to unit testing world, I am finding it difficult to set up the things.
My question is what is the correct approach to write a unit test in this particular scenario.
EDIT:-
I tried solution proposed by @peeskillet but then I started getting type mismatch error between ConfirmationService
and MockConfirmationService
.
Below are the declarations of ConfirmationService
and Confirmation
classes found in the PrimeNG library.
export interface Confirmation {
message: string;
icon?: string;
header?: string;
accept?: Function;
reject?: Function;
acceptVisible?: boolean;
rejectVisible?: boolean;
acceptEvent?: EventEmitter<any>;
rejectEvent?: EventEmitter<any>;
}
export declare class ConfirmationService {
private requireConfirmationSource;
private acceptConfirmationSource;
requireConfirmation$: Observable<Confirmation>;
accept: Observable<Confirmation>;
confirm(confirmation: Confirmation): this;
onAccept(): void;
}