I need to test how the select component behaves when it is positioned at the bottom of a page. When it is 1) at the top of the page - it opens dropdown to the bottom, and when it is 2) at the bottom of the page it opens dropdown to the top. I have no problem with testing its first behaviour, but I cannot test the second.
I tried to test it like this:
document.body.style.height = '500px'
const { container } = render(<Select {...requiredProps} />)
container.firstChild.style.position = 'fixed'
container.firstChild.style.bottom = '0'
fireEvent.click(screen.getByTestId('dropdown-container'))
expect(screen.getByTestId('dropdown-items')).toHaveStyle(stripSpaces('bottom: 100%'))
But still I cannot simulate the proper behaviour.
I tried also below:
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 500 })
render(<Select {...requiredProps} />)
fireEvent.scroll(window, { target: { scrollY: 500 } })
fireEvent.click(screen.getByTestId('dropdown-container'))
expect(screen.getByTestId('dropdown-items')).toHaveStyle(stripSpaces('bottom: 100%'))
And also below:
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 500 })
render(<Select {...requiredProps} style={{ position: 'fixed', bottom: '0' }} />)
fireEvent.click(screen.getByTestId('dropdown-container'))
expect(screen.getByTestId('dropdown-items')).toHaveStyle(stripSpaces('bottom: 100%')
The problem is that to open dropdown at the top, I use getBoundingClientRect() on select, to find out how far it is from top and bottom. And in jest testing, getBoundingClientRect always returns 0 for each value - see https://github.com/jsdom/jsdom/pull/689
I try to manipulate container to give it top value of 480 but with no success.