-
Notifications
You must be signed in to change notification settings - Fork 209
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
chore: migrate use of @testing-library/react-hooks #867
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
adae305
chore: use @testing-library/react instead of react-hooks in useBalanc…
fionnachan ab12156
Merge remote-tracking branch 'origin/master' into testing-library-react
fionnachan 8240c68
update types packages
fionnachan f89533a
fix type errors
fionnachan 15763b5
Merge remote-tracking branch 'origin/master' into testing-library-react
fionnachan 6d5cc2d
do not upgrade packages that can be not updated
fionnachan 1120411
review comments cleanup
fionnachan 2125816
Merge remote-tracking branch 'origin/master' into testing-library-react
fionnachan 0312efe
update hook type
fionnachan 76f847c
Merge remote-tracking branch 'origin/master' into testing-library-react
fionnachan 1cde4bf
make renderHookAsync for all hooks
fionnachan 4ff23bb
remove unused export
fionnachan e5ce224
update types
fionnachan 7dadb9d
update types
fionnachan 7bb4d8c
remove utils and colo
fionnachan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,14 @@ | |
* @jest-environment jsdom | ||
*/ | ||
|
||
import { act, renderHook } from '@testing-library/react-hooks' | ||
import { RenderHookResult, act, renderHook } from '@testing-library/react' | ||
import { StaticJsonRpcProvider } from '@ethersproject/providers' | ||
import { BigNumber } from 'ethers' | ||
import { SWRConfig } from 'swr' | ||
import { PropsWithChildren } from 'react' | ||
import { MultiCaller } from '@arbitrum/sdk' | ||
import { useBalance } from '../useBalance' | ||
|
||
import { UseBalanceProps, useBalance } from '../useBalance' | ||
|
||
// Create a new cache for every test | ||
const Container = ({ children }: PropsWithChildren<unknown>) => ( | ||
|
@@ -17,6 +18,27 @@ const Container = ({ children }: PropsWithChildren<unknown>) => ( | |
|
||
const walletAddress = '0x58b6a8a3302369daec383334672404ee733ab239' | ||
|
||
const renderHookAsyncUseBalance = async ({ | ||
provider, | ||
walletAddress | ||
}: UseBalanceProps) => { | ||
let hook: | ||
| RenderHookResult<ReturnType<typeof useBalance>, UseBalanceProps> | ||
| undefined | ||
|
||
await act(async () => { | ||
hook = renderHook(() => useBalance({ provider, walletAddress }), { | ||
wrapper: Container | ||
}) | ||
}) | ||
|
||
if (!hook) { | ||
throw new Error('Hook is not defined') | ||
} | ||
|
||
return { result: hook.result } | ||
} | ||
|
||
describe('useBalance', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
|
@@ -42,16 +64,10 @@ describe('useBalance', () => { | |
]) | ||
) | ||
|
||
const { result, waitForNextUpdate } = renderHook( | ||
() => | ||
useBalance({ | ||
provider, | ||
walletAddress: undefined | ||
}), | ||
{ wrapper: Container } | ||
) | ||
|
||
await waitForNextUpdate({ timeout: 250 }) | ||
const { result } = await renderHookAsyncUseBalance({ | ||
provider, | ||
walletAddress: undefined | ||
}) | ||
|
||
const { | ||
current: { | ||
|
@@ -89,18 +105,10 @@ describe('useBalance', () => { | |
]) | ||
) | ||
|
||
const { result, waitForNextUpdate } = renderHook( | ||
() => | ||
useBalance({ | ||
provider, | ||
walletAddress | ||
}), | ||
{ wrapper: Container } | ||
) | ||
|
||
try { | ||
await waitForNextUpdate({ timeout: 100 }) | ||
} catch (err) {} | ||
const { result } = await renderHookAsyncUseBalance({ | ||
provider, | ||
walletAddress | ||
}) | ||
|
||
const { | ||
current: { | ||
|
@@ -135,16 +143,11 @@ describe('useBalance', () => { | |
]) | ||
) | ||
|
||
const { result, waitForNextUpdate } = renderHook( | ||
() => | ||
useBalance({ | ||
provider, | ||
walletAddress | ||
}), | ||
{ wrapper: Container } | ||
) | ||
const { result } = await renderHookAsyncUseBalance({ | ||
provider, | ||
walletAddress | ||
}) | ||
|
||
await waitForNextUpdate({ timeout: 100 }) | ||
expect(result.current.eth[0]?.toNumber()).toEqual(32) | ||
expect(getBalanceSpy).toHaveBeenCalledTimes(1) | ||
expect(getBalanceSpy).toHaveBeenCalledWith(walletAddress) | ||
|
@@ -170,16 +173,10 @@ describe('useBalance', () => { | |
]) | ||
) | ||
|
||
const { result, waitForNextUpdate } = renderHook( | ||
() => | ||
useBalance({ | ||
provider, | ||
walletAddress | ||
}), | ||
{ wrapper: Container } | ||
) | ||
|
||
await waitForNextUpdate({ timeout: 100 }) | ||
const { result } = await renderHookAsyncUseBalance({ | ||
provider, | ||
walletAddress | ||
}) | ||
|
||
const { | ||
current: { | ||
|
@@ -191,8 +188,9 @@ describe('useBalance', () => { | |
expect(getBalanceSpy).toHaveBeenCalledTimes(1) | ||
expect(getBalanceSpy).toHaveBeenCalledWith(walletAddress) | ||
|
||
updateEthBalance() | ||
await waitForNextUpdate({ timeout: 100 }) | ||
await act(async () => { | ||
updateEthBalance() | ||
}) | ||
|
||
const { | ||
current: { | ||
|
@@ -233,14 +231,10 @@ describe('useBalance', () => { | |
]) | ||
) | ||
|
||
const { result, waitForValueToChange } = renderHook( | ||
() => | ||
useBalance({ | ||
provider, | ||
walletAddress | ||
}), | ||
{ wrapper: Container } | ||
) | ||
const { result } = await renderHookAsyncUseBalance({ | ||
provider, | ||
walletAddress | ||
}) | ||
const { | ||
current: { | ||
erc20: [, updateErc20Balances] | ||
|
@@ -252,9 +246,7 @@ describe('useBalance', () => { | |
'0x0000000000000000000000000000000000000001', | ||
'0x0000000000000000000000000000000000000002' | ||
] | ||
updateErc20Balances(erc20) | ||
|
||
await waitForValueToChange(() => result.current.erc20, { timeout: 100 }) | ||
await act(async () => updateErc20Balances(erc20)) | ||
|
||
expect(result.current.erc20[0]).toEqual({ | ||
'0x0000000000000000000000000000000000000000': BigNumber.from(10), | ||
|
@@ -290,14 +282,10 @@ describe('useBalance', () => { | |
]) | ||
) | ||
|
||
const { result, waitForValueToChange } = renderHook( | ||
() => | ||
useBalance({ | ||
provider, | ||
walletAddress | ||
}), | ||
{ wrapper: Container } | ||
) | ||
const { result } = await renderHookAsyncUseBalance({ | ||
provider, | ||
walletAddress | ||
}) | ||
const { | ||
current: { | ||
erc20: [, updateErc20Balances] | ||
|
@@ -308,9 +296,7 @@ describe('useBalance', () => { | |
'0xABCdef0000000000000000000000000000000000', | ||
'0xAAADDD0000000000000000000000000000000001' | ||
] | ||
updateErc20Balances(erc20) | ||
|
||
await waitForValueToChange(() => result.current.erc20, { timeout: 100 }) | ||
await act(async () => updateErc20Balances(erc20)) | ||
|
||
const { | ||
current: { | ||
|
@@ -339,32 +325,31 @@ describe('useBalance', () => { | |
} | ||
]) | ||
) | ||
}) | ||
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. assertions shouldn't be wrapped in the same |
||
|
||
const newAddresses = [ | ||
'0xAaADDD0000000000000000000000000000000001', | ||
'0xAAAAAA0000000000000000000000000000000002' | ||
] | ||
await act(async () => updateErc20Balances(newAddresses)) | ||
|
||
/** | ||
* 0x..0 is untouched | ||
* 0x..1 is updated | ||
* 0x..2 is added | ||
* | ||
* All balances are stored in lowercase | ||
*/ | ||
expect(result.current.erc20[0]).toEqual({ | ||
'0xabcdef0000000000000000000000000000000000': BigNumber.from(11), | ||
'0xaaaddd0000000000000000000000000000000001': BigNumber.from(25), | ||
'0xaaaaaa0000000000000000000000000000000002': BigNumber.from(33) | ||
}) | ||
|
||
const newAddresses = [ | ||
'0xAaADDD0000000000000000000000000000000001', | ||
'0xAAAAAA0000000000000000000000000000000002' | ||
] | ||
updateErc20Balances(newAddresses) | ||
await waitForValueToChange(() => result.current.erc20, { timeout: 500 }) | ||
|
||
/** | ||
* 0x..0 is untouched | ||
* 0x..1 is updated | ||
* 0x..2 is added | ||
* | ||
* All balances are stored in lowercase | ||
*/ | ||
expect(result.current.erc20[0]).toEqual({ | ||
'0xabcdef0000000000000000000000000000000000': BigNumber.from(11), | ||
'0xaaaddd0000000000000000000000000000000001': BigNumber.from(25), | ||
'0xaaaaaa0000000000000000000000000000000002': BigNumber.from(33) | ||
}) | ||
|
||
expect(getBalanceSpy).toHaveBeenCalledTimes(1) | ||
expect(getTokenDataSpy).toHaveBeenCalledTimes(2) | ||
expect(getTokenDataSpy).toHaveBeenLastCalledWith(newAddresses, { | ||
balanceOf: { account: walletAddress } | ||
}) | ||
expect(getBalanceSpy).toHaveBeenCalledTimes(1) | ||
expect(getTokenDataSpy).toHaveBeenCalledTimes(2) | ||
expect(getTokenDataSpy).toHaveBeenLastCalledWith(newAddresses, { | ||
balanceOf: { account: walletAddress } | ||
}) | ||
}) | ||
}) | ||
|
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.
@types/react & @types/react-dom v17 for some reason allow it to be of type
any
and won't throw an error but v18 will