Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
¿HAY alguna manera de simular SVGTextElement para poder probar la función, usando Jest, que usa getBBox() para medir el texto?

Escribí una función simple para medir textos antes de decidir qué tamaño de fuente debo usar.

 const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); const text = document.createElementNS('http://www.w3.org/2000/svg', 'text'); svg.appendChild(text); interface HeightAndWidth { width: number; height: number; } interface MeasureProps { label: string; fontFamily: string; size: number; fontWeight?: number; lineHeight?: number; letterSpacing?: string; } export const measureText = ({ label, fontFamily, size, fontWeight = 400, lineHeight = 1, letterSpacing = '0', }: MeasureProps): HeightAndWidth => { text.setAttribute('font-size', `${size}px`); text.setAttribute('line-height', `${lineHeight}px`); text.setAttribute('font-family', fontFamily); text.setAttribute('font-weight', `${fontWeight}`); text.setAttribute('letter-spacing', letterSpacing); text.textContent = label; document.body.appendChild(svg); const { width, height } = text.getBBox(); return { width, height, }; };

Es bastante sencillo y funciona bien, pero tengo problemas para escribir pruebas unitarias para esto. El problema es que mientras trabaja en el navegador, no funciona durante las pruebas y arroja un error.

text.getBBox no es una función.

Intenté burlarme de window.SVGTextElement pero sigue arrojando un error de que a su prototipo le faltan 241 propiedades más. Incluso probé su tipo, pero el error permanece, y el error con la falta de repeticiones de getBBox .

¿Hay alguna manera de probar un código como este?

 import { measureText } from '../measure'; interface MockSvgTextElement { prototype: { getBBox: () => { height: number; width: number }; }; } describe('animateNumber', () => { beforeEach(() => { window.SVGTextElement = { prototype: { getBBox: () => ({ height: 20, width: 100, }), }, } as MockSvgTextElement; const p = document.createElement('p'); document.body.appendChild(p); console.log('window.SVGTextElement', window.SVGTextElement); }); it('should measure text', () => { const textSize = measureText( { label: '12345', fontFamily: 'Roboto', size: 20, } ); expect(textSize).toEqual({}); }); });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

getBBox se puede usar para el tipo SVGGraphicsElement .

Cree un elemento <svg> debajo body y luego agregue un <text> debajo de svg .

Luego puede llamar a getBBox().width para el elemento de text

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda