Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set up js dom for getBoundingClientRect() #1590

Closed
potapovDim opened this issue Aug 25, 2016 · 5 comments
Closed

set up js dom for getBoundingClientRect() #1590

potapovDim opened this issue Aug 25, 2016 · 5 comments

Comments

@potapovDim
Copy link

potapovDim commented Aug 25, 2016

screen shot 2016-08-25 at 4 57 33 pm

maybe somebody setups jsdom that it work with getBoundingClientRect() , i work with simulantjs and my console log back this->
screen shot 2016-08-25 at 5 03 42 pm

    const wrapper = mount(
      <Provider store={store}>
        <FullDecoratedBlock
          uid={'test'}
          setFocusedElement={setFocusedElement}/>
      </Provider>, {attachTo: document.getElementById('app')})
    expect(wrapper.find('.add-wrap').length).to.eql(0)
    simulant.fire(document.body.querySelector('.wrapper-resize-button'), 'mousedown')
    simulant.fire(document.body, 'mousemove', {target: {clientX: 51, clientY: 101}})

maybe someone has ideas??

@domenic
Copy link
Member

domenic commented Aug 25, 2016

I can't understand this issue very well. It seems like maybe it is a dupe of #1581, #653, #1322.

@potapovDim
Copy link
Author

@domenic i have component , when i move mouse to bottom of component , i wait that my component show buttop , and it bount to const rect = node.getBoundingClientRect() frow this rect get coordinates of mouse , and in component write const showPlusButton = event.insideX && Math.abs(event.pos.y - event.componentRect.bottom) <= MOUSE_DISTANCE when i move mouse i always get second screenshot

@Sebmaster
Copy link
Member

jsdom doesn't do any rendering, so getBoundingClientRect() always returns 0,0,0,0. That won't change (even foreseeable long-term). You can try to mock the function to emulate the results you'd expect. Also see #653.

@clearyandzap
Copy link

You can assume the .getBoundingClientRect() API works and do the following:

const p = document.createElement('p')

    p.getBoundingClientRect = jest.fn(() => ({
      x: 851.671875,
      y: 200.046875,
      width: 8.34375,
      height: 17,
      top: 967.046875,
      right: 860.015625,
      bottom: 984.046875,
      left: 851.671875,
    }))

ckifer added a commit to recharts/recharts that referenced this issue Jan 4, 2023
<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->
Refactor 2 very simple util file tests. `DomUtils` and `LogUtils`
  * add one new test for DomUtils
  * remove old files

* Note: DomUtils has some drawbacks with `jsdom` not being able to
actually get back client width and height
jsdom/jsdom#1590 (comment)

## Related Issue
N/A - Jest Migration

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->
Migrate karma tests to Jest

## How Has This Been Tested?
`npm run test` - all tests succeed

## Screenshots (if appropriate): N/A

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [x] Refactor
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.

Co-authored-by: Coltin Kifer <ckifer@amazon.com>
@leslie555
Copy link

leslie555 commented Jan 12, 2023

Update, I think using the way below is more suitable.

window.HTMLElement.prototype.getBoundingClientRect = () => ({
    bottom: 44,
    height: 24,
    left: 10,
    right: 35.67,
    top: 20,
    width: 25.67,
  } as DOMRect);

If you want to set it for a table element, just need to use window.HTMLTableElement.prototype.getBoundingClientRect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants