Peter Makowski has proposed merging ~petermakowski/maas-site-manager:react-hooks-testing-lib-MAASENG-1713 into maas-site-manager:main.
Commit message: chore: remove @testing-library/react-hooks - use renderHook from @testing-library/react - update tests to use act Requested reviews: MAAS Committers (maas-committers) For more details, see: https://code.launchpad.net/~petermakowski/maas-site-manager/+git/site-manager/+merge/443183 -- Your team MAAS Committers is requested to review the proposed merge of ~petermakowski/maas-site-manager:react-hooks-testing-lib-MAASENG-1713 into maas-site-manager:main.
diff --git a/frontend/package.json b/frontend/package.json index e8ed433..49e2869 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -41,7 +41,6 @@ "@remix-run/web-fetch": "4.3.4", "@testing-library/jest-dom": "5.16.5", "@testing-library/react": "14.0.0", - "@testing-library/react-hooks": "8.0.1", "@testing-library/user-event": "14.4.3", "@total-typescript/ts-reset": "0.4.2", "@types/axios": "0.14.0", diff --git a/frontend/src/hooks/useDebouncedValue.test.ts b/frontend/src/hooks/useDebouncedValue.test.ts index ba6d055..f6739f9 100644 --- a/frontend/src/hooks/useDebouncedValue.test.ts +++ b/frontend/src/hooks/useDebouncedValue.test.ts @@ -1,17 +1,13 @@ import useDebouncedValue from "./useDebouncedValue"; -import { renderHook } from "@/test-utils"; +import { act, renderHook } from "@/test-utils"; describe("useDebouncedValue", () => { - beforeAll(() => { + beforeEach(() => { vi.useFakeTimers(); }); afterEach(() => { - vi.clearAllTimers(); - }); - - afterAll(() => { vi.useRealTimers(); }); @@ -21,12 +17,11 @@ describe("useDebouncedValue", () => { value: "value", }, }); - expect(result.current).toBe("value"); - - await rerender({ value: "new-value" }); - await vi.advanceTimersToNextTimer(); - + rerender({ value: "new-value" }); + await act(async () => { + await vi.runAllTimersAsync(); + }); expect(result.current).toBe("new-value"); }); @@ -37,12 +32,11 @@ describe("useDebouncedValue", () => { delay: 5, }, }); - expect(result.current).toBe("value"); - - await rerender({ value: "new-value", delay: 5 }); - await vi.advanceTimersByTime(5); - + rerender({ value: "new-value", delay: 5 }); + await act(async () => { + await vi.advanceTimersByTimeAsync(5); + }); expect(result.current).toBe("new-value"); }); }); diff --git a/frontend/src/hooks/useDebouncedValue.ts b/frontend/src/hooks/useDebouncedValue.ts index 1cf4167..4140266 100644 --- a/frontend/src/hooks/useDebouncedValue.ts +++ b/frontend/src/hooks/useDebouncedValue.ts @@ -1,6 +1,6 @@ import { useEffect, useState } from "react"; -const DEFAULT_DELAY = 500; +export const DEFAULT_DELAY = 500; function useDebouncedValue<T>(value: T, delay = DEFAULT_DELAY): T { const [debouncedValue, setDebouncedValue] = useState<T>(value); diff --git a/frontend/src/hooks/usePagination.test.ts b/frontend/src/hooks/usePagination.test.ts index a999c40..388d05e 100644 --- a/frontend/src/hooks/usePagination.test.ts +++ b/frontend/src/hooks/usePagination.test.ts @@ -1,6 +1,6 @@ import usePagination from "./usePagination"; -import { hookAct, renderHook } from "@/test-utils"; +import { renderHook, act } from "@/test-utils"; describe("usePagination", () => { const samplePageSize = 50; @@ -12,26 +12,22 @@ describe("usePagination", () => { expect(result.current.page).toBe(1); }); - it("next and previous page functions work correctly", () => { + it("next and previous page functions work correctly", async () => { const { result } = renderHook(() => usePagination(samplePageSize, sampleTotalCount)); - - hookAct(() => { + await act(() => { result.current.handleNextClick(); }); - expect(result.current.page).toBe(2); - - hookAct(() => { + await act(() => { result.current.handlePreviousClick(); }); - expect(result.current.page).toBe(1); }); - it("should reset page count after page size is changed", () => { + it("should reset page count after page size is changed", async () => { const { result } = renderHook(() => usePagination(samplePageSize, sampleTotalCount)); - hookAct(() => { + await act(() => { result.current.handleNextClick(); result.current.handlePageSizeChange(10); }); diff --git a/frontend/src/test-utils.tsx b/frontend/src/test-utils.tsx index de43cdc..75de274 100644 --- a/frontend/src/test-utils.tsx +++ b/frontend/src/test-utils.tsx @@ -70,9 +70,8 @@ const getByTextContent = (text: string | RegExp) => { }); }; -export { screen, within, waitFor, act } from "@testing-library/react"; +export { screen, within, waitFor, act, renderHook } from "@testing-library/react"; export { customRender as render }; -export { renderHook, act as hookAct } from "@testing-library/react-hooks"; export { default as userEvent } from "@testing-library/user-event"; export { renderWithMemoryRouter }; export { Providers }; diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 1afec12..25be656 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -1763,14 +1763,6 @@ lodash "^4.17.15" redent "^3.0.0" -"@testing-library/react-hooks@8.0.1": - version "8.0.1" - resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz#0924bbd5b55e0c0c0502d1754657ada66947ca12" - integrity sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g== - dependencies: - "@babel/runtime" "^7.12.5" - react-error-boundary "^3.1.0" - "@testing-library/react@14.0.0": version "14.0.0" resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-14.0.0.tgz#59030392a6792450b9ab8e67aea5f3cc18d6347c" @@ -5552,13 +5544,6 @@ react-error-boundary@4.0.4: dependencies: "@babel/runtime" "^7.12.5" -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-fast-compare@^2.0.1: version "2.0.4" resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-2.0.4.tgz#e84b4d455b0fec113e0402c329352715196f81f9"
-- Mailing list: https://launchpad.net/~sts-sponsors Post to : sts-sponsors@lists.launchpad.net Unsubscribe : https://launchpad.net/~sts-sponsors More help : https://help.launchpad.net/ListHelp