-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Upgrade jest #4401
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
Upgrade jest #4401
Changes from all commits
25c5edd
b8aa4ba
35a31af
e972690
cbf086f
030e4a9
ac7dec8
543e19b
587ae76
599f183
4d6d30b
bf4b4db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,7 +50,7 @@ describe('useCheckboxGroup', () => { | |
| ]} /> | ||
| ); | ||
|
|
||
| let checkboxGroup = getByRole('group', {exact: true}); | ||
| let checkboxGroup = getByRole('group'); | ||
|
Member
Author
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. getByRole only supports strings now |
||
| let checkboxes = getAllByRole('checkbox') as HTMLInputElement[]; | ||
| expect(checkboxGroup).toBeInTheDocument(); | ||
| expect(checkboxes.length).toBe(3); | ||
|
|
@@ -119,7 +119,7 @@ describe('useCheckboxGroup', () => { | |
| {value: 'dragons', children: 'Dragons'} | ||
| ]} /> | ||
| ); | ||
| let checkboxGroup = getByRole('group', {exact: true}); | ||
| let checkboxGroup = getByRole('group'); | ||
|
|
||
| let labelId = checkboxGroup.getAttribute('aria-labelledby'); | ||
| expect(labelId).toBeDefined(); | ||
|
|
@@ -137,7 +137,7 @@ describe('useCheckboxGroup', () => { | |
| {value: 'dragons', children: 'Dragons'} | ||
| ]} /> | ||
| ); | ||
| let checkboxGroup = getByRole('group', {exact: true}); | ||
| let checkboxGroup = getByRole('group'); | ||
|
|
||
| expect(checkboxGroup).toHaveAttribute('aria-label', 'My Favorite Pet'); | ||
| }); | ||
|
|
@@ -153,7 +153,7 @@ describe('useCheckboxGroup', () => { | |
| {value: 'dragons', children: 'Dragons'} | ||
| ]} /> | ||
| ); | ||
| let checkboxGroup = getByRole('group', {exact: true}); | ||
| let checkboxGroup = getByRole('group'); | ||
|
|
||
| expect(checkboxGroup).toHaveAttribute('data-testid', 'favorite-pet'); | ||
| }); | ||
|
|
@@ -171,7 +171,7 @@ describe('useCheckboxGroup', () => { | |
| ]} /> | ||
| ); | ||
|
|
||
| let checkboxGroup = getByRole('group', {exact: true}); | ||
| let checkboxGroup = getByRole('group'); | ||
| expect(checkboxGroup).toHaveAttribute('aria-disabled', 'true'); | ||
|
|
||
| let checkboxes = getAllByRole('checkbox') as HTMLInputElement[]; | ||
|
|
@@ -198,7 +198,7 @@ describe('useCheckboxGroup', () => { | |
| ]} /> | ||
| ); | ||
|
|
||
| let checkboxGroup = getByRole('group', {exact: true}); | ||
| let checkboxGroup = getByRole('group'); | ||
| expect(checkboxGroup).not.toHaveAttribute('aria-disabled'); | ||
|
|
||
| let checkboxes = getAllByRole('checkbox') as HTMLInputElement[]; | ||
|
|
@@ -218,7 +218,7 @@ describe('useCheckboxGroup', () => { | |
| ]} /> | ||
| ); | ||
|
|
||
| let checkboxGroup = getByRole('group', {exact: true}); | ||
| let checkboxGroup = getByRole('group'); | ||
| expect(checkboxGroup).not.toHaveAttribute('aria-disabled'); | ||
|
|
||
| let checkboxes = getAllByRole('checkbox') as HTMLInputElement[]; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,16 @@ export function useAutoScroll(ref: RefObject<Element>) { | |
| dy: 0 | ||
| }).current; | ||
|
|
||
| useEffect(() => { | ||
|
Member
Author
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. saw we missed a raf cleanup
Member
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. for other reviewers, noted that TableView DnD doesn't auto scroll but that seems to have been broken since before this PR |
||
| return () => { | ||
| if (state.timer) { | ||
| cancelAnimationFrame(state.timer); | ||
| state.timer = null; | ||
| } | ||
| }; | ||
| // state will become a new object, so it's ok to use in the dependency array for unmount | ||
| }, [state]); | ||
|
|
||
| let scroll = useCallback(() => { | ||
| scrollableRef.current.scrollLeft += state.dx; | ||
| scrollableRef.current.scrollTop += state.dy; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,6 @@ import userEvent from '@testing-library/user-event'; | |
|
|
||
| describe('useDroppableCollection', () => { | ||
| beforeEach(() => { | ||
| jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => setTimeout(cb, 0)); | ||
| jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { | ||
| let y = 0; | ||
| let height = 50; | ||
|
|
@@ -63,7 +62,7 @@ describe('useDroppableCollection', () => { | |
| return this.getBoundingClientRect().height; | ||
| }); | ||
|
|
||
| jest.useFakeTimers('legacy'); | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
|
|
@@ -224,35 +223,35 @@ describe('useDroppableCollection', () => { | |
|
|
||
| let dataTransfer = new DataTransfer(); | ||
| fireEvent(draggable, new DragEvent('dragstart', {dataTransfer, clientX: 0, clientY: 0})); | ||
| act(() => jest.runAllTimers()); | ||
| act(() => jest.advanceTimersToNextTimer()); | ||
|
Member
Author
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. rafs appear to be able to cause an infinite timer loop, so move all of these to advanceTimersToNextTimer, since that's all we actually want to do here anyways |
||
| expect(draggable).toHaveAttribute('data-dragging', 'true'); | ||
|
|
||
| fireEvent(cells[0], new DragEvent('dragenter', {dataTransfer, clientX: 30, clientY: 30})); | ||
| act(() => jest.runOnlyPendingTimers()); | ||
| act(() => jest.advanceTimersToNextTimer()); | ||
| expect(scrollTop).not.toHaveBeenCalled(); | ||
|
|
||
| fireEvent(cells[2], new DragEvent('dragover', {dataTransfer, clientX: 30, clientY: 100})); | ||
| act(() => jest.runOnlyPendingTimers()); | ||
| act(() => jest.advanceTimersToNextTimer()); | ||
| expect(scrollTop).not.toHaveBeenCalled(); | ||
|
|
||
| fireEvent(cells[4], new DragEvent('dragover', {dataTransfer, clientX: 30, clientY: 135})); | ||
| act(() => jest.runOnlyPendingTimers()); | ||
| act(() => jest.advanceTimersToNextTimer()); | ||
| expect(scrollTop).toHaveBeenCalledTimes(1); | ||
| act(() => jest.runOnlyPendingTimers()); | ||
| act(() => jest.advanceTimersToNextTimer()); | ||
| expect(scrollTop).toHaveBeenCalledTimes(2); | ||
| jest.clearAllTimers(); | ||
|
|
||
| fireEvent(cells[2], new DragEvent('dragover', {dataTransfer, clientX: 30, clientY: 100})); | ||
| act(() => jest.runAllTimers()); | ||
| act(() => jest.advanceTimersToNextTimer()); | ||
| expect(scrollTop).toHaveBeenCalledTimes(2); | ||
|
|
||
| fireEvent(cells[2], new DragEvent('dragover', {dataTransfer, clientX: 30, clientY: 15})); | ||
| act(() => jest.runOnlyPendingTimers()); | ||
| act(() => jest.advanceTimersToNextTimer()); | ||
| expect(scrollTop).toHaveBeenCalledTimes(3); | ||
| jest.clearAllTimers(); | ||
|
|
||
| fireEvent(cells[2], new DragEvent('dragover', {dataTransfer, clientX: 30, clientY: 30})); | ||
| act(() => jest.runAllTimers()); | ||
| act(() => jest.advanceTimersToNextTimer()); | ||
| expect(scrollTop).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,7 +37,7 @@ function pointerEvent(type, opts) { | |
|
|
||
| describe('useHover', function () { | ||
| beforeAll(() => { | ||
| jest.useFakeTimers('legacy'); | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| it('does not handle hover events if disabled', function () { | ||
|
|
@@ -530,10 +530,7 @@ describe('useHover', function () { | |
| } | ||
|
|
||
| beforeAll(() => { | ||
| jest.useFakeTimers('legacy'); | ||
| }); | ||
| afterAll(() => { | ||
| jest.useRealTimers(); | ||
| jest.useFakeTimers(); | ||
|
Member
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. Why do we have to have
Member
Author
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. no idea, didn't notice it, could probably be removed |
||
| }); | ||
|
|
||
| let matchMedia; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,13 +23,7 @@ function Example(props) { | |
|
|
||
| describe('useMove', function () { | ||
| beforeAll(() => { | ||
| jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb()); | ||
| jest.useFakeTimers('legacy'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
|
Member
Author
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. duplicate afterEach, can safely remove one of them |
||
| // for restoreTextSelection | ||
| jest.runAllTimers(); | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,8 +41,7 @@ function pointerEvent(type, opts) { | |
|
|
||
| describe('usePress', function () { | ||
| beforeAll(() => { | ||
| jest.useFakeTimers('legacy'); | ||
| jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb()); | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
|
|
@@ -2441,7 +2440,7 @@ describe('usePress', function () { | |
| fireEvent.touchStart(el, {targetTouches: [{identifier: 1}]}); | ||
| fireEvent.touchEnd(el, {changedTouches: [{identifier: 1}]}); | ||
|
|
||
| act(() => {jest.advanceTimersByTime(300);}); | ||
| act(() => {jest.advanceTimersByTime(316);}); | ||
|
Member
Author
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. raf is no longer instant in modern timer mocking, so have to account for it while advancing timers
Member
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. why change by 16? That makes me nervous that this is too fragile.
Member
Author
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. a raf takes ~16ms in the real world, in fake timers land, it always takes 16. previously we had a mock of the raf with a timeout of 0 |
||
| expect(document.documentElement.style.webkitUserSelect).toBe(mockUserSelect); | ||
|
|
||
| // Checkbox doesn't remove `user-select: none;` style from HTML Element issue | ||
|
|
@@ -2611,7 +2610,7 @@ describe('usePress', function () { | |
| expect(document.documentElement.style.webkitUserSelect).toBe('none'); | ||
|
|
||
| unmount(); | ||
| act(() => {jest.advanceTimersByTime(300);}); | ||
| act(() => {jest.advanceTimersByTime(316);}); | ||
| expect(document.documentElement.style.webkitUserSelect).toBe(mockUserSelect); | ||
| }); | ||
|
|
||
|
|
@@ -2698,17 +2697,12 @@ describe('usePress', function () { | |
| } | ||
|
|
||
| beforeAll(() => { | ||
| jest.useFakeTimers('legacy'); | ||
| }); | ||
| afterAll(() => { | ||
| jest.useRealTimers(); | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| let matchMedia; | ||
| beforeEach(() => { | ||
| matchMedia = new MatchMediaMock(); | ||
| // this needs to be a setTimeout so that the dialog can be removed from the dom before the callback is invoked | ||
| jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => setTimeout(() => cb(), 0)); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
|
|
@@ -2723,7 +2717,6 @@ describe('usePress', function () { | |
| } | ||
|
|
||
| matchMedia.clear(); | ||
| window.requestAnimationFrame.mockRestore(); | ||
| }); | ||
|
|
||
| describe.each` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -180,9 +180,17 @@ describe('useModal', function () { | |
| </div> | ||
| ) | ||
| ).toThrow(); | ||
| expect(console.error).toHaveBeenCalledWith( | ||
| expect.stringContaining('An OverlayContainer must not be inside another container. Please change the portalContainer prop.'), | ||
| expect.anything() | ||
| expect.extend({ | ||
| toHaveBeenNthCalledWithError(received, index, arg) { | ||
| return { | ||
| pass: received.mock.calls[index - 1][0].toString().includes(arg), | ||
| message: () => `expected ${received.mock.calls[0][0]} to include ${arg}` | ||
| }; | ||
| } | ||
| }); | ||
| expect(console.error).toHaveBeenNthCalledWithError( | ||
|
Member
Author
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. error shape changed, add matcher that makes it easy to check |
||
| 1, | ||
| 'An OverlayContainer must not be inside another container. Please change the portalContainer prop.' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
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.
included by jest-environment-jsdom