I needed to create tests for a method that determined the proper height of a dynamic element, using either scrollHeight if it’s available (scrollHeight is zero for inline elements) and, if not, falling back to getBoundingClientRect()

Here’s how to mock them both:

scrollHeight:

scrollHeight is a property so we use the ‘get’ parameter in spyOn

getBoundingClientRect():

returns a new DOMRect() object to mimic the getBoundingClientRect function

--

--

Jim Keller

Chief Digital & Technology Officer @Eastern Standard. Lifelong full-stack developer. Jiu Jitsu novice. Aging punk dad.