I need to do a unit test of the promise of html2canvas with Jasmine in an Ionic project that uses Angular.
The html2canvas library is imported like this, directly without the brackets:
import html2canvas from 'html2canvas';
The method with the promise is called in this manner:
html2canvas(DATA).then(canvas => {
// code
}
We have to consider how the method is declared in its definition in the library, in which the method is exported with export default:
declare const html2canvas: (element: HTMLElement, options?: Partial<Options>) => Promise<HTMLCanvasElement>;
export default html2canvas;
How can I spy this function with Jasmine to make the unit test to proceed into the then block? As commented in other solutions, I tried to spy on default like this that but it's not working, it's detecting default as a method and it doesn't exist.
import * as canvas from 'html2canvas';
spyOn(canvas, 'default')
Error: <spyOn> : default() method does not exist
Usage: spyOn(<object>, <methodName>)
I tried other uses of the spyOn function but none of them worked. How I should test this in Jasmine?