I am using react-testing-library and jest. In componentdidupdate trying to get 'this.testRef.current.offsetHeight', getting offsetHeight as 0 even after content is loaded. I want DOM's exact offsetHeight because of having some conditions based on it.
Is there any possible solution to resolve in react-testing-library and jest.
I am using dangerouslySetInnerHTML to binding html content for testRef's element.
You can do something like this :
const originalOffsetHeight = Object.getOwnPropertyDescriptor(
HTMLElement.prototype,
'offsetHeight'
);
const originalOffsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth');
beforeAll(() => {
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
configurable: true,
value: 200,
});
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
configurable: true, value: 200
});
});
afterAll(() => {
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', originalOffsetHeight);
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', originalOffsetWidth);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With