Tengo una función de terceros, regreso de una función principal, puedo probar la llamada a la función principal, toHaveBeenCalled pero la declaración de devolución anidada no se está probando. Definitivamente necesita burlarse ya que es una función de terceros. La función se ve a continuación:
import * as _html2canvas from 'html2canvas'; // tslint:disable-next-line: no-any const html2canvas: any = _html2canvas; export const downloadElement = (props: string, imageName: string): HTMLElement => { const captureElement = document.querySelector(props); const canvasBg = window.getComputedStyle(document.body, null).getPropertyValue('background-color'); const options = { scale: 0.8, backgroundColor: canvasBg, removeContainer: true, allowTaint: true }; return html2canvas(captureElement, options) .then((canvas: HTMLCanvasElement) => { canvas.style.display = 'none'; document.body.appendChild(canvas); return canvas; }) .then((canvas: HTMLCanvasElement) => { const image = canvas .toDataURL('image/png') .replace('image/png', 'image/octet-stream'); const a = document.createElement('a'); a.setAttribute('download', `${imageName}.png`); a.setAttribute('href', image); a.click(); canvas.remove(); }); };Estoy tratando de cubrir: return html2canvas... hasta el final.
Lo intenté-
jest.mock('html2canvas', () => { html2canvas: jest.fn(); })y
jest.mock('html2canvas', () => () => ({ then : jest.fn(() => ({ then : jest.fn(() => ({ toHaveBeenCalled : jest.fn().mockResolvedValue({}) })) })) })); const element = document.createElement('div'); element.id = "test"; document.body.appendChild(element); let de = downloadElement('test', 'testImage') as any; expect(de).toBeCalled();Probé mockResolvedValue también.
de = jest.fn().mockResolvedValue({});Parece que no puedo encontrar la manera de cubrirlo. Cualquier ayuda apreciada.