Skip to content

Commit

Permalink
chore(core): update queryKeys usages
Browse files Browse the repository at this point in the history
  • Loading branch information
aliemir committed Aug 28, 2023
1 parent a7a9aba commit 6f2258d
Show file tree
Hide file tree
Showing 11 changed files with 523 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/definitions/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { userFriendlySecond } from "./userFriendlySeconds";
export { importCSVMapper } from "./importCSVMapper";
export { userFriendlyResourceName } from "./userFriendlyResourceName";
export { handleUseParams } from "./handleUseParams";
export { queryKeys } from "./queryKeys";
export { queryKeys, queryKeysReplacement } from "./queryKeys";
export { hasPermission } from "./hasPermission";
export { routeGenerator } from "./routeGenerator";
export { createTreeView } from "./treeView/createTreeView";
Expand Down
16 changes: 11 additions & 5 deletions packages/core/src/definitions/helpers/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function convertToLegacy(segments: KeySegment[]) {
// [audit, resource, action, params] (for log and list)
// or
// [audit, action, params] (for rename)
if (segments.length === 4 && segments[2] === "list") {
if (segments[2] === "list") {
return ["logList", segments[1], segments[3]];
}
}
Expand Down Expand Up @@ -184,8 +184,11 @@ class DataIdRequiringKeyBuilder extends BaseKeyBuilder {
super(segments);
}

id(idValue: IdType) {
return new ParamsKeyBuilder([...this.segments, String(idValue)]);
id(idValue?: IdType) {
return new ParamsKeyBuilder([
...this.segments,
idValue ? String(idValue) : undefined,
]);
}
}

Expand All @@ -195,7 +198,10 @@ class DataIdsRequiringKeyBuilder extends BaseKeyBuilder {
}

ids(...idsValue: IdsType) {
return new ParamsKeyBuilder([...this.segments, idsValue.map(String)]);
return new ParamsKeyBuilder([
...this.segments,
...(idsValue.length ? [idsValue.map((el) => String(el))] : []),
]);
}
}

Expand Down Expand Up @@ -239,7 +245,7 @@ class DataKeyBuilder extends BaseKeyBuilder {
super(segments);
}

