-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
275599a
commit c911d85
Showing
2 changed files
with
83 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useEffect, StrictMode } from 'react'; | ||
import { render, waitFor } from '@testing-library/react'; | ||
|
||
export const strictWrapper = ({ children }: any) => ( | ||
<StrictMode>{children}</StrictMode> | ||
); | ||
|
||
test('react: call order in StrictMode', async () => { | ||
const order = jest.fn(); | ||
|
||
const Child = () => { | ||
useEffect(() => { | ||
order('Child Effect'); | ||
return () => { | ||
order('Child Cleanup'); | ||
}; | ||
}, []); | ||
return null; | ||
}; | ||
|
||
const Parent = () => { | ||
useEffect(() => { | ||
order('Parent Effect'); | ||
return () => { | ||
order('Parent Cleanup'); | ||
}; | ||
}, []); | ||
return <Child />; | ||
}; | ||
|
||
const { unmount } = render(<Parent />, { wrapper: strictWrapper }); | ||
unmount(); | ||
|
||
await waitFor(() => { | ||
expect(order.mock.calls).toEqual([ | ||
['Child Effect'], | ||
['Parent Effect'], | ||
['Child Cleanup'], | ||
['Parent Cleanup'], | ||
['Child Effect'], | ||
['Parent Effect'], | ||
['Parent Cleanup'], | ||
['Child Cleanup'], | ||
]); | ||
}); | ||
}); | ||
|
||
test('react: call order in non StrictMode', async () => { | ||
const order = jest.fn(); | ||
|
||
const Child = () => { | ||
useEffect(() => { | ||
order('Child Effect'); | ||
return () => { | ||
order('Child Cleanup'); | ||
}; | ||
}, []); | ||
return null; | ||
}; | ||
|
||
const Parent = () => { | ||
useEffect(() => { | ||
order('Parent Effect'); | ||
return () => { | ||
order('Parent Cleanup'); | ||
}; | ||
}, []); | ||
return <Child />; | ||
}; | ||
|
||
const { unmount } = render(<Parent />); | ||
unmount(); | ||
|
||
await waitFor(() => { | ||
expect(order.mock.calls).toEqual([ | ||
['Child Effect'], | ||
['Parent Effect'], | ||
['Parent Cleanup'], | ||
['Child Cleanup'], | ||
]); | ||
}); | ||
}); |
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