Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
¿Cómo probar un método en Class que extiende HTMLElement con Jest?

Tengo una clase abstracta.

 class Component extends HTMLElement { connectedCallback() { this.render(); } render() { this.innerHTML = this.template(); } template() { return ''; } ... }

Y tengo una Clase que extiende la clase Component .

 class StatisticsModal extends Component { render() { const winningCounts = this.getWinningCounts(winningNumbers, lottoList); const earningsRate = this.getEarningsRate(winningCounts); this.innerHTML = this.template(winningCounts, earningsRate); document.querySelector('body').classList.add('modal-open'); } getEarningsRate(winningCounts) { const total = [5, 4, 3, 2, 1].reduce((money, rank) => { return money + winningCounts[rank] * LOTTO.PRIZE_MONEY[rank]; }, 0); return Math.floor((total / 1000) * 100); } getWinningCounts(winningNumbers, lottoList) { return lottoList.reduce((winningCounts, lottoNums) => { const count = intersect(lottoNums, winningNumbers.normal).length; if (count === 6) winningCounts[1] += 1; else if (count === 5 && lottoNums.includes(winningNumbers.bonus)) winningCounts[2] += 1; else if (count === 5) winningCounts[3] += 1; else if (count === 4) winningCounts[4] += 1; else if (count === 3) winningCounts[5] += 1; return winningCounts; }, Array(6).fill(0)); } ... ... }

Ahora, quiero probar el componente getWinningCounts of StatisticsModal con Jest.

Entonces, escribo el código de prueba como se muestra a continuación.

 describe('This is test description', () => { const winningNumbers = { normal: [1, 2, 3, 4, 5, 6], bonus: 7, }; test('Let's do test!', () => { const lottoList = [ [1, 22, 33, 44, 55, 66], [1, 2, 33, 44, 55, 66], [1, 2, 3, 44, 55, 66], [1, 2, 3, 4, 55, 66], [1, 2, 3, 4, 5, 66], [1, 2, 3, 4, 5, 6], [1, 2, 3, 7, 11, 22], ]; const statisticsModal = new StatisticsModal(); // ReferenceError: HTMLElement is not defined const winningCounts = statisticsModal.getWinningCounts(winningNumbers, lottoList); expect(winningCounts[5]).toBe(2); }); });

Pero tengo error.

"ReferenceError: HTMLElement no está definido"

Entonces, quiero saber cómo probar ese método en clase.

Revisa por favor :D

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!