From b150cf5ad893ff2ba1d56f3e999763f10e52293a Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 1 Jul 2022 13:43:42 +0200 Subject: [PATCH] use tl/react for testing hooks --- hooks/__tests__/useAsync.test.ts | 2 +- hooks/__tests__/useChainId.test.ts | 12 +++- hooks/__tests__/useChains.test.ts | 2 +- hooks/__tests__/useGasPrice.test.ts | 2 +- .../useMnemonicName.test.ts} | 4 +- hooks/__tests__/usePathRewrite.test.ts | 2 +- hooks/__tests__/useSafeAddress.test.ts | 2 +- package.json | 2 +- yarn.lock | 71 ++++++++++++++----- 9 files changed, 72 insertions(+), 27 deletions(-) rename hooks/{useMnemonicName/index.test.ts => __tests__/useMnemonicName.test.ts} (94%) diff --git a/hooks/__tests__/useAsync.test.ts b/hooks/__tests__/useAsync.test.ts index 93cd606883..66af3de922 100644 --- a/hooks/__tests__/useAsync.test.ts +++ b/hooks/__tests__/useAsync.test.ts @@ -1,4 +1,4 @@ -import { act, renderHook } from '@testing-library/react-hooks' +import { act, renderHook } from '@testing-library/react' import useAsync from '@/hooks/useAsync' // Jest tests for the useAsync hook diff --git a/hooks/__tests__/useChainId.test.ts b/hooks/__tests__/useChainId.test.ts index 6fa616320f..a41d607596 100644 --- a/hooks/__tests__/useChainId.test.ts +++ b/hooks/__tests__/useChainId.test.ts @@ -1,4 +1,4 @@ -import { renderHook } from '@testing-library/react-hooks' +import { renderHook } from '@testing-library/react' import { useRouter } from 'next/router' import TestProviderWrapper from '@/mocks/TestProviderWrapper' import useChainId from '@/hooks/useChainId' @@ -54,8 +54,14 @@ describe('useChainId hook', () => { }, })) - const { result } = renderHook(() => useChainId(), { wrapper: TestProviderWrapper }) - expect(result.error).toBeTruthy() + // Mock console error because the hook will throw and show a huge error message in test output + const consoleErrorMock = jest.spyOn(console, 'error').mockImplementation() + try { + renderHook(() => useChainId(), { wrapper: TestProviderWrapper }) + } catch (error) { + expect((error as Error).message).toBe('Invalid chain short name in the URL') + } + consoleErrorMock.mockRestore() }) it('should return the last used chain id if no chain in the URL', () => { diff --git a/hooks/__tests__/useChains.test.ts b/hooks/__tests__/useChains.test.ts index e9ec7b4778..7156fc9a2c 100644 --- a/hooks/__tests__/useChains.test.ts +++ b/hooks/__tests__/useChains.test.ts @@ -1,5 +1,5 @@ import { getChainsConfig } from '@gnosis.pm/safe-react-gateway-sdk' -import { act, renderHook } from '@testing-library/react-hooks' +import { act, renderHook } from '@testing-library/react' import TestProviderWrapper from '@/mocks/TestProviderWrapper' import useChains, { useInitChains } from '@/hooks/useChains' diff --git a/hooks/__tests__/useGasPrice.test.ts b/hooks/__tests__/useGasPrice.test.ts index 3405273876..dbfcd47bab 100644 --- a/hooks/__tests__/useGasPrice.test.ts +++ b/hooks/__tests__/useGasPrice.test.ts @@ -1,4 +1,4 @@ -import { act, renderHook } from '@testing-library/react-hooks' +import { act, renderHook } from '@testing-library/react' import { BigNumber } from 'ethers' import useGasPrice from '@/hooks/useGasPrice' diff --git a/hooks/useMnemonicName/index.test.ts b/hooks/__tests__/useMnemonicName.test.ts similarity index 94% rename from hooks/useMnemonicName/index.test.ts rename to hooks/__tests__/useMnemonicName.test.ts index b293198d84..f9bf9214c2 100644 --- a/hooks/useMnemonicName/index.test.ts +++ b/hooks/__tests__/useMnemonicName.test.ts @@ -1,5 +1,5 @@ -import { getRandomName, useMnemonicName, useMnemonicSafeName } from '.' -import { renderHook } from '@testing-library/react-hooks' +import { getRandomName, useMnemonicName, useMnemonicSafeName } from '../useMnemonicName' +import { renderHook } from '@testing-library/react' // Mock useCurrentChain hook jest.mock('@/hooks/useChains', () => ({ diff --git a/hooks/__tests__/usePathRewrite.test.ts b/hooks/__tests__/usePathRewrite.test.ts index 57a549e5fb..6436bb3e58 100644 --- a/hooks/__tests__/usePathRewrite.test.ts +++ b/hooks/__tests__/usePathRewrite.test.ts @@ -1,4 +1,4 @@ -import { act, renderHook } from '@testing-library/react-hooks' +import { act, renderHook } from '@testing-library/react' import { useRouter } from 'next/router' import usePathRewrite, { useQueryRewrite } from '@/hooks/usePathRewrite' diff --git a/hooks/__tests__/useSafeAddress.test.ts b/hooks/__tests__/useSafeAddress.test.ts index 86889427d8..6e7ff33a9d 100644 --- a/hooks/__tests__/useSafeAddress.test.ts +++ b/hooks/__tests__/useSafeAddress.test.ts @@ -1,6 +1,6 @@ import useSafeAddress from '@/hooks/useSafeAddress' import { useRouter } from 'next/router' -import { renderHook } from '@testing-library/react-hooks' +import { renderHook } from '@testing-library/react' // Mock useRouter jest.mock('next/router', () => ({ diff --git a/package.json b/package.json index 7b1103d267..acb580d3bf 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "@gnosis.pm/safe-react-gateway-sdk": "^3.1.2", "@sentry/types": "^7.1.1", "@testing-library/jest-dom": "^5.16.4", - "@testing-library/react-hooks": "^8.0.0", + "@testing-library/react": "^13.3.0", "@testing-library/user-event": "^14.2.0", "@types/jest": "^28.1.1", "@types/js-cookie": "^3.0.2", diff --git a/yarn.lock b/yarn.lock index c6ccde7961..6d28bb4ea4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -22,6 +22,13 @@ dependencies: "@babel/highlight" "^7.16.7" +"@babel/code-frame@^7.10.4": + version "7.18.6" + resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.18.6.tgz#3b25d38c89600baa2dcc219edfa88a74eb2c427a" + integrity sha512-TDCmlK5eOvH+eH7cdAFlNXeVJqWIQ7gW9tY1GJIpUtFb6CmjVyq2VM3u71bOyR8CRihcCgMUYoDNyLXao3+70Q== + dependencies: + "@babel/highlight" "^7.18.6" + "@babel/compat-data@^7.13.11", "@babel/compat-data@^7.17.10": version "7.18.5" resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.18.5.tgz#acac0c839e317038c73137fbb6ef71a1d6238471" @@ -146,6 +153,11 @@ resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.16.7.tgz#e8c602438c4a8195751243da9031d1607d247cad" integrity sha512-hsEnFemeiW4D08A5gUAZxLBTXpZ39P+a+DGDsHw1yxqyQ/jzFEnxf5uTEGp+3bzAbNOxU1paTgYS4ECU/IgfDw== +"@babel/helper-validator-identifier@^7.18.6": + version "7.18.6" + resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.18.6.tgz#9c97e30d31b2b8c72a1d08984f2ca9b574d7a076" + integrity sha512-MmetCkz9ej86nJQV+sFCxoGGrUbU3q02kgLciwkrt9QqEB7cP39oKEY0PakknEO0Gu20SskMRi+AYZ3b1TpN9g== + "@babel/helper-validator-option@^7.16.7": version "7.16.7" resolved "https://registry.yarnpkg.com/@babel/helper-validator-option/-/helper-validator-option-7.16.7.tgz#b203ce62ce5fe153899b617c08957de860de4d23" @@ -169,6 +181,15 @@ chalk "^2.0.0" js-tokens "^4.0.0" +"@babel/highlight@^7.18.6": + version "7.18.6" + resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.18.6.tgz#81158601e93e2563795adcbfbdf5d64be3f2ecdf" + integrity sha512-u7stbOuYjaPezCuLj29hNW1v64M2Md2qupEKP1fHc7WdOA3DgLh37suiSrZYY7haUB7iBeQZ9P1uiRF359do3g== + dependencies: + "@babel/helper-validator-identifier" "^7.18.6" + chalk "^2.0.0" + js-tokens "^4.0.0" + "@babel/parser@^7.1.0", "@babel/parser@^7.14.7", "@babel/parser@^7.16.7", "@babel/parser@^7.18.5": version "7.18.5" resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.18.5.tgz#337062363436a893a2d22faa60be5bb37091c83c" @@ -2724,6 +2745,20 @@ dependencies: "@sinonjs/commons" "^1.7.0" +"@testing-library/dom@^8.5.0": + version "8.14.0" + resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.14.0.tgz#c9830a21006d87b9ef6e1aae306cf49b0283e28e" + integrity sha512-m8FOdUo77iMTwVRCyzWcqxlEIk+GnopbrRI15a0EaLbpZSCinIVI4kSQzWhkShK83GogvEFJSsHF3Ws0z1vrqA== + dependencies: + "@babel/code-frame" "^7.10.4" + "@babel/runtime" "^7.12.5" + "@types/aria-query" "^4.2.0" + aria-query "^5.0.0" + chalk "^4.1.0" + dom-accessibility-api "^0.5.9" + lz-string "^1.4.4" + pretty-format "^27.0.2" + "@testing-library/jest-dom@^5.16.4": version "5.16.4" resolved "https://registry.yarnpkg.com/@testing-library/jest-dom/-/jest-dom-5.16.4.tgz#938302d7b8b483963a3ae821f1c0808f872245cd" @@ -2739,13 +2774,14 @@ lodash "^4.17.15" redent "^3.0.0" -"@testing-library/react-hooks@^8.0.0": - version "8.0.0" - resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.0.tgz#7d0164bffce4647f506039de0a97f6fcbd20f4bf" - integrity sha512-uZqcgtcUUtw7Z9N32W13qQhVAD+Xki2hxbTR461MKax8T6Jr8nsUvZB+vcBTkzY2nFvsUet434CsgF0ncW2yFw== +"@testing-library/react@^13.3.0": + version "13.3.0" + resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-13.3.0.tgz#bf298bfbc5589326bbcc8052b211f3bb097a97c5" + integrity sha512-DB79aA426+deFgGSjnf5grczDPiL4taK3hFaa+M5q7q20Kcve9eQottOG5kZ74KEr55v0tU2CQormSSDK87zYQ== dependencies: "@babel/runtime" "^7.12.5" - react-error-boundary "^3.1.0" + "@testing-library/dom" "^8.5.0" + "@types/react-dom" "^18.0.0" "@testing-library/user-event@^14.2.0": version "14.2.0" @@ -2843,6 +2879,11 @@ loglevel "^1.8.0" web3-utils "^1.7.3" +"@types/aria-query@^4.2.0": + version "4.2.2" + resolved "https://registry.yarnpkg.com/@types/aria-query/-/aria-query-4.2.2.tgz#ed4e0ad92306a704f9fb132a0cfcf77486dbe2bc" + integrity sha512-HnYpAE1Y6kRyKM/XkEuiRQhTHvkzMBurTHnpFLYLBGPIylZNPs9jJcuOOYWxPLJCSEtmZT0Y8rHDokKN7rRTig== + "@types/babel__core@^7.1.14": version "7.1.19" resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.19.tgz#7b497495b7d1b4812bdb9d02804d0576f43ee460" @@ -3008,7 +3049,7 @@ resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf" integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w== -"@types/react-dom@18.0.5": +"@types/react-dom@18.0.5", "@types/react-dom@^18.0.0": version "18.0.5" resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-18.0.5.tgz#330b2d472c22f796e5531446939eacef8378444a" integrity sha512-OWPWTUrY/NIrjsAPkAk1wW9LZeIjSvkXRhclsFO8CZcZGCOg2G0YZy4ft+rOyYxy8B7ui5iZzi9OkDebZ7/QSA== @@ -4240,7 +4281,7 @@ chalk@^3.0.0: ansi-styles "^4.1.0" supports-color "^7.1.0" -chalk@^4.0.0: +chalk@^4.0.0, chalk@^4.1.0: version "4.1.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01" integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA== @@ -4739,7 +4780,7 @@ doctrine@^3.0.0: dependencies: esutils "^2.0.2" -dom-accessibility-api@^0.5.6: +dom-accessibility-api@^0.5.6, dom-accessibility-api@^0.5.9: version "0.5.14" resolved "https://registry.yarnpkg.com/dom-accessibility-api/-/dom-accessibility-api-0.5.14.tgz#56082f71b1dc7aac69d83c4285eef39c15d93f56" integrity sha512-NMt+m9zFMPZe0JcY9gN224Qvk6qLIdqex29clBvc/y75ZBX9YA9wNK3frsYvu2DI1xcCIwxwnX+TlsJ2DSOADg== @@ -7448,6 +7489,11 @@ ltgt@~2.2.0: resolved "https://registry.yarnpkg.com/ltgt/-/ltgt-2.2.1.tgz#f35ca91c493f7b73da0e07495304f17b31f87ee5" integrity sha512-AI2r85+4MquTw9ZYqabu4nMwy9Oftlfa/e/52t9IjtfG+mGBbTNdAoZ3RQKLHR6r0wQnwZnPIEh/Ya6XTWAKNA== +lz-string@^1.4.4: + version "1.4.4" + resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26" + integrity sha512-0ckx7ZHRPqb0oUm8zNr+90mtf9DQB60H1wMCjBtfi62Kl3a7JbHob6gA2bC+xRvZoOL+1hzUK8jeuEIQE8svEQ== + make-dir@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f" @@ -8097,7 +8143,7 @@ prettier@^2.7.0: resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.0.tgz#a4fdae07e5596c51c9857ea676cd41a0163879d6" integrity sha512-nwoX4GMFgxoPC6diHvSwmK/4yU8FFH3V8XWtLQrbj4IBsK2pkYhG4kf/ljF/haaZ/aii+wNJqISrCDPgxGWDVQ== -pretty-format@^27.0.0, pretty-format@^27.5.1: +pretty-format@^27.0.0, pretty-format@^27.0.2, pretty-format@^27.5.1: version "27.5.1" resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-27.5.1.tgz#2181879fdea51a7a5851fb39d920faa63f01d88e" integrity sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ== @@ -8335,13 +8381,6 @@ react-dom@18.2.0: loose-envify "^1.1.0" scheduler "^0.23.0" -react-error-boundary@^3.1.0: - version "3.1.4" - resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0" - integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA== - dependencies: - "@babel/runtime" "^7.12.5" - react-hook-form@^7.32.1: version "7.32.1" resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.32.1.tgz#103de7b7ef3a7d295264433e4464bd17c8e4bdc9"