How To Write A Jasmine Test For Printer Function
I am trying to write a Jasmine test for the following print function: printContent( contentName: string ) { this._console.Information( `${this.codeName}.printContent: ${conte
Solution 1:
You must make sure, window.open()
returns a fully featured object since the printContent
method under test uses properties and functions of windowPrint
. Such an object is typically created with createSpyObj
.
var doc = jasmine.createSpyObj('document', ['write', 'close']);
var windowPrint = jasmine.createSpyObj('windowPrint', ['focus', 'print', 'close']);
windowPrint.document = doc;
spyOn(window, 'open').and.returnValue(windowPrint);
Your amended unit test would then look as follows:
it( 'printContent should open a window ...', () => {
// givenvar doc = jasmine.createSpyObj('document', ['write', 'close']);
var windowPrint = jasmine.createSpyObj('windowPrint', ['focus', 'print', 'close']);
windowPrint.document = doc;
spyOn(window, 'open').and.returnValue(windowPrint);
// when
sut.printContent('printContent');
// thenexpect(window.open).toHaveBeenCalledWith('', '', 'left=0,top=0,width=925,height=1400,toolbar=0,scrollbars=0,status=0');
expect(doc.write).toHaveBeenCalled();
expect(doc.close).toHaveBeenCalled();
expect(windowPrint.focus).toHaveBeenCalled();
expect(windowPrint.print).toHaveBeenCalled();
expect(windowPrint.close).toHaveBeenCalled();
});
Post a Comment for "How To Write A Jasmine Test For Printer Function"