Skip to content

Commit

Permalink
Use @testing-library/react-hooks for testing useFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
vtaits committed Nov 2, 2024
1 parent 8cfe28c commit 87ca2b0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
3 changes: 3 additions & 0 deletions packages/react-filterlist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@
"@biomejs/biome": "^1.9.4",
"@storybook/react": "^8.4.0",
"@swc/core": "^1.7.42",
"@testing-library/dom": "^10.4.0",
"@testing-library/react-hooks": "^8.0.1",
"@types/qs": "^6.9.16",
"@types/react": "^18.3.12",
"@vitejs/plugin-react": "^4.3.3",
"@vtaits/filterlist": "^2.4.0",
"@vtaits/react-router-dom-fake-browser": "^1.0.0",
"happy-dom": "^15.8.0",
"qs": "^6.13.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
46 changes: 22 additions & 24 deletions packages/react-filterlist/src/useFilter.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { renderHook } from "@testing-library/react-hooks";
import {
type Filterlist,
initialRequestParams,
listInitialState,
} from "@vtaits/filterlist";
import { useCallback, useMemo } from "react";
import { afterEach, describe, expect, test, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { useFilter } from "./useFilter";

vi.mock("react");
vi.mocked(useCallback).mockImplementation((fn) => fn);
vi.mocked(useMemo).mockImplementation((fn) => fn());

afterEach(() => {
vi.clearAllMocks();
});

describe("not inited", () => {
const filter = useFilter(null, null, null, "test");
const {
result: { current: filter },
} = renderHook(() => useFilter(null, null, null, "test"));

test("setFilterValue", () => {
filter.setFilterValue("foo");
Expand Down Expand Up @@ -51,21 +45,25 @@ describe("inited", () => {
resetFilter: vi.fn(),
} as unknown as Filterlist<unknown, unknown, unknown>;

const filter = useFilter(
{
...initialRequestParams,
appliedFilters: {
test: "baz",
const {
result: { current: filter },
} = renderHook(() =>
useFilter(
{
...initialRequestParams,
appliedFilters: {
test: "baz",
},
},
},
{
...listInitialState,
filters: {
test: "bar",
{
...listInitialState,
filters: {
test: "bar",
},
},
},
filterlist,
"test",
filterlist,
"test",
),
);

test("setFilterValue", () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/react-filterlist/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
environment: "happy-dom",
},
});
16 changes: 15 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ __metadata:
languageName: node
linkType: hard

"@testing-library/dom@npm:10.4.0":
"@testing-library/dom@npm:10.4.0, @testing-library/dom@npm:^10.4.0":
version: 10.4.0
resolution: "@testing-library/dom@npm:10.4.0"
dependencies:
Expand Down Expand Up @@ -2226,12 +2226,15 @@ __metadata:
"@biomejs/biome": "npm:^1.9.4"
"@storybook/react": "npm:^8.4.0"
"@swc/core": "npm:^1.7.42"
"@testing-library/dom": "npm:^10.4.0"
"@testing-library/react-hooks": "npm:^8.0.1"
"@types/qs": "npm:^6.9.16"
"@types/react": "npm:^18.3.12"
"@vitejs/plugin-react": "npm:^4.3.3"
"@vtaits/filterlist": "npm:^2.4.0"
"@vtaits/react-router-dom-fake-browser": "npm:^1.0.0"
"@vtaits/use-latest": "npm:^1.3.0"
happy-dom: "npm:^15.8.0"
is-promise: "npm:^4.0.0"
qs: "npm:^6.13.0"
react: "npm:^18.3.1"
Expand Down Expand Up @@ -3847,6 +3850,17 @@ __metadata:
languageName: node
linkType: hard

"happy-dom@npm:^15.8.0":
version: 15.8.0
resolution: "happy-dom@npm:15.8.0"
dependencies:
entities: "npm:^4.5.0"
webidl-conversions: "npm:^7.0.0"
whatwg-mimetype: "npm:^3.0.0"
checksum: 10/eb031f17923f8d5064de1b8ec389f417c0a6ab47eee50b5962c588deacb7da8575533cc25a3b8750fdd260a06a9bfc9746a47620d263c40574e0a5e0207cc39f
languageName: node
linkType: hard

"has-bigints@npm:^1.0.1":
version: 1.0.2
resolution: "has-bigints@npm:1.0.2"
Expand Down

0 comments on commit 87ca2b0

Please sign in to comment.