resource(resourceName: string) {
resource(resourceName?: string) {
return new DataResourceKeyBuilder([...this.segments, resourceName]);
}

Expand Down
329 changes: 328 additions & 1 deletion packages/core/src/definitions/helpers/queryKeys/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { queryKeys } from ".";
import { queryKeys, queryKeysReplacement } from ".";

describe("queryKeys", () => {
describe("all", () => {
Expand Down Expand Up @@ -278,3 +278,330 @@ describe("queryKeys", () => {
});
});
});

describe("queryKeysReplacement should match queryKeys", () => {
describe("all", () => {
it("should return default data-provider", () => {
expect(queryKeysReplacement(true)().all).toEqual(["default"]);
});
it("should return custom data-provider", () => {
expect(
queryKeysReplacement(true)(undefined, "custom-data-provider")
.all,
).toEqual(["custom-data-provider"]);
});
});
describe("resourceAll", () => {
it("should return without data provider", () => {
expect(queryKeysReplacement(true)("post").resourceAll).toEqual([
"default",
"post",
]);
});
it("should return with data provider", () => {
expect(
queryKeysReplacement(true)("post", "custom-data-provider")
.resourceAll,
).toEqual(["custom-data-provider", "post"]);
});
it("should return without resource", () => {
expect(
queryKeysReplacement(true)(undefined, "custom-data-provider")
.resourceAll,
).toEqual(["custom-data-provider", ""]);
});
});
describe("list", () => {
it("should return without data provider", () => {
expect(queryKeysReplacement(true)("post").list()).toEqual([
"default",
"post",
"list",
{},
]);
});
it("should return with data provider", () => {
expect(
queryKeysReplacement(true)(
"post",
"custom-data-provider",
).list(),
).toEqual(["custom-data-provider", "post", "list", {}]);
});
it("should return without resource", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
).list(),
).toEqual(["custom-data-provider", "", "list", {}]);
});
it("should return with config", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
).list({
hasPagination: false,
}),
).toEqual([
"custom-data-provider",
"",
"list",
{
hasPagination: false,
},
]);
});
it("should return with config and meta", () => {
expect(
queryKeysReplacement(true)(undefined, "custom-data-provider", {
meta: {
foo: "bar",
},
}).list({
hasPagination: false,
}),
).toEqual([
"custom-data-provider",
"",
"list",
{
hasPagination: false,
meta: {
foo: "bar",
},
},
]);
});
it("should return with config and metaData", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
undefined,
{
meta: {
foo: "bar",
},
},
).list({
hasPagination: false,
}),
).toEqual([
"custom-data-provider",
"",
"list",
{
hasPagination: false,
meta: {
foo: "bar",
},
},
]);
});
});
describe("many", () => {
it("should return without data provider", () => {
expect(queryKeysReplacement(true)("post").many()).toEqual([
"default",
"post",
"getMany",
{},
]);
});
it("should return with data provider", () => {
expect(
queryKeysReplacement(true)(
"post",
"custom-data-provider",
).many(),
).toEqual(["custom-data-provider", "post", "getMany", {}]);
});
it("should return without resource", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
).many(),
).toEqual(["custom-data-provider", "", "getMany", {}]);
});
it("should return with ids", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
).many([1, 2, 3]),
).toEqual([
"custom-data-provider",
"",
"getMany",
["1", "2", "3"],
{},
]);
});
it("should return with ids and meta", () => {
expect(
queryKeysReplacement(true)(undefined, "custom-data-provider", {
meta: {
foo: "bar",
},
}).many([1, 2, 3]),
).toEqual([
"custom-data-provider",
"",
"getMany",
["1", "2", "3"],
{
meta: {
foo: "bar",
},
},
]);
});
it("should return with ids and metaData", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
undefined,
{
meta: {
foo: "bar",
},
},
).many([1, 2, 3]),
).toEqual([
"custom-data-provider",
"",
"getMany",
["1", "2", "3"],
{
meta: {
foo: "bar",
},
},
]);
});
});
describe("detail", () => {
it("should return without data provider", () => {
expect(queryKeysReplacement(true)("post").detail(1)).toEqual([
"default",
"post",
"detail",
"1",
{},
]);
});
it("should return with data provider", () => {
expect(
queryKeysReplacement(true)(
"post",
"custom-data-provider",
).detail(1),
).toEqual(["custom-data-provider", "post", "detail", "1", {}]);
});
it("should return without resource", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
).detail(1),
).toEqual(["custom-data-provider", "", "detail", "1", {}]);
});
it("should return without id", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
).detail(),
).toEqual(["custom-data-provider", "", "detail", undefined, {}]);
});
it("should return with ids", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
).detail(1),
).toEqual(["custom-data-provider", "", "detail", "1", {}]);
});
it("should return with ids and meta", () => {
expect(
queryKeysReplacement(true)(undefined, "custom-data-provider", {
meta: {
foo: "bar",
},
}).detail(1),
).toEqual([
"custom-data-provider",
"",
"detail",
"1",
{
meta: {
foo: "bar",
},
},
]);
});
it("should return with ids and metaData", () => {
expect(
queryKeysReplacement(true)(
undefined,
"custom-data-provider",
undefined,
{
meta: {
foo: "bar",
},
},
).detail(1),
).toEqual([
"custom-data-provider",
"",
"detail",
"1",
{
meta: {
foo: "bar",
},
},
]);
});
});
describe("logList", () => {
it("should return with resource", () => {
expect(queryKeysReplacement(true)("post").logList()).toEqual([
"logList",
"post",
]);
});
it("should return with meta", () => {
expect(
queryKeysReplacement(true)("post").logList({
foo: "bar",
}),
).toEqual([
"logList",
"post",
{
foo: "bar",
},
]);
});
it("should return with metaData", () => {
expect(
queryKeysReplacement(true)("post", undefined, undefined, {
foo: "bar",
}).logList(),
).toEqual([
"logList",
"post",
{
foo: "bar",
},
]);
});
});
});
Loading

0 comments on commit 6f2258d

Please sign in to comment.