Necesito ayuda para aumentar la cobertura de mi código. Tengo esta función en mi archivo ts y necesito entender cómo hacer que se ejecute esta instrucción if.
setWidth(): void { let maxHeight = 0; document.querySelectorAll<HTMLElement>(`.body`).forEach(element => { if (element.offsetHeight > maxHeight){ maxHeight = element.offsetHeight; } });No tengo claro cómo haría que forEach se ejecutara en mi archivo spec.ts.
Prueba esto:
it('should set maxHeight', () => { // set a spy and return a mocked array. // add as any here below as well. spyOn(document, 'querySelectorAll').and.returnValue([{ offsetHeight: 20 }] as any); service.setWidth(); // below is a bad expectation but I am not sure what to expect for you from the // code provided. Do an assertion that relates to setWidth. expect(1).toBe(1); });