-
Notifications
You must be signed in to change notification settings - Fork 715
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
[deps] Upgrade to react v17 and react-testing-library #1268
Merged
Merged
Changes from 43 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
7af9d27
add react-17 and react-testing-library
50b491b
upgrade and fix unit tests for visx-annotation
gazcn007 1486bc6
add react 17 and fix unit tests for withBoundingRects
gazcn007 13c4d31
nit fixes
gazcn007 0c31c0e
ts fixes
gazcn007 7d1baf5
update visx-brush
gazcn007 1e72ca7
update visx-chord
gazcn007 56ea63d
update visx-clip-path
gazcn007 43e0298
update visx-demo
gazcn007 9ad75ba
update visx-drag
gazcn007 d4fc817
update visx-geo
gazcn007 38cecc4
update visx-glyph
gazcn007 307c0df
update visx-gradient
gazcn007 dee81c7
update visx-grid
gazcn007 7d11538
update visx-group
gazcn007 f747068
update visx-heatmap
gazcn007 c510122
update visx-hierarchy
gazcn007 d89e019
update visx-legend
gazcn007 417fde2
update visx-marker
gazcn007 7a5cbeb
update visx-network
gazcn007 eb19772
update visx-pattern
gazcn007 5f77f15
update visx-responsive
gazcn007 730b9df
update visx-shape
gazcn007 0fa3a14
update visx-stats
gazcn007 59ee866
update visx-text
gazcn007 6c37f2d
update visx-threshold
gazcn007 94ac62e
update visx-tooltip
gazcn007 2dce10b
update visx-visx
gazcn007 35e012a
update visx-voronoi
gazcn007 47135bf
update visx-zoom
gazcn007 2c0f086
update visx-xychart
gazcn007 ae6a329
rewrote tests
gazcn007 1e5f22c
update AreaSeries test
gazcn007 74e7e30
update Hooks and Providers unit tests
gazcn007 4cbaaf5
add AreaStack and BarStack tests
gazcn007 84f54cf
fix unit test for BarGroup, BarSeries, GlyphSeries, Grid, LineSeries,…
gazcn007 e8e8fa4
fix Axis.test.tsx
gazcn007 71596d6
fix comments
gazcn007 28084a7
add react 16.8.0 as a dep
gazcn007 350fe0f
address comments
gazcn007 7c8f061
update visx-react-spring
gazcn007 66f7e2e
update visx-demo dependencies
gazcn007 9ba1368
update visx-axis depedencies
gazcn007 5d8da92
remove shallow from visx-text/Test.test.tsx
gazcn007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,83 @@ | ||
/* eslint-disable jsx-a11y/no-static-element-interactions */ | ||
/* eslint-disable jsx-a11y/click-events-have-key-events */ | ||
import * as React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { render, waitFor } from '@testing-library/react'; | ||
import fireEvent from '@testing-library/user-event'; | ||
import { withBoundingRects } from '../src'; | ||
import '@testing-library/jest-dom'; | ||
|
||
const expectedRectShape = expect.objectContaining({ | ||
top: expect.any(Number), | ||
right: expect.any(Number), | ||
bottom: expect.any(Number), | ||
left: expect.any(Number), | ||
width: expect.any(Number), | ||
height: expect.any(Number), | ||
}); | ||
type RectShape = { | ||
top?: number; | ||
right?: number; | ||
bottom?: number; | ||
left?: number; | ||
width?: number; | ||
height?: number; | ||
}; | ||
|
||
const emptyRect = { | ||
top: 0, | ||
right: 0, | ||
bottom: 0, | ||
left: 0, | ||
width: 0, | ||
height: 0, | ||
}; | ||
|
||
const mockRect = { | ||
top: 50, | ||
left: 50, | ||
bottom: 0, | ||
right: 0, | ||
}; | ||
|
||
type BoundingRectsComponentProps = { | ||
rect?: RectShape; | ||
parentRect?: RectShape; | ||
getRects?: () => DOMRect; | ||
children?: JSX.Element; | ||
otherProps?: object; | ||
}; | ||
|
||
// Component created for testing purpose | ||
function BoundingRectsComponent({ | ||
rect, | ||
parentRect, | ||
getRects, | ||
children, | ||
...otherProps | ||
}: BoundingRectsComponentProps) { | ||
const parentRectStyle = { | ||
top: parentRect?.top, | ||
left: parentRect?.left, | ||
bottom: parentRect?.bottom, | ||
right: parentRect?.right, | ||
}; | ||
|
||
const rectStyle = { | ||
top: rect?.top, | ||
left: rect?.left, | ||
bottom: rect?.bottom, | ||
right: rect?.right, | ||
}; | ||
|
||
return ( | ||
<div data-testid="BoundingRectsComponentParent" style={parentRectStyle}> | ||
<div data-testid="BoundingRectsComponent" style={rectStyle} onClick={() => getRects?.()}> | ||
{children} | ||
{JSON.stringify(otherProps)} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
describe('withBoundingRects()', () => { | ||
beforeAll(() => { | ||
// mock getBoundingClientRect | ||
Element.prototype.getBoundingClientRect = jest.fn(() => ({ | ||
width: 100, | ||
height: 100, | ||
top: 0, | ||
left: 0, | ||
bottom: 0, | ||
right: 0, | ||
jest.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(() => ({ | ||
...mockRect, | ||
x: 0, | ||
y: 0, | ||
width: 100, | ||
height: 100, | ||
toJSON: jest.fn(), | ||
})); | ||
}); | ||
|
@@ -40,41 +86,48 @@ describe('withBoundingRects()', () => { | |
expect(withBoundingRects).toBeDefined(); | ||
}); | ||
|
||
test('it should pass rect, parentRect, and getRect props to the wrapped component', () => { | ||
const Component = () => <div />; | ||
const HOC = withBoundingRects(Component); | ||
const wrapper = mount(<HOC />); | ||
const RenderedComponent = wrapper.find(Component); | ||
test('it should pass rect, parentRect, and getRect props to the wrapped component', async () => { | ||
const HOC = withBoundingRects(BoundingRectsComponent); | ||
// @ts-ignore | ||
const { getByTestId } = render(<HOC />); | ||
|
||
// getBoundingClientRect should be called twice, once for the component, and once for its parent | ||
await waitFor(() => expect(Element.prototype.getBoundingClientRect).toHaveBeenCalledTimes(2)); | ||
|
||
expect(Element.prototype.getBoundingClientRect).toHaveBeenCalled(); | ||
expect(RenderedComponent.prop('rect')).toEqual(expectedRectShape); | ||
expect(RenderedComponent.prop('parentRect')).toEqual(expectedRectShape); | ||
expect(typeof RenderedComponent.prop('getRects')).toBe('function'); | ||
const RenderedComponent = getByTestId('BoundingRectsComponent'); | ||
const RenderedComponentParent = getByTestId('BoundingRectsComponentParent'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL |
||
|
||
const expectedStyle = `top: ${mockRect.top}px; bottom: ${mockRect.bottom}px; left: ${mockRect.left}px; right: ${mockRect.right}px;`; | ||
expect(RenderedComponent).toHaveStyle(expectedStyle); | ||
expect(RenderedComponentParent).toHaveStyle(expectedStyle); | ||
|
||
fireEvent.click(RenderedComponent); | ||
// upon onClick time, getBuondingClientRect should be called extra 2 times | ||
expect(Element.prototype.getBoundingClientRect).toHaveBeenCalledTimes(4); | ||
}); | ||
|
||
test('it should pass additional props to the wrapped component', () => { | ||
const Component = () => <div />; | ||
const HOC = withBoundingRects(Component); | ||
const HOC = withBoundingRects(BoundingRectsComponent); | ||
// @ts-ignore | ||
const wrapper = mount(<HOC bananas="are yellow" />); | ||
const RenderedComponent = wrapper.find(Component); | ||
expect(RenderedComponent.prop('bananas')).toBe('are yellow'); | ||
const { getByText } = render(<HOC bananas="are yellow" />); | ||
expect(getByText('are yellow', { exact: false })).toBeInTheDocument(); | ||
}); | ||
|
||
test('it should return default empty state if no node', () => { | ||
test('it should not render if no node', () => { | ||
const Component = () => null; | ||
const HOC = withBoundingRects(Component); | ||
const wrapper = mount(<HOC />); | ||
const RenderedComponent = wrapper.find(Component); | ||
expect(RenderedComponent.prop('rect')).toBeUndefined(); | ||
expect(RenderedComponent.prop('parentRect')).toBeUndefined(); | ||
const { container } = render(<HOC />); | ||
expect(container.innerHTML).toHaveLength(0); | ||
}); | ||
|
||
test('it should set rect and parentRect to empty state if no getBoundingClient()', () => { | ||
const Component = () => <>{''}</>; | ||
const HOC = withBoundingRects(Component); | ||
const wrapper = mount(<HOC />); | ||
const RenderedComponent = wrapper.find(Component); | ||
expect(RenderedComponent.prop('rect')).toEqual(emptyRect); | ||
(Element.prototype.getBoundingClientRect as unknown) = null; | ||
const HOC = withBoundingRects(BoundingRectsComponent); | ||
// @ts-ignore | ||
const { getByTestId } = render(<HOC />); | ||
const RenderedComponent = getByTestId('BoundingRectsComponent'); | ||
const RenderedComponentParent = getByTestId('BoundingRectsComponentParent'); | ||
expect(RenderedComponent).toHaveStyle(emptyRect); | ||
expect(RenderedComponentParent).toHaveStyle(emptyRect); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@williaster This is where RTL's philosophy of testing differs from Enzyme. Directly checking internal props & states is a NO-NO in RTL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^I agree the RTL philosophy is better for things like this 💯 this is why enzyme struggles with adapters for each react release 😅