Skip to content

Commit

Permalink
chore(core): add specs for useCustom hook (refinedev#3886)
Browse files Browse the repository at this point in the history
* chore(core): add specs for useCustom hook

* fix(core): update test:all:coverage script

* fix(core): update test:coverage script

* fix(core): remove trailing . from test:all:coverage script
  • Loading branch information
BatuhanW authored Mar 16, 2023
1 parent 995fdce commit fcd94ef
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"lint:staged": "lint-staged",
"test": "lerna run test --stream --scope @refinedev/core",
"test:all": "lerna run test --stream --scope @refinedev/* --scope create-refine-app",
"test:coverage": "npm run test --stream -- -- -- --coverage",
"test:all:coverage": "npm run test:all --stream -- -- -- --coverage"
"test:coverage": "npm run test --stream -- -- --coverage",
"test:all:coverage": "npm run test:all --stream -- -- --coverage"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
Expand Down
74 changes: 72 additions & 2 deletions packages/core/src/hooks/data/useCustom.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { renderHook, waitFor } from "@testing-library/react";

import { MockJSONServer, TestWrapper } from "@test";

import * as ReactQuery from "@tanstack/react-query";

import { useCustom } from "./useCustom";

describe("useCustom Hook", () => {
it("with rest json server", async () => {
it("works with rest json server", async () => {
const { result } = renderHook(
() => useCustom({ url: "remoteUrl", method: "get" }),
() =>
useCustom({
url: "remoteUrl",
method: "get",
}),
{
wrapper: TestWrapper({
dataProvider: MockJSONServer,
Expand All @@ -24,4 +30,68 @@ describe("useCustom Hook", () => {

expect(data?.data).toHaveLength(2);
});

describe("custom query key", () => {
describe("without custom query key", () => {
const config = { sorters: [{ field: "id", order: "desc" }] } as any;
const meta = { meta: "meta" };

it("builds query key itself", async () => {
const useQuerySpy = jest.spyOn(ReactQuery, "useQuery");

renderHook(
() =>
useCustom({
url: "remoteUrl",
method: "get",
config,
meta,
}),
{
wrapper: TestWrapper({
dataProvider: MockJSONServer,
resources: [{ name: "posts" }],
}),
},
);

expect(useQuerySpy).toHaveBeenLastCalledWith(
expect.objectContaining({
queryKey: [
undefined,
"custom",
"get",
"remoteUrl",
{ ...config, ...meta },
],
}),
);
});
});

describe("with custom query key", () => {
it("prioritizes custom query key", async () => {
const useQuerySpy = jest.spyOn(ReactQuery, "useQuery");

renderHook(
() =>
useCustom({
url: "remoteUrl",
method: "get",
queryOptions: { queryKey: ["MyKey"] },
}),
{
wrapper: TestWrapper({
dataProvider: MockJSONServer,
resources: [{ name: "posts" }],
}),
},
);

expect(useQuerySpy).toHaveBeenLastCalledWith(
expect.objectContaining({ queryKey: ["MyKey"] }),
);
});
});
});
});

0 comments on commit fcd94ef

Please sign in to comment.