mercredi 20 novembre 2019

ReactJS and jest: how to use a mock value for element.offsetWidth?

I have a component that programmatically creates a span element and adds text to it, then checks for its offsetWidth. Within my tests this value is 0. I would like to know how I can "force" it to another arbitrary value for testing.

I tried with the following with no avail:

Object.defineProperty(HTMLElement.prototype, 'offsetWidth', { configurable: true, value: 500 })

Can anyone point me to the right direction? Thanks!

Github: https://github.com/sickdyd/react-dynamic-badge

This is a link to the test file for the component: https://github.com/sickdyd/react-dynamic-badge/blob/master/src/test/test/DynamicBadge.test.js

Aucun commentaire:

Enregistrer un commentaire