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&l
I've built off of peeskillet's answer. The p-confirmDialog component calls the subscribe of the service, so I had to added requireConfirmationSource, which I found in the PrimeNg source. My mock is as follows:
//
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
//
export class ConfirmationServiceMock {
public key: string = 'mock1';
public header: string = 'Delete Confirmation';
public icon: string = '';
public message: string = '';
// Call Accept to emulate a user accepting
public accept: Function;
// Call Reject to emulate a user rejecting
public reject: Function;
private requireConfirmationSource = new Subject<any>();
requireConfirmation$ = this.requireConfirmationSource.asObservable();
//
public confirm(config: any) {
console.log( 'In confirm service mock...' );
this.message = config.message;
this.accept = config.accept;
this.reject = config.reject;
console.log( this.message );
return this;
}
}
//
I would probably make the mock hold on to the references of the accept
and reject
functions. Then in the test you can call them to check that they emit the correct boolean value. Something like
class MockConfirmationService {
accept: Function;
reject: Function;
confirm(config: any) {
this.accept = config.accept;
this.reject = config.reject;
}
}
Then in your test just call the accept
to test that true
is emitted, and call reject
to test that false
is emitted.
describe('serivce: ModalService', () => {
let modalService: ModalService;
let confirmService: MockConfirmationService;
beforeEach(() => {
confirmService = new MockConfirmationService();
TestBed.configureTestingModule({
providers: [
ModalService,
{ provide: ConfirmationService, useValue: confirmService }
]
});
modalService = TestBed.get(ModalService);
});
it('should return true when accepted', async(() => {
modalService.confirmDelete().subscribe(result => {
expect(result).toBe(true);
console.log('accepted test complete');
});
confirmService.accept();
}));
it('should return false when rejected', async(() => {
modalService.confirmDelete().subscribe(result => {
expect(result).toBe(false);
console.log('rejected test complete');
});
confirmService.reject();
}));
});